UiviewPropertyanimator动画运动取决于锅方向



嗨,我试图使我的动画转到两边,具体取决于用户的态度,但它行不通,但我无法弄清楚怎么了。当我只使用一个方向工作时,手势正常工作,但是当我将逻辑移至movelabel()函数时,它不再工作了。动画甚至没有开始。我的目标是拥有相同的动画(因为我会在其中添加更多细节),但根据锅方向,该特定标签向左或右移动。到目前为止,这是我的感冒

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var labelDummy: UILabel!
    var labelAnimation = UIViewPropertyAnimator()
    override func viewDidLoad() {
        super.viewDidLoad()
        labelDummy.center.x = view.bounds.maxX/2
        view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.moveLabel)))
    }

    func moveLabel(gesture: UIPanGestureRecognizer){
        let trans = gesture.translation(in: view)
        if trans.x >= 0{
            labelAnimation = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
                let yPos = self.labelDummy.center.y
                self.labelDummy.center = CGPoint(x: 100 + (self.labelDummy.frame.size.width/2), y: yPos)
            }
        }
        if trans.x < 0 {
            labelAnimation = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
                let yPos = self.labelDummy.center.y
                self.labelDummy.center = CGPoint(x: 10 + (self.labelDummy.frame.size.width/2), y: yPos)
            }
        }
        labelAnimation.fractionComplete = abs((trans.x/100))

        if gesture.state == .ended{
            labelAnimation.fractionComplete = 0
        }
        print("fractionCompleted: ", labelAnimation.fractionComplete)
    }
}

我该如何解决这个问题?

有效的代码:

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelDummy: UILabel!
var labelAnimation = UIViewPropertyAnimator()
override func viewDidLoad() {
    super.viewDidLoad()

    labelDummy.center.x = view.bounds.maxX/2
    // Pan for hele viewen som spiller label animasjon
    view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.moveLabel)))
    // animasjon
    labelAnimation = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
        let yPos = self.labelDummy.center.y
        self.labelDummy.center = CGPoint(x: 10 + (self.labelDummy.frame.size.width/2), y: yPos)
    }
    //labelAnimation.startAnimation()
}
func moveLabel(gesture: UIPanGestureRecognizer){
    print("retning: ", gesture.velocity(in: view).x)
    let trans = gesture.translation(in: view)
    print("trans: ", trans)
    labelAnimation.fractionComplete = trans.x/100
    print("fractionCompletet prøver å settes til : ", trans.x/100)
    print(trans.x)
    if gesture.state == .ended{
        print("-ENDED-")
        labelAnimation.fractionComplete = 0
    }
    print("fractionCompletet: ", labelAnimation.fractionComplete)
}

}

最终为x位置制作一个全局变量,在movelabel函数中,我使用的是if语句诸如" if .began"之类的内容,然后检查转换。根据翻译是否高于或低于0,将xposition设置在左侧的某个地方,或者右侧的某个地方。

if gesture.state == .began {
        if gesture.velocity(in: view).x > 0{
            // Panning right, Animate Left
            self.animationDirection = .left
            self.animateToXPos = CGPoint(x: headerLabelPositionLeft!, y: headerLabelPositionY!)
            self.setAnimation(direction: AnimationDirection.left)
            self.dayLabel.textAlignment = .left
        } else {
            // Panning left, Animate Right
            self.animationDirection = AnimationDirection.right
            self.animateToXPos = CGPoint(x: self.view.bounds.width - (self.dayLabel.frame.size.width/2), y: headerLabelPositionY)
            self.setAnimation(direction: AnimationDirection.right)
            self.dayLabel.textAlignment = .right
        }
    }

最新更新