zoukankan      html  css  js  c++  java
  • UIImageView帧动画,包含暂停和继续功能

    //
    //  SecondViewController.m
    //  Birth_OC
    //
    //  Created by lsp on 2021/6/25.
    //
    
    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @property (strong, nonatomic) UIImageView *animationImageView;
    @property (assign, nonatomic) BOOL isStart;
    @property (weak, nonatomic) IBOutlet UIButton *startBtn;
    @property (weak, nonatomic) IBOutlet UIButton *pausebtn;
    @property (strong, nonatomic) NSMutableArray *imageArrM;
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationItem.title = @"帧动画控制";
        [self setupUI];
    }
    
    - (void)setupUI {
        // 初始化UIImageView
        self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 200, self.view.bounds.size.width - 120, self.view.bounds.size.width - 120)];
        self.animationImageView.image = [UIImage imageNamed:@"3_0000.jpg"];
        self.animationImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:self.animationImageView];
    }
    
    #pragma mark - 播放与停止
    - (IBAction)start:(UIButton *)sender {
        if (self.animationImageView.isAnimating) {
            // 停止动画
            [self.animationImageView stopAnimating];
            self.animationImageView.animationImages = nil;
            self.imageArrM = nil;
            self.isStart = NO;
            [sender setTitle:@"播放动画" forState:UIControlStateNormal];
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateEnd) object:nil];
            
        }else {
            // 开始动画
            [self.imageArrM removeAllObjects];
            for(NSInteger i = 0; i < 51; i++){
                NSString *imagePath = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"3_00%02ld.jpg", i] ofType:nil];
                UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
                [self.imageArrM addObject:image];
            }
            self.animationImageView.animationImages = self.imageArrM;
            self.animationImageView.animationDuration = 3.0;
            self.animationImageView.animationRepeatCount = 1;
            [self.animationImageView startAnimating];
            self.isStart = YES;
            [sender setTitle:@"停止动画" forState:UIControlStateNormal];
            [self performSelector:@selector(animateEnd) withObject:nil afterDelay:3.0];
        }
    }
    
    #pragma mark - 暂停与恢复
    - (IBAction)resume:(UIButton *)sender {
        if (!self.animationImageView.isAnimating) {
            return;
        }
        CALayer *layer = self.animationImageView.layer;
        if (self.isStart) {
            // 暂停
            CFTimeInterval currentTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
            layer.speed = 0;
            layer.timeOffset = currentTime;
            
            self.isStart = NO;
            [sender setTitle:@"恢复动画" forState:UIControlStateNormal];
        }else {
            [sender setTitle:@"暂停动画" forState:UIControlStateNormal];
            self.isStart = YES;
            // 恢复
            CFTimeInterval pausedTime = layer.timeOffset;
            layer.speed = 1;
            layer.timeOffset = 0;
            CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() - pausedTime fromLayer:nil];
            layer.beginTime = timeSincePause;
        }
    }
    
    - (void)animateEnd {
        [self.startBtn setTitle:@"播放动画" forState:UIControlStateNormal];
        self.isStart = NO;
    }
    
    -(NSMutableArray *)imageArrM {
        if (!_imageArrM) {
            _imageArrM = [NSMutableArray array];
        }
        return _imageArrM;
    }
    
    @end
  • 相关阅读:
    SQL Server删除表及删除表中数据的方法
    springmvc+spring+mybatis+sqlserver----插入一条新数据
    fiddler filters 使用(fiddler只显示指定请求,fiddler不显示指定请求,即filter请求过滤)(转)
    rest-vs-websocket-comparison-benchmarks
    需求分析+概要设计+详细设计
    win10专业版怎么激活?
    Risingstack we help companies succeed with Node.js
    Mouse Recoil
    Node.js async await
    Node.js Performance
  • 原文地址:https://www.cnblogs.com/dashengios/p/14931519.html
Copyright © 2011-2022 走看看