2

我有 9 张图像需要交叉淡入淡出。我试图用imageview的animationImages方法做动画。但不幸的是,它不支持图像之间的交叉淡入淡出效果。谁能告诉我如何实现这一目标?

4

3 回答 3

4

我通常使用 Core Animation,具体来说CABasicAnimation

我最近使用了这个,一个带有几个手势识别器的图像视图,用于左右滑动。查看我正在使用的褪色动画:

_someImageView = [[UIImageView alloc] initWithFrame:myFrame];
_someImageView.image = [myArrayOfImages objectAtIndex:0];
[self.view addSubview:_someImageView];
[_someImageView release];

_currentImageIndex = 0;

_someImageView.userInteractionEnabled = YES;

UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[_someImageView addGestureRecognizer:swipeLeftGesture];
[swipeLeftGesture release];

UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage:)];
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[_someImageView addGestureRecognizer:swipeRightGesture];
[swipeRightGesture release];

然后每次我滑动时都会调用它,或者如果你更喜欢基于时间做一些事情,那么让你的计时器调用它,只要确保你增加一个计数器,以便你可以循环图像:

- (void)changeImage:(UISwipeGestureRecognizer *)swipe{

    NSArray *images = [myArrayOfImages images];
    NSInteger nextImageInteger = _currentImageIndex;


    if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)
        nextImageInteger++;
    else
        nextImageInteger--;


    if(nextImageInteger < 0)
        nextImageInteger = images.count -1;
    else if(nextImageInteger > images.count - 1)
        nextImageInteger = 0;

    _currentImageIndex = nextImageInteger;

    UIImage *target = [images objectAtIndex:_currentImageIndex];

    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
    crossFade.duration      = 0.5;
    crossFade.fromValue     = _someImageView.image;
    crossFade.toValue       = target;
    [_someImageView.layer addAnimation:crossFade forKey:@"animateContents"];
    _someImageView.image = target;
}
于 2012-10-10T00:24:55.147 回答
1

只需创建两个图像视图并将它们放在另一个之上。当您想要交叉淡入淡出到下一张图像时,只需将底部图像视图的图像设置为您想要淡入淡出的图像并将顶部图像视图的 alpha 设置为 0。冲洗并重复。

于 2012-10-10T00:16:20.377 回答
0
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *img=[UIImage imageNamed:@"images.jpeg"];
imgview=[[UIImageView alloc]init];
imgview.frame=CGRectMake(30,90, img.size.width, img.size.height);
[self.view addSubview:imgview];
[self animateImages];
}
- (void)animateImages
{
static int count = 0;
NSArray *animationImages = @[[UIImage imageNamed:@"images.jpeg"], [UIImage imageNamed:@"images (1).jpeg"]];
UIImage *image = [animationImages objectAtIndex:(count % [animationImages count])];

[UIView transitionWithView:imgview
                  duration:2.0f // animation duration
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    imgview.image = image;
                } completion:^(BOOL finished) {
                    [self animateImages];
                    count++;
                }];
}

于 2014-09-30T04:57:03.357 回答