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

时间:2012-01-14 07:52:14

标签: iphone uiimageview uiimage touchesbegan user-interaction

开发iphone app,我使用了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覆盖的其他区域?

1 个答案:

答案 0 :(得分:0)

- (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];