在应用动画之前重置标签位置 (swift3)

Reset label position before animation is applied (swift3)

我下面的代码将标签向北移动。我想创建另一个按钮,在动画触发之前重置标签。我知道我可以反向重新创建动画,但这似乎效率不高。

   @IBAction func press(_ sender: Any) {
    UIView.animate(withDuration: 10, animations: {
        self.label.frame.origin.y -= 500
    }, completion: nil )
}
   @IBAction func reset(_ sender: Any) {
  //reset the "label" position
}

试试这个:

 //To store the default value of the position of the label
   var yPosLabel : CGFloat!
override func viewDidLoad() {
        super.viewDidLoad()

        yPosLabel = self.myLabel.frame.origin.y
        // Do any additional setup after loading the view.
    }

 @IBAction func reset(_ sender: Any) {
 //Just in case you are using AutoLayout
 self.view. translatesAutoresizingMaskIntoConstraints = true
 let newFrame = CGRect(x:self.label.frame.origin.x, y:yPosLabel, 
 width:self.label.frame.size.width, height:self.label.frame.size.height)
 self.label.frame = newFrame
 }

希望这对您有所帮助。快乐编码。