【发布时间】:2012-10-08 18:55:34
【问题描述】:
我一直在尝试使用touchedBegan 和touchedEnded。我基本上只是想在我的视图中创建一个图像。然后能够在用户触摸图像时做一些事情。当我触摸图像时,它确实会执行我的 if 语句中的代码。知道我做错了什么吗?
viewDidload:
UIImage *image = [UIImage imageNamed:@"myimage1.png"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 320, 200);
[self.view addSubview:imageView];
imageViewsArray = [NSArray arrayWithObjects:imageView, nil];
touchesEnded:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// do this on touch
UIView *touchedView = [touch view];
if ([imageViewsArray indexOfObject:touchedView] != NSNotFound) {
// not not found means found!
NSLog(@"Got Touch!");
}
}
我也尝试过使用这个 if 语句:
if ([[touch view] isKindOfClass:[UIImageView class]]) {
NSLog(@"Got Touch 2!");
}
【问题讨论】:
-
查看我的帖子;我认为你的问题是你没有将 UIImageView 的属性 userInteractionEnabled 设置为 YES
标签: objective-c ios view touch