如何通过标签选择和移动UIimageViews?

时间:2012-03-09 13:47:35

标签: objective-c xcode cocoa-touch tags uiimageview

我正在寻找任何方法如何触摸图像并改变你的位置。但同样需要在屏幕上显示20张图像。所以我生成了20个带有不同标签的UIimageViews。但我仍然不知道如何通过标签选择这些UIimageViews。

有我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

}

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

    imageView = (UIImageView *)[imageView viewWithTag:imageView.tag] ; 
    NSLog(@"%d",imageView.tag);
    imageView.center = location;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    imageView.tag = arc4random() % 19;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView = nil;

    for (int i; i<19; i++) {

        int randX = arc4random() % 240;
        int randY = arc4random() % 368;

        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_1.png"]];
        imageView.frame = CGRectMake(randX, randY, 80, 92);
        imageView.tag = i;

        //NSLog(@"%d",imageView.tag);

        [self.view addSubview:imageView];
    } 
}

感谢您阅读

1 个答案:

答案 0 :(得分:0)

如果您有标签列表,请执行以下操作:

#define TAG_VIEW1 1
#define TAG_VIEW2 2

您可以轻松完成:

[self.view viewWithTag:TAG_VIEW1];

该方法将为您提供该标记的视图。