【问题标题】:How to detect touch on UICollectionView?如何检测 UICollectionView 上的触摸?
【发布时间】:2014-03-18 17:47:47
【问题描述】:

我想在屏幕上有手指时禁用 UICollectionViewController 的自动旋转,就像 iPhone 照片应用一样。

如何做到这一点?

  • 如果使用点击手势,如何区分不同的触摸状态? (状态应该是touching,即使在手指移动之后。)
  • 如果使用touchBegan:withEvent:,该代码放在哪里? (命中视图可以是 UICollectionView 的任何子视图。)

【问题讨论】:

    标签: ios touch uicollectionview uitapgesturerecognizer


    【解决方案1】:

    我会在touchesBegan 中设置一个标志并在touchesEnded 中清除它。然后在您的shouldAutoRotate 方法中,您可以检查标志,如果设置了标志,则返回 false。

    类似这样的:

    // 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 中覆盖 touchesBegan:。为什么我不能只覆盖 touchesBegan: 在 UICollectionViewController 中,它也是一个 UIResponder?
    • touchesBegan: 也是一个 UIViewController 方法。
    猜你喜欢
    • 2017-05-12
    • 2013-10-19
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    相关资源
    最近更新 更多