【问题标题】:Moving uiimageview created programmatically移动以编程方式创建的 uiimageview
【发布时间】:2012-07-17 11:22:51
【问题描述】:

我写了以下代码,但它不起作用。图片出现了,但我不能移动它

UIImageView *oneselement = [[UIImageView alloc] initWithFrame:self.view.frame];
oneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]];
oneselement.frame = CGRectMake(0, 0, 122, 122);
[self.view addSubview:oneselement];


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if([touch view]==oneselement)
    {
        CGPoint location = [touch locationInView:self.view];
        oneselement.center=location;
    }}

我添加了一张图片查看并写了以下代码

.h

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *oneEl;
}
@property (nonatomic,retain) IBOutlet UIImageView *oneEl;

.m

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==oneEl)
    {
        CGPoint location = [touch locationInView:self.view];
        oneEl.center=location;
    }
}

而且它有效! 我怎样才能让 uiimageview 以编程方式创建的可以移动?

NSMUTABLEARRAY

self.elements=[myElements getElements];

imagesElements = [[NSMutableArray alloc]init];
for(ElemetsList *item in self.elements)
{
        UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]];
        oneelement.frame = CGRectMake(item.positionX, item.positionY, item.width, item.height);
        oneelement.userInteractionEnabled=YES;
        [imagesElements addObject:oneelement];
}

for(UIImageView *img in imagesElements) 
    [self.view addSubview:img];

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    for(UIImageView *img in imagesElements) 
    {  
        if([self view] == img)
        {
            CGPoint location = [touch locationInView:self.view];
            img.center=location;
        }
    }
}

【问题讨论】:

  • 您要移动图像还是旋转图像?
  • 你的代码有内存泄漏,你应该在添加到数组后释放一个元素。

标签: iphone objective-c xcode ios5


【解决方案1】:

像下面的代码一样初始化你的 UIImageView,然后它就会像你的第二个例子一样工作:

ViewController.h

@interface ViewController : UIViewController
{
    UIImageView *oneselement;
}
@property (nonatomic,retain) UIImageView *oneselement;

ViewController.m

//initialize your UIImageView
UIImageView *newOneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]];
self.oneselement = newOneselement;
self.oneselement.userInteractionEnabled = YES;
self.oneselement.frame = CGRectMake(0, 0, 122, 122);
[self.view addSubview:self.oneselement];
[newOneselement release];

//Handle touchesMoved
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==self.oneselement)
    {
        CGPoint location = [touch locationInView:self.view];
        self.oneselement.center=location;
    }
}

【讨论】:

  • 您好!有必要一个元素不是。 H. 以编程方式创建图片。
  • 在初始化 UIImageView 的地方添加一行代码:self.oneselement.userInteractionEnabled = YES;
  • 为什么不呢,你可以创建一个数组,把你的uiimageviews放进去
  • 请查看我尝试这样做但不起作用的第一条消息
【解决方案2】:

请参阅this 链接。
你也可以试试:

为了移动图像,您应该将要移动的代码包含在动画块中:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];

您还可以使用 UIView setAnimationDidStopSelector: 方法创建一个在动画完成后执行的方法。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animateImages)];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];

//other stuff

-(void)animateImages{
     [tapImage startAnimating];
}

【讨论】:

  • 您好!用手指移动图像需要一点点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多