我正在尝试实施类似于Touch Up Inside
和UIButton
的触摸事件。我看过一些使用touchesBegan:withEvent
的代码,但看起来我需要在屏幕上滑动一下手指。我其实只想触摸图像
代码:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(@"Image touched!");
}
问候!
答案 0 :(得分:46)
您可以使用手势来触及UIImageView
。
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(ClickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:2];
[tapRecognizer setDelegate:self];
//Don't forget to set the userInteractionEnabled to YES, by default It's NO.
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:tapRecognizer];
实施ClickEventOnImage
功能。
-(void) ClickEventOnImage:(id) sender
{
}
答案 1 :(得分:6)
UIImageView
因此而臭名昭着,因为userInteractionEnabled
默认设置为NO
。你必须通过
imageView.userInteractionEnabled = YES;
然后您可以使用手势识别器或普通touches*
方法来处理触摸。