ios - How to animate to back UI element into original position in Swift? -


import uikit  class aboutview: uiview {      @iboutlet var aboutview: uiview!     @iboutlet weak var studiotxt: uilabel!     @iboutlet weak var closebtn: uibutton!      var studiotxtoriginy: cgfloat = 0.0      override init(frame: cgrect) {         super.init(frame: frame)         initializeviews()     }      required init?(coder adecoder: nscoder) {         super.init(coder: adecoder)         initializeviews()     }      private func initializeviews() {         bundle.main.loadnibnamed("aboutview", owner: self)         addsubview(aboutview)          studiotxtoriginy = self.studiotxt.frame.origin.y          self.studiotxt.frame.origin.y = studiotxtoriginy + 27     }      @ibaction func onclose(_ sender: any) {         close()     }      func close() {         uiview.animate(withduration: 0.5, delay: 0, usingspringwithdamping: 0.7, initialspringvelocity: 0.0, options: .curveeaseinout, animations: {             self.studiotxt.frame.origin.y = self.studiotxtoriginy             }, completion: { _ in             //         })     } } 

when touch btn, label 100 pixels higher original position, used autoresizing equals height , weight constraint label

this happens because on initialization layout not performed yet. better if find different approach want, "dirty" solution can use layoutsubviews method:

override func layoutsubviews() {     super.layoutsubviews()     if studiotxtoriginy == 0 { // avoid setting value more 1 time         studiotxtoriginy = self.studiotxt.frame.origin.y     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -