淡入淡出图像数组swift

时间:2015-12-11 09:47:38

标签: ios 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
      })
    }
  }

这是我的形象:

enter image description here

4 个答案:

答案 0 :(得分:2)

你可以用多种方式做到这一点

选择-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
    //
})
}

答案 1 :(得分:0)

您应该在上一个动画完成时开始下一个图像显示动画。如果您将动画代码放在for范围内,动画事务将一次发送到runloop。所以你的观点不正常。

答案 2 :(得分:0)

在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
            }
    }

}

}

答案 3 :(得分:0)

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