Swift 中的 UIAnimation 重复问题
UIAnimation repeating issue in Swift
我的项目中有一个简单的 UIButton,按钮内有动画块 运行。基本上,我有一个连接到另外两个子按钮的 mainButton。当按下 mainButton 时,我的 subButtons 将出现在带有动画的视图中。我正在使用以下代码。
@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 时都出现动画....
提前致谢。
您遇到了出现和消失的动画时间问题。动画的两个部分都工作正常。如果提前单击按钮,按钮的 X 位置就会出现问题。
请注意,如果您对 "pieces of animation" 发表评论,您的代码可以正常工作。您可以打印 X 值并查看发生了什么。
几个选项:
- 在两者之间切换。
创建起点,按下按钮时:
class ViewController: UIViewController {
var intialPointXButton1: CGFloat = 0.0
var intialPointXButton2: CGFloat = 0.0
...
动画播放时禁用按钮 运行:
@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
})
})
}
我的项目中有一个简单的 UIButton,按钮内有动画块 运行。基本上,我有一个连接到另外两个子按钮的 mainButton。当按下 mainButton 时,我的 subButtons 将出现在带有动画的视图中。我正在使用以下代码。
@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 时都出现动画....
提前致谢。
您遇到了出现和消失的动画时间问题。动画的两个部分都工作正常。如果提前单击按钮,按钮的 X 位置就会出现问题。 请注意,如果您对 "pieces of animation" 发表评论,您的代码可以正常工作。您可以打印 X 值并查看发生了什么。
几个选项:
- 在两者之间切换。
创建起点,按下按钮时:
class ViewController: UIViewController { var intialPointXButton1: CGFloat = 0.0 var intialPointXButton2: CGFloat = 0.0 ...
动画播放时禁用按钮 运行:
@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 }) }) }