【问题标题】:UICollectionView is unintentionally scrolling on button pressUICollectionView 在按钮按下时无意滚动
【发布时间】:2014-09-19 21:19:09
【问题描述】:

我在UICollectionViewCell 中添加了一个UIButton,其中一个触摸事件调用了一个方法,该方法调出了一个UIPopoverController,与集合视图完全无关。

问题是当我按下这个按钮时,我的UICollectionView 会稍微向下滚动。我似乎无法弄清楚为什么。滚动动作是在我的按钮被实际按下之前调用的——所以我试图在调用的方法中阻止这种形式是徒劳的。

有没有人遇到过这个问题,或者有任何关于如何防止它发生的指示?

我已尝试在触摸时禁用集合视图上的滚动,但似乎触摸也触发了滚动 - 在滚动禁用之前。

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:

    要解决这个问题,你应该override func touchesShouldCancel

    首先,您创建新类继承UICollectionViewUITableViewUIScrollView

    有问题,您想在UICollectionView 中取消。 所以你让类继承UICollectionCiew

    其次,在func init 中设置属性

    self.delaysContentTouches = false
    self.canCancelContentTouches = true
    

    最后,你覆盖func touchesShouldCancel可以取消触摸视图种类 有问题,你想取消UIButtontouch。

    你在代码下面创建继承类

    import UIKit
    
    class TouchCancelCollectionView: UICollectionView {
    
        override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
            super.init(frame: frame, collectionViewLayout: layout)
            self.delaysContentTouches = false
            self.canCancelContentTouches = true
        }
    
        required init(coder aDecoder: NSCoder) {
            fatalError()
        }
    
        override func touchesShouldCancel(in view: UIView) -> Bool {
    
            if view.isKind(of: UIButton.self) {
                return true
            }
    
            return super.touchesShouldCancel(in: view)
        }
    }
    
    

    并使用它!

    【讨论】:

      【解决方案2】:

      如果我的问题没看错,我认为这行代码可以帮助你。

      yourCollectionView.delaysContentTouches = NO;
      

      问题是当有一个collectionView上的触摸时,系统不知道你是在尝试点击按钮还是滚动视图,所以Apple给了滚动更高的优先级。希望这个答案是正确的。

      很高兴回答一年前提出的问题:)

      【讨论】:

      • 一年后这个问题对我来说仍然没有解决。我很高兴看到你的帖子,但不幸的是它并没有解决我的问题。当我触摸 UIButton 时,集合视图中仍有一点反弹。
      【解决方案3】:

      我尝试使用来自홍성호 的答案,它几乎是正确的,但您必须返回false 以获得您不想跟踪的视图。 我的实现是针对UIScrollView,但它与UICollectionView 类似。

      final class NewScrollView: UIScrollView {
      override init(frame: CGRect) {
          super.init(frame: frame)
          delaysContentTouches = false
          canCancelContentTouches = true
      }
      
      @available(*, unavailable)
      required init?(coder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
      
      override func touchesShouldCancel(in view: UIView) -> Bool {
          if view is NameOfYourView {
              return false
          }
      
          return super.touchesShouldCancel(in: view)
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-15
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多