Ios 为流上的UIButton设置动画
我的应用程序中有一个带有图像的UIButtonIos 为流上的UIButton设置动画,ios,swift,uibutton,Ios,Swift,Uibutton,我的应用程序中有一个带有图像的UIButton 我想能够调用一个功能,闪烁按钮的次数(说5次) 我也希望能够停止这个闪烁,而它正在运行 以下是代码的重要部分: var flashesRemaining: Int = 5; var isFlashing: Bool = false @IBOutlet weak var btnMM: UIButton! // Flash the button @IBAction func flashMemoryButton(sender: AnyObject
- 我想能够调用一个功能,闪烁按钮的次数(说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值。以下是工作代码:
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
方法,而不是重复动画本身
接下来,我在else
分支中添加了isflash=false
。这是因为当isflash
为true时,按钮在下次调用该方法时淡出,而当isflash
为false时,按钮淡出。你可以把正在闪烁想象成一个开关
当淡入/淡出动画完成时,我调用了flashMemoryButton()
,因为当动画完成时,您想做另一个动画,对吗
在淡出动画的完成处理程序中,我减少了flashsremaining
。这是要减小它的位置,而不是在if语句之外。如果你把它放在外面,它会闪烁你告诉它的一半时间 以下是工作代码:
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
方法,而不是重复动画本身
接下来,我在else
分支中添加了isflash=false
。这是因为当isflash
为true时,按钮在下次调用该方法时淡出,而当isflash
为false时,按钮淡出。你可以把正在闪烁想象成一个开关
当淡入/淡出动画完成时,我调用了flashMemoryButton()
,因为当动画完成时,您想做另一个动画,对吗
在淡出动画的完成处理程序中,我减少了flashsremaining
。这是要减小它的位置,而不是在if语句之外。如果你把它放在外面,它会闪烁你告诉它的一半时间 您可以使用UIView.setAnimationRepeatCount
;因此,您不再需要闪存维护
。您还应该在完成处理程序中重新设置isflash
-标志。这里是我修改过的代码:
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})
}
}
您可以使用UIView.setAnimationRepeatCount
;因此,您不再需要闪存维护
。您还应该在完成处理程序中重新设置isflash
-标志。这里是我修改过的代码:
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})
}
}