首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >更改UIImageView的图像时淡入/淡出

更改UIImageView的图像时淡入/淡出
EN

Stack Overflow用户
提问于 2011-10-04 02:03:54
回答 5查看 93.4K关注 0票数 163

与创建两个UIImageViews相比,简单地更改一个视图的image似乎是合乎逻辑的。如果我这样做了,是否存在在两个图像之间进行淡出/交叉融合而不是即时切换?

EN

回答 5

Stack Overflow用户

发布于 2017-03-10 12:03:39

对于Swift 3.0.1:

代码语言:javascript
复制
UIView.transition(with: self.imageView,
              duration:0.5,
              options: .transitionCrossDissolve,
              animations: { self.imageView.image = newImage },
              completion: nil)

参考:https://gist.github.com/licvido/bc22343cacfa3a8ccf88

票数 9
EN

Stack Overflow用户

发布于 2011-10-04 02:12:02

是的,你所说的是绝对正确的,这就是你所做的。我写了这个方法&总是用它来淡出我的图像。为此,我与CALayer打交道。为此,您需要导入核心动画。

代码语言:javascript
复制
+ (void)fadeInLayer:(CALayer *)l
{
    CABasicAnimation *fadeInAnimate   = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeInAnimate.duration            = 0.5;
    fadeInAnimate.repeatCount         = 1;
    fadeInAnimate.autoreverses        = NO;
    fadeInAnimate.fromValue           = [NSNumber numberWithFloat:0.0];
    fadeInAnimate.toValue             = [NSNumber numberWithFloat:1.0];
    fadeInAnimate.removedOnCompletion = YES;
    [l addAnimation:fadeInAnimate forKey:@"animateOpacity"];
    return;
}

您可以对淡出图像执行相反的操作。在它淡出之后。只需将其从superview (即UIImageView)中删除即可。[imageView removeFromSuperview]

票数 6
EN

Stack Overflow用户

发布于 2013-03-11 04:44:46

我需要无限期地重复这个过渡。在这个过程中,我进行了大量的尝试和错误,但我最终得到了我想要的最终结果。这些是用于向UITableViewCell中的UIImageView添加图像动画的代码片段。

相关代码如下:

代码语言:javascript
复制
@interface SomeViewController ()
@property(nonatomic, strong) NSMutableArray *imagesArray;
@property(nonatomic, assign) NSInteger varietyImageAnimationIndex;
@property(nonatomic, assign) BOOL varietyImagesAnimated;
@end

@implementation SomeViewController
@synthesize imagesArray;
@synthesize varietyImageAnimationIndex;
@synthesize varietyImagesAnimated;
...

// NOTE: Initialize the array of images in perhaps viewDidLoad method.

-(void)animateImages
{
    varietyImageAnimationIndex++;

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ...
        [cell.imageView setImage:[imagesArray objectAtIndex:0]];


        [self setVarietyImageView:cell.imageView];

        if (! varietyImagesAnimated)
        {
            varietyImagesAnimated = YES;
            [self animateImages];
        }

    ...
    return cell;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7638831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档