2017-05-12 73 views
3

我有一个滚动视图,我想滚动视图只滚动向下,它不应该滚动任何其他方向。这不是关于水平或垂直,而是我希望scrollview仅在垂直模式下向下滚动而不向上滚动。UIScrollView只能向下滚动

+0

需要设置scrollView.contentSize比其帧大小为滚动。如果您想要水平滚动,请将contentSize的宽度设置为大于框架宽度。 – nynohu

+0

http://stackoverflow.com/questions/5370428/uiscrollview-disable-scrolling-in-just-one-direction可能的副本 – user3581248

回答

0

子类的UIScrollView并重写来限制水平滚动且仅滚动如果方向是向下的方法:

class DownwardsOnlyScrollView: UIScrollView 
{ 
    override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) { 
     // restrict movement to vertical only 
     let newOffset = CGPoint(x: 0, y: contentOffset.y) 

     //only scroll if scroll direction is downwards 
     if newOffset.y > self.contentOffset.y 
     { 
      super.setContentOffset(newOffset, animated: animated) 
     } 
    } 
} 
+1

为什么你只想向下滚动而不让用户回滚?这是一个非常糟糕的用户界面决策,苹果指导方针声明:“用户应该始终处于控制之中而不是应用程序”。通过不允许他们向上滚动,您将远离用户的控制权。 – torinpitchers

+0

用户将能够通过使用按钮而不是通过滚动来向上滚动。 –

+0

我已经更新了我的答案,这将做你需要的东西 – torinpitchers