ios 在 Swift 中添加图像过渡动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26898955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Image Transition Animation in Swift
提问by user3318660
Below is the code that automatically transitions between different images, every 5 seconds. I want to add animations to the transitions i.e. fade, scroll from left, etc. How would I go about doing this in Swift? Thanks.
下面是每 5 秒自动在不同图像之间转换的代码。我想为过渡添加动画,即淡入淡出、从左侧滚动等。我将如何在 Swift 中执行此操作?谢谢。
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.animationImages = [
UIImage(named: "brooklyn-bridge.jpg")!,
UIImage(named: "grand-central-terminal.jpg")!,
UIImage(named: "new-york-city.jpg")!,
UIImage(named: "one-world-trade-center.jpg")!,
UIImage(named: "rain.jpg")!,
UIImage(named: "wall-street.jpg")!]
imageView.animationDuration = 25.0
imageView.startAnimating()
}
回答by ylin0x81
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
let images = [
UIImage(named: "brooklyn-bridge.jpg")!,
UIImage(named: "grand-central-terminal.jpg")!,
UIImage(named: "new-york-city.jpg"),
UIImage(named: "one-world-trade-center.jpg")!,
UIImage(named: "rain.jpg")!,
UIImage(named: "wall-street.jpg")!]
var index = 0
let animationDuration: NSTimeInterval = 0.25
let switchingInterval: NSTimeInterval = 3
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = images[index++]
animateImageView()
}
func animateImageView() {
CATransaction.begin()
CATransaction.setAnimationDuration(animationDuration)
CATransaction.setCompletionBlock {
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
self.animateImageView()
}
}
let transition = CATransition()
transition.type = kCATransitionFade
/*
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
*/
imageView.layer.addAnimation(transition, forKey: kCATransition)
imageView.image = images[index]
CATransaction.commit()
index = index < images.count - 1 ? index + 1 : 0
}
}
Implement it as a custom image view would be better.
将其实现为自定义图像视图会更好。
回答by ddb
animating code, based on this answer, in Swift 3
动画代码,基于这个答案,在Swift 3
let animationDuration: TimeInterval = 0.25
let switchingInterval: TimeInterval = 3
func animateImageView()
{
CATransaction.begin()
CATransaction.setAnimationDuration(animationDuration)
CATransaction.setCompletionBlock {
DispatchQueue.main.asyncAfter(deadline: .now() + self.switchingInterval) {
self.animateImageView()
}
}
let transition = CATransition()
transition.type = kCATransitionFade
/*
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
*/
imageView.layer.add(transition, forKey: kCATransition)
imageView.image = images.object(at: index) as! UIImage
CATransaction.commit()
index = index < images.count - 1 ? index + 1 : 0
}
回答by Ako
Here is a standalone class you can use to animate image change with fade animation.
这是一个独立的类,您可以使用淡入淡出动画来为图像更改设置动画。
class FadeImageView: UIImageView
{
@IBInspectable
var fadeDuration: Double = 0.13
override var image: UIImage?
{
get {
return super.image
}
set(newImage)
{
if let img = newImage
{
CATransaction.begin()
CATransaction.setAnimationDuration(self.fadeDuration)
let transition = CATransition()
transition.type = kCATransitionFade
super.layer.add(transition, forKey: kCATransition)
super.image = img
CATransaction.commit()
}
else {
super.image = nil
}
}
}
}