Touchesbegan not detecting what is being touched

前端 未结 2 2015
情歌与酒
情歌与酒 2021-01-14 22:09

I\'m building a rotating banner using a NSTimer to keep track of the current image with the image being animated from 5 different images. I have a touchesBegan set up to kee

2条回答
  •  庸人自扰
    2021-01-14 22:37

    First of all you have to set userInteractionEnabled to YES in your viewDidLoad method like below:

    [myImageView setUserInteractionEnabled:YES];
    

    Note that for the myImageView, checking User Interaction Enabled via Identity Inspector didn't work for me.

    Then change

    UITouch *touch = [[event allTouches] anyObject];
    

    to

    UITouch *touch = [touches anyObject];
    

    so that the touchesBegan method looks like below:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        if ([touch view] == myImageView) {
           // place any code here when myImageView is touched
        }
     }
    

提交回复
热议问题