触摸内部以获取UIImageView

时间:2011-06-16 13:16:06

标签: iphone uiimageview

我正在尝试实施类似于Touch Up InsideUIButton的触摸事件。我看过一些使用touchesBegan:withEvent的代码,但看起来我需要在屏幕上滑动一下手指。我其实只想触摸图像

代码:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {   
    NSLog(@"Image touched!");
}

问候!

2 个答案:

答案 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*方法来处理触摸。