修饰 UIImageView 的内部

发布于 2024-11-15 15:21:22 字数 307 浏览 2 评论 0原文

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

代码:

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

问候!

I'm trying to implement touch event similar to a Touch Up Inside a UIButton. I've seen some codes using touchesBegan:withEvent but it looks I need to slide a bit my finger over the screen. I actually just want to touch in the image

Code:

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

Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-11-22 15:21:22

您可以使用手势来触摸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
{

}

You could use the gesture to get the touch on 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];

Implement the ClickEventOnImage function.

-(void) ClickEventOnImage:(id) sender
{

}
我是男神闪亮亮 2024-11-22 15:21:22

UIImageView 在这方面臭名昭著,因为它的 userInteractionEnabled 默认设置为 NO。您必须通过执行以下操作来启用它

imageView.userInteractionEnabled = YES;

,然后您可以使用手势识别器或普通的 touches* 方法来处理触摸。

UIImageView is notorious regarding this as its userInteractionEnabled is set to NO by default. You will have to enable it by doing

imageView.userInteractionEnabled = YES;

And then you can use gesture recognizers or the normal touches* methods to handle touches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文