16

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()
}
Joel Bell
  • 2,718
  • 3
  • 26
  • 32
user3318660
  • 303
  • 1
  • 3
  • 17

3 Answers3

35
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.

linimin
  • 6,239
  • 2
  • 26
  • 31
4

animating code, based on this answer, in Swift 3

let animationDuration: TimeInterval = 0.25
let switchingInterval: TimeInterval = 3
func animateImageView()
{
    CATransaction.begin()

    CATransaction.setAnimationDuration(animationDuration)
    CATransaction.setCompletionBlock {
        DispatchQueue.main.asyncAfter(deadline: .now() + self.switchingInterval) {
            self.animateImageView()
        }
    }

    let transition = CATransition()
    transition.type = kCATransitionFade
    /*
     transition.type = kCATransitionPush
     transition.subtype = kCATransitionFromRight
     */
    imageView.layer.add(transition, forKey: kCATransition)
    imageView.image = images.object(at: index) as! UIImage

    CATransaction.commit()

    index = index < images.count - 1 ? index + 1 : 0
}
ddb
  • 2,423
  • 7
  • 28
  • 38
3

Here is a standalone class you can use to animate image change with fade animation.

class FadeImageView: UIImageView
{    
    @IBInspectable
    var fadeDuration: Double = 0.13

    override var image: UIImage? 
    {
        get {
            return super.image
        }
        set(newImage) 
        {
            if let img = newImage 
            {
                CATransaction.begin()
                CATransaction.setAnimationDuration(self.fadeDuration)

                let transition = CATransition()
                transition.type = kCATransitionFade

                super.layer.add(transition, forKey: kCATransition)
                super.image = img

                CATransaction.commit()
            } 
            else {
                super.image = nil
            }
        }
    } 
}
Ako
  • 956
  • 1
  • 10
  • 13
  • I added this class to a UIImageView and to use that animation but it doesn't use the custom transition. I add images to animationImages and tell the view to startAnimating() but it's not the custom animation. So what am I doing wrong? Thx – MotoxX Oct 09 '17 at 23:45
  • Without the code I have no clue what could be the problem. If you can, paste the code you wrote and I will see what's wrong. https://pastebin.com/ – Ako Oct 10 '17 at 03:52
  • I made a new Class with your code and added this to the UIImageView in Interface builder. I added an `@IBOutlet weak var slideImageView: FadeImageView!`. In my ViewController I try to start the animation using the following code: `slideImageView.animationImages = [ UIImage(named: "image1")!, UIImage(named: "image2")!, UIImage(named: "image3")!, UIImage(named: "image4")!] slideImageView.animationDuration = 25.0 slideImageView.startAnimating()` I hope this helps... – MotoxX Oct 10 '17 at 08:56
  • UIImageView animationImages does not support changing animation. Take a look [here](https://stackoverflow.com/a/31745806/770081), you should use [CAKeyframeAnimation](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html#//apple_ref/doc/uid/TP40004514-CH3-SW11) for custom animation. – Ako Oct 10 '17 at 10:13