2014-03-18 57 views
1

我想禁用UICollectionViewController的自动旋转,只要屏幕上有手指,就像iPhone照片应用那样。如何检测UICollectionView的触摸?

如何做到这一点?

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

回答

6

我将设置一个标志在touchesBegantouchesEnded清除它。然后在您的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); 
} 
+0

好,它的工作原理。我应该覆盖touchesBegan:在UICollectionView中。为什么我不能只覆盖touchesBegan:在UICollectionViewController中,这也是一个UIResponder? – smilingpoplar

+0

touchesBegan:也是一个UIViewController方法。 – smilingpoplar

+0

这个解释http://stackoverflow.com/a/1654818/1263403是有道理的。 – smilingpoplar