[英]fade in fade out for an images array swift
我有一个图像数组的全局变量
public var images = ["11", "12", "13", "14", "15", "16", "17", "18", "11", "12", "13", "14", "15", "16", "17", "18", "11", "12", "13", "14", "15", "16", "17", "18"]
当我单击按钮时,我想显示它在 2 秒后淡入淡出每个图像。 表示先出现图像“11”,2秒后淡出,图像“12”淡入,以此类推。 但是我的代码不能正常工作。
这是我的代码:
@IBAction func playAutomaticPhotoImages(sender: AnyObject) {
for image in images {
photoImageView.hidden = false
photoImageView.alpha = 1.0
UIView.animateWithDuration(2.0, animations: {
self.photoImageView.image = UIImage(named: image)
self.photoImageView.alpha = 1
}, completion: {
(value: Bool) in
self.photoImageView.hidden = true
self.photoImageView.alpha = 0.0
})
}
}
这是我的形象:
你可以通过多种方式做到这一点
选择-1
@IBAction func playAutomaticPhotoImages(sender: AnyObject) {
self.animateImages()
}
fun animateImages()
{
var count: Int = 0
var images = ["11", "12", "13", "14", "15", "16", "17", "18", "11", "12", "13", "14", "15", "16", "17", "18", "11", "12", "13", "14", "15", "16", "17", "18"]
var image: UIImage = images[(count % images.count)]
UIView.transitionWithView(self.photoImageView, duration: 2.0, options: .CurveEaseOut, animations: {() -> Void in
self.photoImageView.image = image
}, completion: {(finished: Bool) -> Void in
self.animateImages()
// once finished, repeat again
count++
})
}
选择 2
@IBAction func playAutomaticPhotoImages(sender: AnyObject) {
self.animateImages(0) /*call animageImage with parameter number as image number as i user image name as "11", "12", "13", "14", "15", "16", "17", "18", "11", "12", "13", "14", "15", "16", "17", "18", "11", "12", "13", "14", "15", "16", "17", "18"*/
}
func animateImages(no:Int)
{
var Number:Int = no
let t:NSTimeInterval = 1;
let t1:NSTimeInterval = 0;
var name:String = "yourImageName\(Number).png"
self.photoImageView!.alpha = 0.4
self.photoImageView!.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.photoImageView!.alpha = 1.0;
},
completion: {(Bool) in
Number++;
self. animateImages(Number);
})
}
}
选择 3
@IBAction func playAutomaticPhotoImages(sender: AnyObject) {
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "crossfade", userInfo: nil, repeats: true)
photoImageView.animationImages = images
photoImageView.animationDuration = 2
photoImageView.animationRepeatCount = 0
photoImageView.startAnimating()
}
func crossfade() {
UIView.animateWithDuration(2.0, delay: 0.0, options: .CurveEaseInOut, animations: {() -> Void in
photoImageView.alpha = !photoImageView.alpha
}, completion: {(done: Bool) -> Void in
//
})
}
当上一个动画完成时,您应该开始下一个图像显示动画。如果将动画代码放在for
范围内,动画事务将一次发送到 runloop。 所以你的看法不会正常。
在 Swift 中试试这个:
class ImageLoader {
var images = ["Image1","Image2","Image3","Image4"]
var copyImages : [String] = []
var loadNextImage : Bool = false {
didSet {
if loadNextImage{
// Reload the array if its empty.
if copyImages.count == 0 {
copyImages = images
}
// Load next image here.
let theImage = copyImages.removeFirst()
// Pass the image to the load function.
loadNextAnimationImage(theImage)
}
}
}
func loadNextAnimationImage(theImage : String){
UIView.animateWithDuration(2.0, animations: { () -> Void in
// Do your animation action here.
print ("The i")
}) { (animationComplete) -> Void in
if animationComplete {
self.loadNextImage = true
}
}
}
}
var images = ["1","2","3"]
private func showImage( at index : Int) {
if slideImage1.image != nil {
slideImage1.fadeOut { (finished: Bool) in
self.slideImage1.image = UIImage(named: self.images[index])
self.slideImage1.fadeIn()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
self.showImage(at: (index + 1) % self.images.count)
}
}
} else {
slideImage1.image = UIImage(named: images[index])
slideImage1.fadeIn()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
self.showImage(at: (index + 1) % self.images.count)
}
}
}
我使用过fadeIn() 和fadeOut(),来显示数组中的图像。
slideImage1 是 UIImageView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.