2

开发 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 覆盖的其余区域上获得事件?

4

1 回答 1

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];
于 2012-01-14T08:08:28.343 回答