You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止FLAnimatedImageView从ViewController返回时自动启动动画

FLAnimatedImageView 跳转返回自动播放问题修复方案

根因说明

FLAnimatedImageView 框架内置了视图可见性监听逻辑:当视图被添加到UIWindow层级(即pop回原页面、cell滚动进入可视区域)时,只要animatedImage属性已赋值,就会默认自动触发动画播放,属于框架原生默认行为,和你之前加的点击控制逻辑无关。

你之前在跳转按钮上加校验逻辑无法生效,是因为自动播放行为发生在pop返回、视图重新渲染的阶段,不是点击跳转的阶段。

修复步骤

  • 第一步:给自定义UITableViewCell添加播放状态标记属性
    Objective-C 示例:
    //  cell头文件添加属性
    @property (nonatomic, assign) BOOL shouldPlayGif;
    
    Swift 示例:
    var shouldPlayGif: Bool = false
    
  • 第二步:重写cell的willMoveToWindow:方法,拦截自动播放逻辑
    该方法会在cell被添加到视图层级/从视图层级移除时触发,pop返回场景必然会调用,在该方法内按照你自己存储的状态控制动画播放:
    Objective-C 示例:
    - (void)willMoveToWindow:(UIWindow *)newWindow {
        [super willMoveToWindow:newWindow];
        // 仅处理cell即将显示到页面的场景
        if (newWindow) {
            if (self.shouldPlayGif) {
                [self.animatedImageView startAnimating];
            } else {
                [self.animatedImageView stopAnimating];
                // 可选:需要停在第一帧就加下面这行
                self.animatedImageView.currentFrameIndex = 0;
            }
        }
    }
    
    Swift 示例:
    override func willMove(toWindow newWindow: UIWindow?) {
        super.willMove(toWindow: newWindow)
        guard let newWindow = newWindow else { return }
        if shouldPlayGif {
            animatedImageView.startAnimating()
        } else {
            animatedImageView.stopAnimating()
            // 可选:需要停在第一帧就加下面这行
            animatedImageView.currentFrameIndex = 0
        }
    }
    
  • 第三步:同步点击事件的状态标记
    修改你原有的点击播放/停止逻辑,操作动画的同时同步更新shouldPlayGif的值:
    // 点击播放按钮/动图的触发逻辑
    - (void)gifPlayToggleTriggered {
        self.shouldPlayGif = !self.shouldPlayGif;
        if (self.shouldPlayGif) {
            [self.animatedImageView startAnimating];
        } else {
            [self.animatedImageView stopAnimating];
        }
    }
    
  • 第四步:复用cell时同步状态
    在tableView:cellForRowAtIndexPath:方法中给cell赋值时,按照你数据源中存储的对应位置动图播放状态,给cell的shouldPlayGif赋值,避免cell复用导致的状态错乱。

内容的提问来源于stack exchange,提问作者Eman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 23:54:03