-(void)touchesBegan (TO A SPECIFIED UIIMAGEVIEW)

微笑、不失礼 提交于 2019-12-12 05:48:52

问题


Hi I want to make it so that if you touch an image that I put on the view the -(void)checkcollision happens. The -(void)checkcollision happens, but when I touch anything. How can I say it only works if i touch a specified image. e.g. a pimple:

IBOutlet UIImageView *pimple;
@property (nonatomic, retain) UIImageView *pimple;

here is the touchesBegan code

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    UITouch *myTouch = [[event allTouches] anyObject];
    [self checkcollision];
}


-(void)checkcollision {

    if (label.text = @"0") {
        label.text = @"1";
    }

    pimple.hidden = YES;
}

回答1:


CGPoint point = [myTouch locationInView:pimple];
if ( CGRectContainsPoint(pimple.bounds, point) ) {
    ... Touch detected.
}

Alternatively you can consider gesture recognizers. You can use a tap recognizer for this case.




回答2:


There are two options:

  1. Subclass UIImageView and define your own handler
  2. Check sender - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event and compare it to your UIImageView instance.


来源:https://stackoverflow.com/questions/6160741/voidtouchesbegan-to-a-specified-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!