与平移手势闪动视图相关的问题

问题描述 投票:0回答:1

我正在尝试使用平移手势将底部视图向上向下移动。它的工作,但有一个闪烁的问题,因此它不是一个平滑的过渡。

以下是代码

@IBOutlet weak var bottomViewHeight: NSLayoutConstraint!
@IBOutlet weak var bottomView: UIView!    
var maxHeight: CGFloat = 297
var minHeight: CGFloat = 128    

let panGest = PanVerticalScrolling(target: self, action: #selector(panAction(sender:)))
bottomView.addGestureRecognizer(panGest)    

@objc func panAction(sender: UIPanGestureRecognizer) {
    if sender.state == .changed {
        let endPosition = sender.location(in: self.bottomView)

        let differenceHeight = maxHeight - endPosition.y

        if differenceHeight < maxHeight && differenceHeight > minHeight {
            bottomViewHeight.constant = differenceHeight
            self.bottomView.layoutIfNeeded()
        }
    }
}

这里是手势类

class PanVerticalScrolling : UIPanGestureRecognizer {
    override init(target: Any?, action: Selector?) {
        super.init(target: target, action: action)
    }    

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesMoved(touches, with: event)
           if state == .began {
           let vel = velocity(in: self.view)
           if abs(vel.x) > abs(vel.y) {
                state = .cancelled
           }
        }
    }
 }

在这张图中,你可以检查实际问题。

enter image description here

ios swift uipangesturerecognizer
1个回答
1
投票

问题是,你反复获得触摸位置 在下图中 .然后改变底部视图大小。

由于触摸Y是相对于视图的高度而言的,所以它在周围跳动。

试试这个...

添加一个类属性。

var initialY: CGFloat = -1.0

然后改变你的 panAction() 到这个。

@objc func panAction(sender: UIPanGestureRecognizer) {
    if sender.state == .changed {

        if initialY == -1.0 {
            initialY = sender.location(in: self.view).y
        }

        let endPositionY = sender.location(in: self.view).y - initialY

        let differenceHeight = maxHeight - endPositionY

        if differenceHeight < maxHeight && differenceHeight > minHeight {
            bottomViewHeight.constant = differenceHeight
            self.bottomView.layoutIfNeeded()
        }
    }
}

你需要重新设置 initialY-1 每次你 "结束 "拖动调整大小的过程。


编辑

更好的方式--保持当前状态。

// these will be used inside panAction()
var initialY: CGFloat = -1.0
var initialHeight: CGFloat = -1.0

@objc func panAction(sender: UIPanGestureRecognizer) {
    if sender.state == .changed {
        let endPositionY = sender.location(in: self.view).y - initialY

        let differenceHeight = self.initialHeight - endPositionY

        if differenceHeight < maxHeight && differenceHeight > minHeight {
            bottomViewHeight.constant = differenceHeight
        }
    }
    if sender.state == .began {
        // reset position and height tracking properties
        self.initialY = sender.location(in: self.view).y
        self.initialHeight = bottomViewHeight.constant
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.