我正在尝试制作一个图片显示1秒,然后再次淡出到原始图片。这是我的尝试;
@IBOutlet var button1: UIButton!
var button1flashing = false
override func viewDidLoad() {
super.viewDidLoad()
if !button1flashing {
flashbutton1()
} else {
stopflashingbutton1()
}
}
func flashbutton1() {
button1flashing = true
UIButton.animateWithDuration(1.0, animations: {
self.button1.setImage(UIImage(named: "code1"), forState: .Normal)
}, completion: {Bool in
})
}
func stopflashingbutton1() {
UIButton.animateWithDuration(1.0, animations: {
self.button1.setImage(UIImage(named: "code2"), forState: .Normal)
}, completion: nil)
}问题:该按钮在视图加载后立即设置为图像"code1“(没有动画),即使它在故事板中设置为"code2”。此外,它不会淡出到图像"code2“(不执行func stopflashingbutton1)
发布于 2016-02-20 23:23:06
使用此函数(您需要显示和隐藏按钮):
func flashButton(button: UIButton, imageName: String, completion: (finished: Bool) -> Void) {
UIView.animateWithDuration(1.0, animations: {
button.alpha = 0
}, completion: {finished in
button.setImage(UIImage(named: imageName), forState: .Normal)
UIButton.animateWithDuration(1.0, animations: {
button.alpha = 1
}, completion: {finished in
completion(finished: finished)
})
})
}
func flashButton(button: UIButton, imageName: String) {
UIView.animateWithDuration(1.0, animations: {
button.alpha = 0
}, completion: {finished in
button.setImage(UIImage(named: imageName), forState: .Normal)
UIButton.animateWithDuration(1.0, animations: {
button.alpha = 1
})
})
}在你的viewDidLoad中
override func viewDidLoad() {
super.viewDidLoad()
flashButton(button1, imageName: "code1") { (finished) -> Void in
self.flashButton(self.button1, imageName: "code2")
}
}
}发布于 2016-03-10 02:52:24
将alpha设置为0.1,然后设置为1.0,两者之间的间隔为秒。这会产生动画效果。
https://stackoverflow.com/questions/35523992
复制相似问题