开发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覆盖的其他区域?
答案 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];