UIImageView 像素完美的“触摸开始”,用于 UIImage 的非透明部分

发布于 2024-12-27 05:48:49 字数 545 浏览 0 评论 0原文

开发iPhone应用程序,我使用了UIImageview,并且设置了不规则形状的图像

// uiimageview type shutter
shutter = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,32.5)];

//setting image on shutter
UIImage* shutterimg = [UIImage imageNamed:@"trayshutter.png"];
[shutter setImage:shutterimg];
// User interaction
shutter.userInteractionEnabled=YES;

,并且使用touchesbegan进行触摸,

一切都很好,但问题是我还在CGRectMake占用的所有矩形区域中获取事件(即0,0,320, 32.5)的 imageview,而我只需要图像上的事件(其形状为 -----|||||-----)。

如何确保我仅在图像上获取事件,而不是在 cgrectmake 覆盖的其余区域上获取事件?

developing iphone app, I have used a UIImageview and i have set an image of irregular shape

// uiimageview type shutter
shutter = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,32.5)];

//setting image on shutter
UIImage* shutterimg = [UIImage imageNamed:@"trayshutter.png"];
[shutter setImage:shutterimg];
// User interaction
shutter.userInteractionEnabled=YES;

and i get touches using touchesbegan

everything is fine, but issue is I also get events in all the rectangle area occupied by the CGRectMake (i.e. 0,0,320,32.5) of imageview, whereas i require events only on the image (which is of shape of -----|||||-----).

How can i make sure that i get events only on the image and not on the rest of area covered by cgrectmake ?

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

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

发布评论

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

评论(1

薄凉少年不暖心 2025-01-03 05:48:49
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  if ([touch view] == yourDesiredImage) {
    // Do something here
  }
}

编辑:也许你可以尝试 UITapGestureRecognizer 代替:

UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
[tapGestureRecognizer addTarget:self action:@selector(yourTouchMethod:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[shutter addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  if ([touch view] == yourDesiredImage) {
    // Do something here
  }
}

EDIT: maybe you can try UITapGestureRecognizer instead:

UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
[tapGestureRecognizer addTarget:self action:@selector(yourTouchMethod:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[shutter addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文