【问题标题】:ios add panGesture to a view, change view frame, but not fluencyios在视图中添加panGesture,更改视图框架,但不流畅
【发布时间】:2017-01-05 07:25:16
【问题描述】:

我给self.view加了一个view,在view上加了一个UIPangestureRecognizer,移动view,但是不流畅,感觉不流畅, 这是我的代码,谢谢你帮助我

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)
    }
}

【问题讨论】:

  • 为什么每次都重新分配视图的框架?只有y 值,发生了什么变化。

标签: ios swift3 move gesture uipangesturerecognizer


【解决方案1】:

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

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

【讨论】:

  • sorry,这个不是词,感觉不够流畅,UIView.animate(withDuration: 0.05, animations: { let redY:CGFloat = self.redView.frame.origin.y + point.y self .redView.frame = CGRect.init(x: 0, y: Int(redY), width: KWidth, height: 40) })
  • @tianpengli 你需要在动画块内写 layoutIfNeeded 而不是整个逻辑,像这样:let redY:CGFloat = self.redView.frame.origin.y + point.y self.redView.frame = CGRect.init(x: 0, y: Int(redY), width: KWidth, height: 40)UIView.animate(withDuration: 0.05, animations: { UIView.animate(withDuration: 0.05, animations: { self.view.layoutIfNeeded()})
【解决方案2】:

可能此代码对您有所帮助。我正在使用平移手势更改视图 (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.
    }
}

【讨论】:

    【解决方案3】:

    我不建议每次状态为.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 的操作,它应该会提供更流畅的体验。

    【讨论】:

    • 首先,谢谢你的帮助,但是我能感觉到红色视图滑动不流畅,我希望红色视图滑动流畅
    • 您可以将动画持续时间增加到 0.2,这将使 xp 更平滑,但您会看到移动略有延迟。
    • 我觉得没有时间可以修复这个bug,我不知道怎么办,非常怀疑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    相关资源
    最近更新 更多