Ios 在Swift中为图像淡入淡出动画
我有一组图像,我想有淡入我的登录屏幕背景。我在斯威夫特找不到任何能做到这一点的东西。有没有一种方法我能做到 这是我目前的代码:Ios 在Swift中为图像淡入淡出动画,ios,swift,xcode6,Ios,Swift,Xcode6,我有一组图像,我想有淡入我的登录屏幕背景。我在斯威夫特找不到任何能做到这一点的东西。有没有一种方法我能做到 这是我目前的代码: override func viewDidLoad() { super.viewDidLoad() startAnimating() // Do any additional setup after loading the view, typically from a nib. } func startAnimating(){ back
override func viewDidLoad() {
super.viewDidLoad()
startAnimating()
// Do any additional setup after loading the view, typically from a nib.
}
func startAnimating(){
backgroundImage.animationImages = [
UIImage(named: "background1.jpg"),
UIImage(named: "background2.jpg")
]
backgroundImage.animationDuration = 3
backgroundImage.startAnimating()
}
下面的代码将帮助您设置具有淡出效果的图像动画,并更改背景
import UIKit
class ViewController: UIViewController {
var imgView:UIImageView? //class variable to display you bg image
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imgView = UIImageView(frame: self.view.frame) //imgView to display background
self.view.insertSubview(imgView, atIndex: 0) //imgview add above the background of main view
animateImage(1) /*call animageImage with parameter number as image number as i user image name as avatar1.png, avatar2.png, avatar3.png and avatar4.png*/
}
func animateImage(no:Int)
{
var imgNumber:Int = no
let t:NSTimeInterval = 1;
let t1:NSTimeInterval = 0;
var name:String = "avatar\(imgNumber).png"
imgView!.alpha = 0.4
imgView!.image = UIImage(named:name);
//code to animate bg with delay 2 and after completion it recursively calling animateImage method
UIView.animateWithDuration(2.0, delay: 0, options:UIViewAnimationOptions.CurveEaseOut, animations: {() in
self.imgView!.alpha = 1.0;
},
completion: {(Bool) in
imgNumber++;
if imgNumber>4 //only for 4 image
{
imgNumber = 1
}
self.animateImage(imgNumber);
})
}
}
这段代码运行良好。你想用另一种方法来实现这个动画吗?是的,在图像之间有一种缓慢的淡入淡出