我的代码将标签向北移动。我想创建一个另一个按钮,在动画触发之前重置标签。我知道我可以反向重新创建动画,但这看起来效率低下。
@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 :(得分: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
}
希望这会有所帮助。快乐的编码。