【问题标题】:How to select and move UIimageViews by tag?如何按标签选择和移动 UIimageViews?
【发布时间】:2012-03-26 23:07:04
【问题描述】:

我正在寻找任何方法来触摸图像并改变你的位置。但同样的事情需要在屏幕上显示 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];
    } 
}

感谢您的阅读

【问题讨论】:

  • viewWithTag 有什么问题?

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


【解决方案1】:

如果您有标签列表,如下所示:

#define TAG_VIEW1 1
#define TAG_VIEW2 2

你可以轻松做到:

[self.view viewWithTag:TAG_VIEW1];

该方法将返回该标签的视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-08
    • 2012-10-18
    • 1970-01-01
    • 2011-07-09
    • 2011-03-26
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多