如何检测 UICollectionView 上的触摸? [英] How to detect touch on UICollectionView?
本文介绍了如何检测 UICollectionView 上的触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在屏幕上有手指时禁用 UICollectionViewController 的自动旋转,就像 iPhone 照片应用一样.
I want to disable UICollectionViewController's autorotation whenever there's a finger on the screen, as the iPhone photo app does.
怎么做?
- 如果使用点击手势,如何区分不同的触摸状态?(状态应该是
touching
,即使在手指移动之后.) - 如果使用
touchBegan:withEvent:
,代码放在哪里?(点击视图可以是 UICollectionView 的任何子视图.)
- 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.)
推荐答案
我会在 touchesBegan
中设置一个标志并在 touchesEnded
中清除它.然后在您的 shouldAutoRotate
方法中,您可以检查标志并在设置标志时返回 false.
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.
像这样:
// 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);
}
这篇关于如何检测 UICollectionView 上的触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文