Created
May 17, 2017 13:25
-
-
Save ozero/77e3e4b58e122dfcefcba6db76d8f773 to your computer and use it in GitHub Desktop.
UIImageView cross-fade slideshow autoplay repeat sample in Swift 3 without rotten pods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var imagesArraySlideshow : [UIImage] = [] | |
var slideShowIndex:NSInteger = 0 | |
var slideShowMax:NSInteger = 0 | |
var ivSlideshow:UIImageView = UIImageView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
buildImagesArraySlideshow() | |
//slideshow | |
self.ivSlideshow = (self.view.viewWithTag(Int(114514)) as? UIImageView)! //SET TAG Int(114514) with your UIImageView | |
self.slideShowMax = self.imagesArraySlideshow.count | |
DispatchQueue.global(qos: .userInteractive).async ( | |
execute: {() -> Void in | |
while 1 == 1 { | |
print ("MAX:"+String(self.slideShowMax)) | |
DispatchQueue.main.async(execute: {() -> Void in | |
let toImage = self.imagesArraySlideshow[self.slideShowIndex] | |
print ("index:"+String(self.slideShowIndex)) | |
UIView.transition( | |
with: self.ivSlideshow, | |
duration: 0.3, | |
options: .transitionCrossDissolve, | |
animations: {self.ivSlideshow.image = toImage}, | |
completion: nil | |
) | |
}) | |
self.slideShowIndex += 1 | |
if self.slideShowIndex == self.slideShowMax { | |
self.slideShowIndex = 0 | |
} | |
sleep(3) | |
} | |
}) | |
} | |
func buildImagesArraySlideshow(){ | |
// example: localImageFilePath:URL = *URLs FOR SOME LOCAL IMAGE FILES* | |
// for localImageFilePath in YOURIMAGEGFILES { | |
do { | |
let imageData = try Data(contentsOf: localImageFilePath) | |
imagesArraySlideshow.append(UIImage(data: imageData)!) | |
} catch { | |
print("Error loading image : \(error)") | |
} | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment