【问题标题】:UIImageView pixel-perfect `touches began` for non-transparent parts of UIImageUIImageView 像素完美的“开始触摸”,用于 UIImage 的非透明部分
【发布时间】:2012-02-10 06:24:50
【问题描述】:

开发 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 进行接触

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

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

【问题讨论】:

    标签: iphone uiimageview uiimage touchesbegan user-interaction


    【解决方案1】:
    - (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];
    

    【讨论】:

    • 我也试过了,也试过用 imageview 对象检查 [touch view] 但问题还是一样。
    • 实际上我已经创建了一个具有这个 imageview 和 image 的类 opencloseshutter,所以我尝试不检查和检查 imageview 和 image 但没有帮助..
    • 我不认为这回答了这个问题......提问者似乎关心触摸图像的非透明部分,而这只是触摸整个 UIImageView CGRect。
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多