ios - Weird behavior when animating UILabel change of color -


i'm animating textcolor of uibutton's label

there's animation when user taps down , when touch inverts backgroundcolor , textcolor.

the problem animation on tap down changes color current color -> white, here animation cut , new color set

but when touch animation works fine new color -> previous color

here's code

override func begintracking(_ touch: uitouch, event: uievent?) -> bool {     let location = touch.location(in: self)     if self.layer.bounds.contains(location) {         fade(tobackgroundcolor: textcolor, textcolor: bgcolor)         return true     }     return false }  override func continuetracking(_ touch: uitouch, event: uievent?) -> bool {     let location = touch.location(in: self)     if !self.layer.bounds.contains(location) {         fade(tobackgroundcolor: bgcolor, textcolor: textcolor)         return false     }     return true }  override func endtracking(_ touch: uitouch?, event: uievent?) {     fade(tobackgroundcolor: bgcolor, textcolor: textcolor) }  func fade(tobackgroundcolor bgcolor: uicolor, textcolor: uicolor) {     backgroundcolor = .clear     let easing = camediatimingfunction(controlpoints: 0.25, 1.0, 0.25, 1)      let bgcoloranimation = cabasicanimation(keypath: "backgroundcolor")     bgcoloranimation.fromvalue = layer.backgroundcolor     bgcoloranimation.tovalue = bgcolor.cgcolor     bgcoloranimation.timingfunction = easing     bgcoloranimation.duration = 0.3      uiview.transition(with: self.titlelabel!, duration: 0.3, options: [.curveeaseinout, .transitioncrossdissolve], animations: {         self.titlelabel?.textcolor = textcolor     }, completion: { _ in         if self.layer.backgroundcolor == bgcolor.cgcolor {             self.titlelabel?.textcolor = textcolor         }else{             self.titlelabel?.textcolor = bgcolor         }     })     layer.add(bgcoloranimation, forkey: nil)     layer.backgroundcolor = bgcolor.cgcolor } 

i tried add catextlayer this

let textlayer = catextlayer() textlayer.frame = self.bounds textlayer.rasterizationscale = uiscreen.main.scale textlayer.contentsscale = uiscreen.main.scale  textlayer.fontsize = (titlelabel?.font.pointsize)! textlayer.font = titlelabel?.font textlayer.foregroundcolor = uicolor.magenta.cgcolor  textlayer.string = currenttitle ?? "" layer.addsublayer(textlayer) 

but text not positioning properly

red text uilabel of button, , magenta text catextlayer


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -