gpt4 book ai didi

iOS 在 ImageView 中交叉淡化多个图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:42 25 4
gpt4 key购买 nike

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

最佳答案

我通常使用Core Animation,特别是CABasicAnimation

我最近使用了这个,一个 ImageView ,带有几个用于左右滑动的手势识别器。查看我正在使用的淡入淡出动画:

_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;
}

关于iOS 在 ImageView 中交叉淡化多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com