【问题标题】:iOS: How to Enable UIScrollView Panning on Long PressiOS:如何在长按时启用 UIScrollView 平移
【发布时间】:2016-07-23 16:24:55
【问题描述】:

我可以使用scrollView.scrollEnabled 属性有效地启用/禁用滚动。

目前我在longPressRecognizer 的状态为UIGestureStateBegan 时启用滚动,并在UIGestureStateEnded 时禁用滚动。

当我长按然后拖动手指时,scrollView 不会滚动。

我认为 scrollView 的 panGestureRecognizer 不知何故没有收到触摸事件,这很奇怪,因为 scrollView(我使用 touchesBegan、touchesMoved 等方法进行了检查)

【问题讨论】:

    标签: ios objective-c iphone uiscrollview


    【解决方案1】:

    UIScrollView 没有被触摸,因为触摸是在您启用 UIScrollView 上的滚动之前开始的。此示例代码使用 UILongPressGestureRecognizer 为您管理滚动,滚动一个大 UIImageView:

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet var gr: UILongPressGestureRecognizer!
    var initialPos = CGPointZero
    var dragPos = CGPointZero
    
    // This action is bound to long press gesture recognizer within Interface Builder
    @IBAction func longpressHandler(sender: UILongPressGestureRecognizer) {
        if sender.state == .Began {
            // Save initial position for moving
            initialPos = sender.locationInView(scrollView)
        }
        else if sender.state == .Changed {
            // Get new drag location
            let loc = sender.locationInView(scrollView)
            let newPos = CGPoint(x: initialPos.x - loc.x + dragPos.x, y: initialPos.y - loc.y + dragPos.y)
            dragPos = newPos
            scrollView.contentOffset = newPos
        }
        else if sender.state == .Ended {
            // Check for end position of the content, snap it back to the edge of the screen if moved too far
            var newOffset = scrollView.contentOffset
            var move = false
            if scrollView.contentOffset.x < 0 {
                newOffset.x = 0
                move = true
            }
            if scrollView.contentOffset.y < 0 {
                newOffset.y = 0
                move = true
            }
            if scrollView.contentOffset.x > photo.frame.size.width - scrollView.frame.size.width {
                newOffset.x = max(self.photo.frame.size.width - self.scrollView.frame.size.width, 0)
                move = true
            }
            if scrollView.contentOffset.y > photo.frame.size.height - scrollView.frame.size.height {
                newOffset.y = max(self.photo.frame.size.height - self.scrollView.frame.size.height, 0)
                move = true
            }
            if move {
                UIView.animateWithDuration(0.25, animations: {
                    self.scrollView.contentOffset = newOffset
                })
            }
        }
    }
    
    var photo = UIImageView(image: UIImage(named: "photo"))
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Set up your scrollview
        scrollView.addSubview(photo)
        scrollView.contentSize = photo.frame.size
    
        // add the long press gesture recognizer to the scrollview
        scrollView.addGestureRecognizer(gr)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2016-05-19
      相关资源
      最近更新 更多