How to detect touch on UICollectionView?

大憨熊 提交于 2019-12-11 09:26:49

问题


I want to disable UICollectionViewController's autorotation whenever there's a finger on the screen, as the iPhone photo app does.

How to do that?

  • If use tap gesture, how to distinguish different touch states? (The state should be touching, even after finger moving.)
  • If use touchBegan:withEvent:, where to put that code? (The hit view can be any subview of UICollectionView.)

回答1:


I would set a flag in touchesBegan and clear it in touchesEnded. Then in your shouldAutoRotate method you can check the flag and return false if the flag is set.

Something like this:

// In your UICollectionView subclass:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Do stuff
    ...
    canRotate = NO;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Do stuff
    ...
    canRotate = YES;
}

// In your UICollectionViewController:

-(bool)shouldAutorotate
{
    return(canRotate);
}


来源:https://stackoverflow.com/questions/22487121/how-to-detect-touch-on-uicollectionview

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