iOS 使用UIPanGestureRecognizer实现UIView的拖动

使用UIPanGestureRecognizer实现UIView的拖动的时候,需要先给UIView添加手势对象

let gesture = UIPanGestureRecognizer(target: self, action: #selector(move))
        gesture.maximumNumberOfTouches = 1
        gesture.minimumNumberOfTouches = 1
        self.mButton.addGestureRecognizer(gesture)

然后实现手势对象的回调

    @objc func move(sender:UIPanGestureRecognizer) {
        if sender.state == .changed || sender.state == .ended {
            let offset = sender.translation(in: self.mButton)
            self.mShrinkView.center = CGPoint(x: self.mButton.center.x + offset.x, y: self.mButton.center.y + offset.y)
            sender.setTranslation(CGPoint.zero, in: self.mButton) //初始化sender的转换坐标,否则会一直积累
        }
    }

因为我用的是UIButton,通过亲自测试UIButton的单击事件不受影响,也就是拖动和单击同时可用不会起冲突。

说点什么

avatar
  Subscribe  
提醒