添加图像过渡动画斯威夫特 [英] Adding Image Transition Animation in Swift
本文介绍了添加图像过渡动画斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是code,不同的图像,每5秒之间自动转换。我想动画添加到转换,即褪色,从左至右滚动,等等。我怎么会去斯威夫特这样做呢?谢谢你。
类的ViewController:UIViewController的{
@IBOutlet弱VAR ImageView的:UIImageView的!覆盖FUNC viewDidLoad中(){
super.viewDidLoad() imageView.animationImages = [
的UIImage(命名为:布鲁克林bridge.jpg)!
的UIImage(命名为:盛大中央terminal.jpg)!
的UIImage(命名为:新约克city.jpg)!
的UIImage(命名为:一个世界贸易中心.jpg)!
的UIImage(命名为:rain.jpg)!
的UIImage(命名为:墙street.jpg)! imageView.animationDuration = 25.0
imageView.startAnimating()
}
解决方案
类的ViewController:UIViewController的{
@IBOutlet弱VAR ImageView的:UIImageView的! 让图像= [
的UIImage(命名为:布鲁克林bridge.jpg)!
的UIImage(命名为:盛大中央terminal.jpg)!
的UIImage(命名为:新约克city.jpg),
的UIImage(命名为:一个世界贸易中心.jpg)!
的UIImage(命名为:rain.jpg)!
的UIImage(命名为:墙street.jpg)!
VAR指数= 0
让animationDuration:NSTimeInterval = 0.25
让switchingInterval:NSTimeInterval = 3 覆盖FUNC viewDidLoad中(){
super.viewDidLoad() imageView.image =图像[指数++]
animateImageView()
} FUNC animateImageView(){
CATransaction.begin() CATransaction.setAnimationDuration(animationDuration)
CATransaction.setCompletionBlock {
让延迟= dispatch_time(DISPATCH_TIME_NOW,Int64的(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
dispatch_after(延迟,dispatch_get_main_queue()){
self.animateImageView()
}
} 让过渡= CATransition()
transition.type = kCATransitionFade
/ *
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
* /
imageView.layer.addAnimation(过渡,forKey:kCATransition)
imageView.image =图像[指数] CATransaction.commit() 指数=指数< images.count - 1?索引+ 1:0
}
}
它实现为自定义图像视图效果会更好。
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.
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()
}
解决方案
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.
这篇关于添加图像过渡动画斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文