我如何在 swift 中向上滑动时减小比例

how can i decrease scale as i am swipping upward in swift

我想在向上滑动时减小 uiview 的比例,我想要关闭应用程序时使用与 ios 12 或 13 中相同的动画。

class ViewController: UIViewController {
    @IBOutlet weak var myView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
       let panGesture = UIPanGestureRecognizer(target: self, action: #selector(didPanningFromBottom))
        
        myView.addGestureRecognizer(panGesture)
        panGesture.delegate = self
    }
    @objc func didPanningFromBottom(recognizer: UIPanGestureRecognizer){
        if recognizer.state == .began {
            print("began")
        }
        else if recognizer.state == .changed {
            UIView.animate(withDuration: 0.1, delay: 0.25, options: .curveEaseOut) {
                self.myView.transform = CGAffineTransform(scaleX: 0.25, y: 0.25 )
            }

        }
    }
}

你可以这样做。

假设您的其余代码完好无损,只需交换 didPanningFromBottom

基本上您需要做两件事。首先跟踪拖动操作,然后用手指沿着视图移动,如果需要,可以稍微缩放它。其次,在发布时,确定是否应该将视图动画化或让它反弹。在我的代码中,我检查了释放点的速度和距离。您可能想要修改确切的值。

您可能还想检查 .cancel 大小写以始终移回起始位置。

@objc func didPanningFromBottom(recognizer: UIPanGestureRecognizer){
    let yPosition = recognizer.translation(in: view).y
    let yVelocity = recognizer.velocity(in: view).y

    if recognizer.state == .began {

    } else if recognizer.state == .changed {

        // Scale down a small amount
        let scale: CGFloat = 1.0 - abs(yPosition / 2000)
        
        myView.transform = CGAffineTransform.identity
            .translatedBy(x: 0, y: yPosition)
            .scaledBy(x: scale, y: scale)

    } else if recognizer.state == .ended {

        // If either have a minimum velocity or release beyond a fixed point
        if yVelocity < -200 || (yVelocity <= 0 && yPosition < 400) {

            UIView.animate(withDuration: 0.25) {
                self.myView.transform = CGAffineTransform.identity.translatedBy(x: 0, y: -self.view.bounds.size.height / 2 - self.myView.bounds.size.height / 2)
                self.myView.alpha = 0
            }
        } else {
            // Move it back to start
            UIView.animate(withDuration: 0.25) {
                self.myView.transform = CGAffineTransform.identity
            }
        }
    }
}