0

I have an NSArray of five images and a UIImageView. I want to be able to slowly increase the image size inside the ImageView for 5 seconds and then move to the next image and so on. How can I achieve this?

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Stefan Olaru
  • 63
  • 13

3 Answers3

1

You could use completion blocks provided by UIAnimation. In completion block you can also change the image for example.

The concept is:

func animateView(view: UIView, scale: CGFloat, duration: NSTimeInterval = 1, delay: NSTimeInterval = 0)
{
    UIView.animateWithDuration(duration, delay: delay, options: [.CurveEaseInOut], animations: { () -> Void in
        view.transform = CGAffineTransformMakeScale(scale, scale)
        }, completion: { (Bool) -> Void in
            self.animateView(nextImageView, scale: scale)
    })
}

You can also try to search Apple documentation or here on StackOverflow

Community
  • 1
  • 1
David Rysanek
  • 901
  • 12
  • 16
0

My idea for this problem is: I will create size of each image from small to big. and add all array. After that I assign it to animationImages of UImageView. I set duration, repeat count and start animate. You can see my code:

NSMutableArray *test = [NSMutableArray array];
for (int i = 2; i <= 6; i++) {
    [test addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%i", i]]];
}
for (int i = 2; i <= 6; i++) {
    [test addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%ir", i]]];
}
self.imgTest.animationImages = [NSArray arrayWithArray:test];
self.imgTest.animationDuration = 10.0;
self.imgTest.animationRepeatCount = 1;
[self.imgTest startAnimating];

Solution 2: You can use this function

- (void)animate:(int)i {
if (i >= self.listImage.count) {
    return;
}
[UIView animateWithDuration:5
                      delay:0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:(void (^)(void)) ^{
                     self.imgTest.transform=CGAffineTransformMakeScale(1.2, 1.2);
                 }
                 completion:^(BOOL finished){
                     self.imgTest.transform=CGAffineTransformIdentity;
//Fade
                     [UIView transitionWithView:self.imgTest
                                       duration:1.0f
                                        options:UIViewAnimationOptionTransitionCrossDissolve
                                     animations:^{
                                         self.imgTest.image = self.test[i];
                                     } completion:^(BOOL finished){
                                         [self animate:i+1];
                                     }];
                 }];
}

And when start you just call:

self.imgTest.image = self.listImage[0];
[self animate:0];
vien vu
  • 4,277
  • 2
  • 17
  • 30
0

Here is my solution, it only uses 2% CPU on iPhone 6.

- (void)changeFrame{

    CGRect rect = self.imageView.frame;

    rect.size.width  = rect.size.width +0.25;
    rect.size.height = rect.size.height + 0.25;

    self.imageView.frame = rect;

    counter ++;

    if (counter == 130) {
        index ++;
        if (index == images.count) {
            index = 0;
        }

        self.imageView.image = [images objectAtIndex:index];
        self.imageView.frame = firstRect;

        self.myView.alpha = 0.0f;
        self.myView.alpha = 1.0f;

        counter = 0;
    }

}

And I call it with a timer like so:

 index = 0;
    counter = 0;
    images = [[NSMutableArray alloc] init];
    NSInteger animationImageCount = 4;
    for (int i = 0; i < animationImageCount; i++) {
        // Images are numbered IndexedImagesInMyAnimation0, 1, 2, etc...
        UIImage *image = [Globals imageWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"IndexedImagesInMyAnimation%d", i]] scaledToSize:[Globals aspectScaledImageSizeForImageView:self.imageView image:[UIImage imageNamed:[NSString stringWithFormat:@"IndexedImagesInMyAnimation%d", i]]]];

        [images addObject:image];
    }

    self.imageView.image = [images objectAtIndex:0];
    self.myView.clipsToBounds = YES;
    firstRect = self.imageView.frame;

    timer = [NSTimer timerWithTimeInterval:0.1
                                             target:self
                                           selector:@selector(changeFrame)
                                           userInfo:nil
                                            repeats:YES];

   [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Stefan Olaru
  • 63
  • 13