【发布时间】:2017-02-10 21:09:20
【问题描述】:
我有 4 个按钮,都使用相同的动画功能
- imageViewWithoutDelay
- imageViewWithDelay
- 无延迟查看
- ViewWithDelay
我的代码:
import UIKit
class ViewController: UIViewController {
@IBAction func imageViewithoutDelay(_ sender: AnyObject) {
let circleImage = UIImageView()
// kindly add your own image.
circleImage.image = UIImage(named: "empty")
animate(inputView: circleImage, delay: false)
}
@IBAction func imageViewWithDelay(_ sender: UIButton) {
let circleImage = UIImageView()
circleImage.image = UIImage(named: "empty")
animate(inputView: circleImage, delay: true)
}
func animate(inputView : UIView, delay: Bool){
inputView.layer.cornerRadius = inputView.frame.size.width / 2
inputView.clipsToBounds = true
inputView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(inputView)
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height * 0.10
inputView.center.y = screenHeight
view.setNeedsLayout()
view.setNeedsDisplay()
if delay == true{
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
UIView.animate(withDuration: 2.5, animations: {
inputView.alpha = 0.0
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height * 0.10
inputView.center.y -= screenHeight
})
})} else{
UIView.animate(withDuration: 2.5, animations: {
inputView.alpha = 0.0
let screenSize = UIScreen.main.bounds
let screenHeight = screenSize.height * 0.10
inputView.center.y -= screenHeight
})
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
让我感到有趣的是UIview 和UIImageView 的不同行为。
UIImageView 实例仅按预期设置动画(从原点上升)如果我使用延迟。然而,不使用延迟块,UIImageView 实例首先下降,然后动画上升到其原点)。
UIView 没有表现出不同的行为。它从原点开始动画并上升。
此问题与this 原始问题相切。
【问题讨论】:
标签: swift uiview uiimageview core-animation uiviewanimation