ios将panGesture添加到视图中,更改视图框,但不流畅

时间:2017-01-05 07:25:17

标签: ios swift3 move gesture uipangesturerecognizer

enter image description here

我向self.view添加一个视图,并在视图中添加UIPangestureRecognizer,移动视图,但不流畅,我感觉不流畅, 这是我的代码,谢谢你的帮助

var redView:UIView = UIView();
var redFrame:CGRect = CGRect();
override func viewDidLoad() {
    super.viewDidLoad()
    redView = UIView.init(frame: CGRect.init(x: 0, y: 100, width: KWidth, height: 40))
    redView.backgroundColor = UIColor.red
    self.redFrame = redView.frame;
    self.view.addSubview(redView);
    let pan:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(moveFunction(pan:)))
    self.redView.addGestureRecognizer(pan)
}
func moveFunction(pan:UIPanGestureRecognizer) {
    let point:CGPoint = pan.translation(in: self.view)
    pan.setTranslation(CGPoint.zero, in: self.view)
    if  pan.state == .changed{
        print(point)
        let redY:CGFloat = redView.frame.origin.y + point.y
        redView.frame = CGRect.init(x: 0, y: Int(redY), width: KWidth, height: 40)
    }
}

3 个答案:

答案 0 :(得分:0)

在设置帧上使用UIView动画,动画持续时间为0.05。

class func animate(withDuration duration: TimeInterval, animations: @escaping () -> Void)

答案 1 :(得分:0)

我不建议每次状态为.changed时重新分配视图的框架。仅更新手势识别器视图的center属性。

另一方面,如果你考虑现实生活,当一切开始或停止移动时,它们不会使用相同的速度,它们会加速,并在最终停止之前减速。

因此我建议将center定位包含在UIView的动画块中,并使用.curveEaseInOut选项。

private var originalX: CGFloat = 0.0
private var originalY: CGFloat = 0.0

func handlePanGestureRecognizer(_ pan: UIPanGestureRecognizer) {

    guard let panView = pan.view else {
        return
    }

    // Make sure, the gastureRecognizer's view the topmost view
    self.view.bringSubview(toFront: panView) 

    // Find coordinates of the gesture recognizer event on the screen 
    var translatedPoint = pan.translation(in: self.view)

    // The dragging has just started, lets remember the inital positions
    if pan.state == .began {
        originalX = panView.center.x
        originalY = panView.center.y
    }

    // increase the coordinates of the gesturerecognizers view by the dragging movement's y coordinate
    translatedPoint = CGPoint(x: originalX, y: originalY + translatedPoint.y)

    UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseInOut], animations: {
        pan.view?.center = translatedPoint
    } , completion: nil)
} 

将此方法添加为UIPanGestureRecognizer的操作,它应该提供更流畅的体验。

答案 2 :(得分:0)

此代码可能对您有帮助。我正在使用平移手势更改视图(panView)高度限制。

@IBOutlet weak var h: NSLayoutConstraint!
@IBOutlet weak var panView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    let gesture = UIPanGestureRecognizer(target: self, action: #selector(self.pan(_:)))
    panView.addGestureRecognizer(gesture)
}

@objc func pan(_ pan: UIPanGestureRecognizer) {

    let translation = pan.translation(in: panView)

    if pan.state == .began {
    } else if pan.state == .changed {
        h.constant = h.constant - translation.y
        pan.setTranslation(CGPoint.zero, in: panView)
    } else if pan.state == .ended {
        // You can handle this how you want.
    }
}