V2SuperUser
2022-07-01 09:56:15 +08:00
```swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let screenBounds = UIScreen.main.bounds
let imageView = UIImageView(image: UIImage(named: "24110307_5"))
view.addSubview(imageView)
let imageW = screenBounds.width + 100
imageView.frame = CGRect(x: 0, y: 0, width: imageW, height: screenBounds.height)
//方式 1
// UIView.animate(withDuration: 4) {
// imageView.frame = CGRect(x: -100, y: 0, width: imageW, height: screenBounds.height)
// } completion: { isFinished in
// if isFinished{
// print("结束")
// }
// }
//方式 2:
let animation = CABasicAnimation()
animation.keyPath = "transform.translation.x"
animation.fromValue = 0
animation.toValue = -100
animation.duration = 4
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
imageView.layer.removeAllAnimations()
imageView.layer.add(animation, forKey: nil)
}
}
```