// // XYPlayPAGView.m // Timi // // Created by 翟玉磊 on 2021/12/28. // #import "XYPlayPAGView.h" @interface XYPlayPAGView () /// PAG显示视图 @property (strong, nonatomic) PAGView *pagView; /// PAG资源文件 @property (strong, nonatomic) PAGFile *pagFile; @end @implementation XYPlayPAGView #pragma mark - Public - (void)clear { if (_pagView) { [_pagView removeFromSuperview]; _pagView = nil; } } /// 设置动画资源路径且是否播放 /// @param fileName 资源文件 /// @param play 是否播放 - (void)setResourcePathWithFileName:(NSString *)fileName play:(BOOL)play { [self setResourcePathWithFileName:fileName]; if (play) { [self.pagView play]; } } /// 只设置动画资源路径 - (void)setResourcePathWithFileName:(NSString *)fileName { // 构建pag播放文件 self.pagFile = [PAGFile Load:[self resourcePathWithFileName:fileName]]; //关联PAGView和PAGFile [self.pagView setComposition:self.pagFile]; } /// 设置播放次数 -1则无限播放 默认1 - (void)setRepeatCount:(int)repeatCount { [self.pagView setRepeatCount:repeatCount]; } /// 动画视图显示方式 拉伸,平铺、、 - (void)setScaleMode:(PAGScaleMode)value { [self.pagView setScaleMode:value]; } /// 替换文本 - (void)replaceText:(XYPlayPAGReplaceTextContentType)type text:(NSString *)text { // 获取第0个图层的文字信息 PAGText *pagText = [self.pagFile getTextData:(int)type]; pagText.text = text; // 设置第0个图层的文字信息,这里可以设置的index为0到numText-1 [self.pagFile replaceText:(int)type data:pagText]; } /// 替换图片 - (void)replaceImage:(XYPlayPAGReplaceImageContentType)type image:(UIImage *)image { // 通过image获取pagImage,pagImage还可以通过path/pixelBuffer等等途径获取,具体请看PAGImage类 PAGImage *pagImage = [PAGImage FromCGImage:image.CGImage]; // 替换第0个图层的图像,这里可以设置的index为0到numImages-1 [self.pagFile replaceImage:(int)type data:pagImage]; } /// 当前播放状态 - (BOOL)isPlaying { return self.pagView.isPlaying; } /// 播放 - (void)play { [self.pagView play]; } /// 暂停 - (void)stop { [self.pagView stop]; } /// 替换过内容刷新 - (BOOL)flush { return [self.pagView flush]; } - (NSString *)resourcePathWithFileName:(NSString *)fileName { return [[NSBundle mainBundle] pathForResource:fileName ofType:@"pag"]; } #pragma mark - InIt - (instancetype)init { if (self = [super init]) { [self setupUI]; } return self; } - (void)setupUI { [self pagView]; } #pragma mark - lazy - (PAGView *)pagView { if (_pagView == nil) { _pagView = [[PAGView alloc] init]; [self addSubview:self.pagView]; [self.pagView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; } return _pagView; } @end