i doing on ios in swift. trying animate uiview updating frame. created custom subclass of uitextfield. code have follows.
import foundation import uikit enum textfieldstate { case active case inactiveblank case inactivetext } /** use class create text field in new workout vc */ class textfield: uitextfield { // mark: properties /// color accent elements private let standardcolor: uicolor = color().blue /// color error elements private let errorcolor: uicolor = color().red /// font inactive state private let inactivefont: uifont = fonts.regular().sixteen /// font active state private let activefont: uifont = fonts.bold().thirtysix /// font hint active state private let activehintfont: uifont = fonts.regular().sixteen /// font hint in inactive state private let inactivehintfont: uifont = fonts.regular().twelve /// placeholder , hint text field let placeholdertext: string /// type of field let type: textfieldtype /// bottom line text field var bottomline: uiview { return self.createbottomline() } /// placeholder field var placeholderlabel: uilabel { return self.createplaceholderlabel() } /// rect editing , text bounds private var textbounds: rect { return rect(x: 0, y: 31, w: self.frame.w, h: 96) } /// variable text of text field // mark: initializers /// default initalizer init<a: textfieldoptions>(frame: cgrect, options: a) { // set placeholder text value self.placeholdertext = options.placeholdertext // set type of text field self.type = options.type // call super initalizer super.init(frame: frame) // update settings based on type self.styletype() // set delgate text field self.delegate = self // set background color self.backgroundcolor = color().white // set font text field self.font = self.activefont // layout views self.layoutviews() } /// required apple never use required init?(coder adecoder: nscoder) { fatalerror("this class doesn't support nscoding") } // mark: functions private func layoutviews() { // add placeholder text text field self.addsubview(self.placeholderlabel) // add bottom line text field self.addsubview(self.bottomline) } private func styletype() { if self.type == .all { self.returnkeytype = .next self.autocorrectiontype = .no } } private func createbottomline() -> uiview { // y position line let y: cgfloat = self.placeholderlabel.frame.origin.y + self.placeholderlabel.frame.height + 4 // create frame bottom line let bottomlineframe: rect = rect(x: 0, y: y , w: self.frame.w, h: 1) // create bottom line let botttomline: line = line(frame: bottomlineframe, alpha: 0.38) // return bottom line return botttomline } private func createplaceholderlabel() -> uilabel { // create frame placeholder text let placeholderframe: cgrect = cgrect(x: 0, y: 20, width: self.frame.width, height: 19) // create placeholder text let placeholder: uilabel = uilabel(frame: placeholderframe, font: self.inactivefont, align: .left, color: color().black) // set alpha placeholder placeholder.alpha = 0.38 // set text placeholder placeholder.text = self.placeholdertext // return placeholder return placeholder } /** function updates style text field - parameter state: state text field current */ private func updatestyle(state: textfieldstate) { switch state { case .active: self.activestyle() case .inactiveblank: print("update inactive blank state & style changes") case .inactivetext: print("update inactive text state , style changes") } } /** function when text field in active state , needs style accordingly */ private func activestyle() { // create animation let animate: () -> () = { print("animate!!!") self.placeholderlabel.font = self.activehintfont self.placeholderlabel.frame.origin.y = 4 self.placeholderlabel.textcolor = self.standardcolor self.bottomline.backgroundcolor = self.standardcolor var newframe: cgrect = self.bottomline.frame newframe.origin.y = 86 self.bottomline.frame = newframe } // animate view uiview.animatewithduration(0.3, animations: animate) uiview.animatewithduration(0.3, delay: 0.0, options: .curveeaseinout, animations: { self.bottomline.frame.origin.y = 86 }, completion: nil) } } // mark: methods text placement extension textfield { override func editingrectforbounds(bounds: cgrect) -> cgrect { return textbounds } override func textrectforbounds(bounds: cgrect) -> cgrect { return textbounds } } // mark: delegate methods extension textfield : uitextfielddelegate { /** function handles when text field begins editing */ func textfielddidbeginediting(textfield: uitextfield) { print("i started editing") self.updatestyle(.active) } /** function handles when text field finishes editing */ func textfielddidendediting(textfield: uitextfield) { print("i finished editing") } /** function handles when text field changes */ func textfielddidchange(textfield: textfield) { print("i changed") print("text = \(self.text)") } }
the animation block gets called nothing updates. view on screen , know viewwillapear
has been called. cause type of issue? have tried passing animation onto thread , didn't work.
it's logic setting frame's y position. frame can set view's frame setter.
let newframe: cgrect = self.bottomline.frame; newframe.origin.y = 86; self.bottomline.frame = newframe;
edit bottomline initializer getting called anew before animation. lazy build of view (by assigning tag) approach.
Comments
Post a Comment