2017-06-19 25 views
1

我的代码如下将标签向北移动。我想创建另一个按钮,在动画被触发前重置标签。我知道我可以重新创建相反的动画,但效率不高。重置动画应用之前的标签位置(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 
} 

回答

0

尝试了这一点:

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

希望这有助于。快乐编码。