ImageView animation, looping through images
Asked Answered
K

2

6

So I want to animate a UIImageView on a load screen and I've been told that the built in "animationImages" method takes up a lot of memory and is bad programming so I won't use that, I also had issues with it but that's besides the point.

The code below almost works but it animates through way too fast even though I put 3 seconds.

func animate() {

    UIView.animateWithDuration(3, animations: { () -> Void in

        self.logoImageView.image = UIImage(named: "00")
        self.logoImageView.image = UIImage(named: "02")
        self.logoImageView.image = UIImage(named: "03")
        self.logoImageView.image = UIImage(named: "04")
        self.logoImageView.image = UIImage(named: "05")
        self.logoImageView.image = UIImage(named: "06")
        self.logoImageView.image = UIImage(named: "07")
        self.logoImageView.image = UIImage(named: "08")
        self.logoImageView.image = UIImage(named: "09")
        self.logoImageView.image = UIImage(named: "10")
        self.logoImageView.image = UIImage(named: "11")
        self.logoImageView.image = UIImage(named: "12")
        self.logoImageView.image = UIImage(named: "13")
        self.logoImageView.image = UIImage(named: "14")
        self.logoImageView.image = UIImage(named: "15")
        self.logoImageView.image = UIImage(named: "16")
        self.logoImageView.image = UIImage(named: "17")
        self.logoImageView.image = UIImage(named: "18")
        self.logoImageView.image = UIImage(named: "19")
        self.logoImageView.image = UIImage(named: "20")
        self.logoImageView.image = UIImage(named: "21")
        self.logoImageView.image = UIImage(named: "22")
        self.logoImageView.image = UIImage(named: "23")
        self.logoImageView.image = UIImage(named: "24")
        self.logoImageView.image = UIImage(named: "25")
        self.logoImageView.image = UIImage(named: "26")

        }) { (success) -> Void in

            self.fadeInLabel()
    }

However, the code below here fades in the welcome label and works just fine so I wonder what the issue with the above is. Thanks for the help!

func fadeInLabel() {

    UIView.animateWithDuration(2, animations: { () -> Void in

        self.labelImageView.alpha = 1

        }) { (success) -> Void in

          self.performSelector("pushToCreateVC", withObject: self, afterDelay: 1)
    }

}
Kobold answered 16/11, 2015 at 3:38 Comment(1)
The timer method mentioned above is better, but still not the whole story: https://mcmap.net/q/1913971/-pre-rendered-core-graphics-animation-doesn-39-t-animate-smoothly-amp-hogs-memorySway
G
4

If u want to animate images u can use animationImages property of UIImageView

self.logoImageView.animationImages = imagesListArray;
self.logoImageView.animationDuration = 3.0
self.logoImageView.startAnimating()

where imagesListArray is array of images u want to animate

OR

If u want to animate the images with custom animation u can use below code where i used UIViewAnimationOptionTransitionFlipFromLeft animation option.Belowcode is in Obj-C , I hope u can map the semantic.

// in view Load
 _slide = 0
 [self changeSlide];

// Loop gallery 
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0f target:self selector:@selector(changeSlide) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];


- (void)changeSlide
{

if(_slide > _galleryImages.count-1) _slide = 0;

UIImage *toImage = [UIImage imageNamed:_galleryImages[_slide]];
[UIView transitionWithView:_yourimageView
              duration:0.6f
               options:UIViewAnimationOptionTransitionFlipFromLeft
            animations:^{
                _yourimageView.image = toImage;

            } completion:nil];
_slide++;

}
Garderobe answered 16/11, 2015 at 5:11 Comment(0)
S
0

I had an issue with the animationImages method reverting the imageView back to the original image on completion. I am not sure if there is a better way, but I ended up using a timer so the imageView keeps the last image once complete.

var counter = 0
var timer = NSTimer()
@IBAction func countButton(sender: UIButton) {

    timer.invalidate()
    timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "timerAction", userInfo: nil, repeats: true)

}

func timerAction() {
    ++counter
    switch counter{
    case 1 :
        image1.image = UIImage(named:"Bird-1.gif")
    case 2 :
        image1.image = UIImage(named:"Bird-2.gif")
    case 3 :
        image1.image = UIImage(named:"Bird-3.gif")
    case 4 :
        image1.image = UIImage(named:"Bird-4.gif")
    case 5 :
        image1.image = UIImage(named:"Bird-5.gif")
    case 6 :
        counter = 0
        timer.invalidate()
    default :
        print("Error")
    }
}
Soundless answered 16/2, 2016 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.