gpt4 book ai didi

swift - 快速录制按钮的 Flash 动画问题

转载 作者:搜寻专家 更新时间:2023-11-01 06:45:34 25 4
gpt4 key购买 nike

我正在尝试制作一个录制按钮,以便当用户单击它时它开始录制,我想在该按钮上应用 flash 动画,我找到了 THIS邮政。我将该代码转换为 swift 但它不起作用,这是我的 swift 代码:

var buttonFlashing = false
@IBAction func record(sender: AnyObject) {

println("Button Tapped")
if !buttonFlashing {
startFlashingbutton()
} else {
stopFlashingbutton()
}
}

func startFlashingbutton() {

buttonFlashing = true
recordButton.alpha = 1

UIView.animateWithDuration(0.5 , delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction, animations: {

self.recordButton.alpha = 0

}, completion: {Bool in
})
}

func stopFlashingbutton() {

buttonFlashing = false

UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.BeginFromCurrentState, animations: {

self.recordButton.alpha = 1

}, completion: {Bool in
})
}

当我第一次按下按钮时它正在打印 "Button Tapped" 并且动画发生但是当我再次按下按钮时 "Button Tapped" 没有打印到控制台.而且我无法停止动画。

我找不到这里有什么问题。

最佳答案

您的代码的问题是 alpha 设置为零。因此,当 alpha 为零或隐藏时,操作系统会禁用交互。动画应用于按钮层,它已经将 alpha 设置为最终值。因此,您可以将 alpha 值设置为低至 0.1 以启用用户交互,这应该没问题。

您可以执行以下任一选项,

将透明度设置为 0.1

UIView.animateWithDuration(0.5 , delay: 0.0, options: 
[
UIViewAnimationOptions.CurveEaseInOut,
UIViewAnimationOptions.Autoreverse,
UIViewAnimationOptions.Repeat,
UIViewAnimationOptions.AllowUserInteraction
],
animations: {
self.recordButton.alpha = 0.1
}, completion: {Bool in
})

或者,然后将图层的颜色设置为 clearColor,这在您的情况下似乎更明智。

UIView.animateWithDuration(0.5 , delay: 0.0, options: 
[
UIViewAnimationOptions.CurveEaseInOut,
UIViewAnimationOptions.Autoreverse,
UIViewAnimationOptions.Repeat,
UIViewAnimationOptions.AllowUserInteraction
],
animations: {
self.recordButton.layer.backgroundColor = UIColor.clearColor().CGColor
}, completion: {Bool in
})

关于swift - 快速录制按钮的 Flash 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com