Swift 中的 UIAnimation 重复问题 [英] UIAnimation repeating issue in Swift
问题描述
我的项目中有一个简单的 UIButton,在按钮内运行动画块.基本上,我有一个连接到另外两个子按钮的 mainButton.当 mainButton 被按下时,我的 subButtons 将出现在带有动画的视图中.我正在使用以下代码.
I have a simple UIButton in my project with animation block running inside the button. Basically, I have a mainButton that connected to two other sub Buttons. When mainButton is pressed, my subButtons will appear to the view with animation. I am using the following code.
@IBOutlet weak var mainButton: UIButton!
@IBOutlet weak var subButton1: UIButton!
@IBOutlet weak var subButton2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
subButton1.hidden = true
subButton2.hidden = true
}
// override func viewDidAppear(animated: Bool) {
// subButton1.hidden = true
// subButton2.hidden = true
// }
@IBAction func mainButtonAction(sender: AnyObject) {
self.subButton1.hidden = false
self.subButton2.hidden = false
self.subButton1.center.x += self.view.frame.width
self.subButton2.center.x -= self.view.frame.width
//First Piece of Animation:
UIView.animateWithDuration(1.5, delay: 0.05,usingSpringWithDamping: 0.9,initialSpringVelocity: 0.5, options:UIViewAnimationOptions.CurveEaseIn, animations: {
self.subButton1.center.x -= self.view.frame.width
self.subButton2.center.x += self.view.frame.width
}, completion: {finished in
//Second Piece of Animation:
UIView.animateWithDuration(1.5, delay: 5, options:UIViewAnimationOptions.CurveEaseOut, animations: {
self.subButton1.center.x += self.view.frame.width
self.subButton2.center.x -= self.view.frame.width
}, completion: nil)
})
}
以上代码按我的意愿工作,但仅在我第一次按下 mainButton 时才起作用.但是,问题是当我再次按下 mainButton 时没有任何反应.我希望每次按下 mainButton 时都会出现动画....
Above code works as I want it to work but only when I press the mainButton for the first time. But, the problem is that nothing happens when I press the mainButton again. I want the animation to occur every time when the mainButton is pressed....
提前致谢.
推荐答案
您遇到动画出现和消失的时间问题.动画的两个部分都工作正常.如果您提前单击按钮,则会遇到按钮的 X 位置问题.请注意,如果您对动画片段"发表评论,则您的代码工作正常.您可以打印 X 值并查看发生了什么.
You are having an issue with the animation timing for appearing and disappearing. Both portions of animation are working fine. If you click the button ahead of time, you are having issues with the X location of the button. Notice that if you comment on of "pieces of animation" your code works fine. You can print the X values and see what is happening.
几个选项:
- 在两者之间切换.
创建一个起点,当按钮被按下时:
- Toggle between the two.
Create a starting point, and when the button is pressed:
class ViewController: UIViewController {
var intialPointXButton1: CGFloat = 0.0
var intialPointXButton2: CGFloat = 0.0
...
在动画运行时禁用按钮:
Disable the button while the animation is running:
@IBAction func mainButtonAction(sender: AnyObject) {
self.subButton1.hidden = false
self.subButton2.hidden = false
self.subButton1.center.x += self.view.frame.width
self.subButton2.center.x -= self.view.frame.width
self.mainButton.enabled = false // Disable the main button
//First Piece of Animation:
UIView.animateWithDuration(1.5, delay: 0.05,usingSpringWithDamping: 0.9,initialSpringVelocity: 0.5, options:UIViewAnimationOptions.CurveEaseIn, animations: {
self.subButton1.center.x -= self.view.frame.width
self.subButton2.center.x += self.view.frame.width
}, completion: {finished in
//Second Piece of Animation:
UIView.animateWithDuration(1.5, delay: 5, options:UIViewAnimationOptions.CurveEaseOut, animations: {
self.subButton1.center.x += self.view.frame.width
self.subButton2.center.x -= self.view.frame.width
}, completion: { finished in
self.mainButton.enabled = true // Enable the main button
})
})
}
这篇关于Swift 中的 UIAnimation 重复问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!