【发布时间】: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