-1
我想使用CATransition製作一個動畫UILabel,當我觸摸屏幕時,它可以淡出原始文本並淡入新文本。這是我的代碼,我不知道爲什麼沒有動畫。請幫幫我。我使用Xcode 7.3。CATransition不能正常工作的動畫UILabel
var subtitle:UILabel!
var winsize:CGSize!
override func viewDidLoad() {
super.viewDidLoad()
let animation = CATransition()
animation.type = kCATransitionFade
animation.duration = 0.75
animation.fillMode = kCAFillModeBoth
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animation.delegate = self
self.subtitle = UILabel()
self.subtitle.text = "f"
self.subtitle.frame = CGRectMake(45, 30, 200, 50)
self.subtitle.font = UIFont.systemFontOfSize(25)
self.subtitle.textColor = UIColor.grayColor()
self.view.addSubview(self.subtitle)
self.subtitle.layer.addAnimation(animation, forKey: "animation")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.subtitle.text="HighSchool"
}
與@馬特的幫助,下面的代碼工作。
var subtitle:UILabel!
var winsize:CGSize!
override func viewDidLoad() {
super.viewDidLoad()
winsize = self.view.frame.size
self.subtitle = UILabel()
self.subtitle.text = "f"
self.subtitle.frame = CGRectMake(45, 30, 200, 50)
self.subtitle.font = UIFont.systemFontOfSize(25)
self.subtitle.textColor = UIColor.grayColor()
self.view.addSubview(self.subtitle)
UIView.transitionWithView(self.subtitle, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: nil, completion: nil)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.subtitle.text="HighSchool"
}
您的幫助,先謝謝。我想要做的是製作一個UILabel,當我改變它的文本時,原始文本淡出並且新文本淡入,如http://stackoverflow.com/questions/3073520/animate-text-change中所示。 -in-uilabel – user2232335
那麼,你的代碼不會做那樣的事情。你只是說'self.subtitle.text =「HighSchool」'。這裏沒有淡入淡出的動畫。它不會掉出天空;你必須明確地寫入動畫。調用'transitionWithView:duration:options:animations:completion:'並更改文本。這是三行代碼,非常簡單。 – matt
它的工作原理!非常感謝! – user2232335