0

我正在尝试使用替代图像设置 UIImageView 的图像。我知道我可以做 [imageView setImage: image]。但是,更改图像时是否可以添加任何溶解效果(动画)?

4

3 回答 3

3

我之前已经这样做了,这里有一段代码可以帮助你。这段代码可以很好地解决你上面描述的问题。

-(void)changeImage
{
        myImageView.animationImages=[NSArray arrayWithArray:self.yourImageArray];
        myImageView.animationDuration=10.0;
        myImageView.animationRepeatCount=0;
        [myImageView startAnimating];
        myImageView.transform = CGAffineTransformMakeRotation(M_PI/2);
        [self.view addSubview:myImageView];
        //The timers time interval is the imageViews animation duration devided by the       number of images in the animationImages array. 20/5 = 4
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
                                                 target:self
                                               selector:@selector(onTimer)
                                               userInfo:nil
                                                repeats:YES];

        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [timer fire];

}

//这将淡入淡出图像。

-(void)onTimer{
        [UIView animateWithDuration:2.0 animations:^{
            myImageView.alpha = 0.0;
        }];
        [UIView animateWithDuration:0.5 animations:^{
            myImageView.alpha = 1.0;
        }];
    }

如果你想要交叉溶解,你可以使用这个 UIViewAnimationOptionTransitionCrossDissolve。希望这对您有所帮助,如果没有,请随时询问。

于 2013-11-04T06:14:02.793 回答
2

您可以使用 UIView 的transitionWithView方法来实现这一点。

示例代码片段:

UIImage * newImage= [UIImage imageNamed:@"newImg.png"];
[UIView transitionWithView:self.view
              duration:3.0f
               options:UIViewAnimationOptionTransitionCrossDissolve
            animations:^{
              yourImageView.image = newImage;
            } completion:nil];
于 2013-11-04T06:02:50.080 回答
1

您可以通过使用 CAAnimation 过渡来实现此目的

您正在寻找的效果被称为kCATransitionFade

在这里这里看看一些方便的教程以及类参考。

于 2013-11-04T03:34:47.797 回答