我的应用程序中有一个带有图像的UIButton。
我希望能够调用一个闪烁该按钮多次的功能(比如说5次)
我也希望能够在运行时停止闪烁
以下是代码的重要部分:
var flashesRemaining: Int = 5;
var isFlashing: Bool = false
@IBOutlet weak var btnMM: UIButton!
// Flash the button
@IBAction func flashMemoryButton(sender: AnyObject) {
print(self.flashesRemaining)
if self.flashesRemaining == 0 {
return
}
if !isFlashing {
self.btnMM.alpha = 1.0
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.curveEaseInOut, .repeat, .autoreverse, .allowUserInteraction], animations: {() -> Void in
self.btnMM.alpha = 0.0100000003
}, completion: {(finished: Bool) -> Void in })
isFlashing = true
}
else {
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseInOut, .beginFromCurrentState], animations: {() -> Void in
self.btnMM.alpha = 1.0
}, completion: {(finished: Bool) -> Void in })
}
self.flashesRemaining = self.flashesRemaining - 1
}
所以我希望能够像这样调用上面的函数,开始闪烁:
self.flashesRemaining = 5
self.flashMemoryButton(sender: self.btnMM)
然后如果我想禁用闪烁,我应该调用:
self.flashesRemaining = 0
但是上面的代码根本不起作用。一旦我打电话
self.flashMemoryButton(sender: self.btnMM)
我打印输出 5 ,然后按钮一直闪烁。 我在这里缺少什么?
顺便说一句,神奇的数字0.0100 ... 3是UIButton允许的最小alpha值,同时保持用户的交互性。
答案 0 :(得分:1)
这是工作代码:
func flashMemoryButton() {
print(self.flashesRemaining)
if self.flashesRemaining == 0 {
btnMM.alpha = 1
return
}
if !isFlashing {
self.btnMM.alpha = 1.0
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.curveEaseInOut, .allowUserInteraction], animations: {() -> Void in
self.btnMM.alpha = 0.0100000003
}, completion: {(finished: Bool) -> Void in
if finished {
self.flashMemoryButton()
self.flashesRemaining = self.flashesRemaining - 1
}
})
isFlashing = true
}
else {
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseInOut, .beginFromCurrentState], animations: {() -> Void in
self.btnMM.alpha = 1.0
}, completion: {(finished: Bool) -> Void in
if finished {
self.flashMemoryButton()
}
})
isFlashing = false
}
}
以下是我所做的更改:
首先,我删除了.repeat
和.autoreverse
。这是因为我们希望多次调用flashMemoryButton
方法而不是重复动画。
接下来,我在isFlashing = false
分支中添加了else
。这样,当isFlashing
为真时,按钮会在下次调用方法时淡出,而当isFlashing
为假时,按钮会淡出。您可以将isFlashing
视为切换。
当淡入/淡出动画完成时,我调用flashMemoryButton()
,因为当动画结束时,你想要做另一个动画,对吗?
在淡出动画的完成处理程序中,我减少了flashesRemaining
。这是您想要减少它的地方,而不是在if语句之外。如果你把它放在外面,它会闪现你告诉它的一半时间。
答案 1 :(得分:1)
您可以使用UIView.setAnimationRepeatCount
;因此你不再需要flashesRemaining
了。您还应该在完成处理程序中重新设置isFlashing
- 标志。这是我修改过的代码:
var isFlashing: Bool = false
@IBOutlet weak var btnMM: UIButton!
@IBAction func flashMemoryButton(sender: AnyObject) {
if !isFlashing {
self.btnMM.alpha = 1.0
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.curveEaseInOut, .repeat, .autoreverse, .allowUserInteraction], animations: {() -> Void in
UIView.setAnimationRepeatCount(5)
self.btnMM.alpha = 0.0100000003
}, completion: {(finished: Bool) -> Void in
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseInOut, .beginFromCurrentState], animations: {() -> Void in
self.btnMM.alpha = 1.0
}, completion: {(finished: Bool) -> Void in self.isFlashing = false})
})
isFlashing = true
}
else {
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseInOut, .beginFromCurrentState], animations: {() -> Void in
self.btnMM.alpha = 1.0
}, completion: {(finished: Bool) -> Void in self.isFlashing = false})
}
}