美文网首页iOS开发技术分享
UIImageView 动画自动停止问题

UIImageView 动画自动停止问题

作者: 一只大码农 | 来源:发表于2019-03-30 09:35 被阅读0次

最近在用UIImageView animationImages 做loading动画时,动画会莫名其妙的自己停了

最后发现有2种原因导致

1、视图切换时,比如UIViewController push,UITabViewController 切换,这个会触发willMoveToWindow,导致动画停止

解决方法

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    [super willMoveToWindow:newWindow];
    if(newWindow){
        if(self.loading){
            [self.loadingImageView startAnimating];
        }
    }
}

2、如果loading是放在 UITableViewCell或者UICollectionViewCell上的,在点击cell时会把UIImageView 变成高亮,动画也会停止

解决方法

@interface SeaLoadingImageView : UIImageView

///是否正在loading
@property(nonatomic, assign) BOOL loading;

@end

@implementation SeaLoadingImageView

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if(self.loading && !self.isAnimating){
        [self startAnimating];
    }
}

@end

相关文章

网友评论

    本文标题:UIImageView 动画自动停止问题

    本文链接:https://www.haomeiwen.com/subject/ukxjbqtx.html