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
Post a Comment