如何在切换图像时应用动画

时间:2014-10-06 09:29:37

标签: ios objective-c

我的屏幕顶部有一个imageView,我想要的是 - 以特定的时间间隔切换图像,并用动画制作它(我没有指明真正的动画应该是什么,可能会褪色例)。我建议添加该代码段:

 // load all the frames of our animation
    self.imageSlideshow.animationImages = [NSArray arrayWithObjects:
                                      self.firstImageIps, self.secondImageIps, self.thirdImageIps, self.fourthImageIps, self.fifthImageIps, nil];

    // all frames will execute in 1.75 seconds
    self.imageSlideshow.animationDuration = 1.75;
    // repeat the animation forever
    self.imageSlideshow.animationRepeatCount = 0;
    // start animating
    NSLog(@"that one called?");
    [self.imageSlideshow startAnimating];

但这里的图像只能切换,没有任何动画。 这里是更严重的问题 - 如果我将持续时间设置为更多,然后是3秒,它开始行为怪异(不是所有图像显示,第一个图像显示更频繁,然后是第二个和第三个。看起来它无法实现完整周期并在一段时间后一次又一次地移动到第一张图片。)

如何使用动画切换图像,定时器间隔为5-7秒?

任何建议都将不胜感激,谢谢!

4 个答案:

答案 0 :(得分:2)

试试这个:

   - (void)viewDidLoad {
    [super viewDidLoad];
    self.imgArr = [[NSArray alloc] initWithObjects:@"DSC06715.JPG",@"Snapshot_20121113_18.JPG",@"IMAG1689.jpg",@"IMAG1720.jpg",@"IMAG1396.jpg", nil];

    [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(performTransition) userInfo:nil repeats:YES];

    }

    -(void)performTransition
    {
      int randImg = arc4random()%self.imgArr.count;
      [self.imageView1 setImage:[UIImage imageNamed:[self.imgArr objectAtIndex:randImg]]];

      CATransition *transition = [CATransition animation];
      transition.duration = 1;
      transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

      NSString *types[4] = {kCATransitionFade};
      transition.type = types[1];
      transition.delegate = self;
      [self.imageView1.layer addAnimation:transition forKey:nil];
    }

答案 1 :(得分:1)

做动画的最简单方法之一。

代码示例:

   [UIView animateWithDuration:0.3 animations:^{
                        //change alpha with animation.
                        self.friendsTableView.alpha = 1.f;
                    } completion:^(BOOL finished) {

                    }];

此动画也可以使用delayoptions进行设置。据我所知,这是设置适当动画的最简单方法。

答案 2 :(得分:1)

-(void)animateImages
{
    count++;

   [UIView transitionWithView:imageSlideshow
                      duration:2.0f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        imageSlideshow.image = [imagesArray objectAtIndex: count % [imagesArray count]];
                    } completion:^(BOOL finished) {
                        [self animateImages];
                    }];
}

此代码段将帮助您添加任何类型的动画。

答案 3 :(得分:1)

要在2个视图之间制作动画,您可以使用空格UIView方法:

- (void)makeCrossDissolveAnimation {
    NSArray *images = self.imageSlideshow.animationImages;
    static int counter = 0;
    UIImageView *startView = [[UIImageView alloc] initWithImage:images[counter]];
    counter = (counter+1) % [images count];
    UIImageView *endView = [[UIImageView alloc] initWithImage:images[counter]];
    [UIView transitionFromView:startView toView:endView duration:ELAnimationDuration
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {
                        // Your completion block
                    }];
}