2017-06-19 3 views
1

Mein Code unten bewegt die Beschriftung nach Norden. Ich möchte eine andere Schaltfläche erstellen, die die Beschriftung zurücksetzt, bevor die Animation ausgelöst wurde. Ich weiß, dass ich die Animation einfach in umgekehrter Richtung nachbauen könnte, aber das scheint ineffizient zu sein.Setzen Sie die Etikettenposition zurück, bevor die Animation angewendet wird (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 
} 

Antwort

0

Probieren Sie dies aus:

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

Hoffnung, das hilft. Glückliche Kodierung.

Verwandte Themen