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

 

posted on 2021-06-25 16:27  大圣ios博客  阅读(349)  评论(0编辑  收藏  举报