我一直在尝试使用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];
接触结束:
-(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!");
}