XYPlayPAGView.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // XYPlayPAGView.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/12/28.
  6. //
  7. #import "XYPlayPAGView.h"
  8. @interface XYPlayPAGView ()
  9. /// PAG显示视图
  10. @property (strong, nonatomic) PAGView *pagView;
  11. /// PAG资源文件
  12. @property (strong, nonatomic) PAGFile *pagFile;
  13. @end
  14. @implementation XYPlayPAGView
  15. #pragma mark - Public
  16. - (void)clear {
  17. if (_pagView) {
  18. [_pagView removeFromSuperview];
  19. _pagView = nil;
  20. }
  21. }
  22. /// 设置动画资源路径且是否播放
  23. /// @param fileName 资源文件
  24. /// @param play 是否播放
  25. - (void)setResourcePathWithFileName:(NSString *)fileName play:(BOOL)play {
  26. [self setResourcePathWithFileName:fileName];
  27. if (play) {
  28. [self.pagView play];
  29. }
  30. }
  31. /// 只设置动画资源路径
  32. - (void)setResourcePathWithFileName:(NSString *)fileName {
  33. // 构建pag播放文件
  34. self.pagFile = [PAGFile Load:[self resourcePathWithFileName:fileName]];
  35. //关联PAGView和PAGFile
  36. [self.pagView setComposition:self.pagFile];
  37. }
  38. /// 设置播放次数 -1则无限播放 默认1
  39. - (void)setRepeatCount:(int)repeatCount {
  40. [self.pagView setRepeatCount:repeatCount];
  41. }
  42. /// 动画视图显示方式 拉伸,平铺、、
  43. - (void)setScaleMode:(PAGScaleMode)value {
  44. [self.pagView setScaleMode:value];
  45. }
  46. /// 替换文本
  47. - (void)replaceText:(XYPlayPAGReplaceTextContentType)type text:(NSString *)text {
  48. // 获取第0个图层的文字信息
  49. PAGText *pagText = [self.pagFile getTextData:(int)type];
  50. pagText.text = text;
  51. // 设置第0个图层的文字信息,这里可以设置的index为0到numText-1
  52. [self.pagFile replaceText:(int)type data:pagText];
  53. }
  54. /// 替换图片
  55. - (void)replaceImage:(XYPlayPAGReplaceImageContentType)type image:(UIImage *)image {
  56. // 通过image获取pagImage,pagImage还可以通过path/pixelBuffer等等途径获取,具体请看PAGImage类
  57. PAGImage *pagImage = [PAGImage FromCGImage:image.CGImage];
  58. // 替换第0个图层的图像,这里可以设置的index为0到numImages-1
  59. [self.pagFile replaceImage:(int)type data:pagImage];
  60. }
  61. /// 当前播放状态
  62. - (BOOL)isPlaying {
  63. return self.pagView.isPlaying;
  64. }
  65. /// 播放
  66. - (void)play {
  67. [self.pagView play];
  68. }
  69. /// 暂停
  70. - (void)stop {
  71. [self.pagView stop];
  72. }
  73. /// 替换过内容刷新
  74. - (BOOL)flush {
  75. return [self.pagView flush];
  76. }
  77. - (NSString *)resourcePathWithFileName:(NSString *)fileName {
  78. return [[NSBundle mainBundle] pathForResource:fileName ofType:@"pag"];
  79. }
  80. #pragma mark - InIt
  81. - (instancetype)init {
  82. if (self = [super init]) {
  83. [self setupUI];
  84. }
  85. return self;
  86. }
  87. - (void)setupUI {
  88. [self pagView];
  89. }
  90. #pragma mark - lazy
  91. - (PAGView *)pagView {
  92. if (_pagView == nil) {
  93. _pagView = [[PAGView alloc] init];
  94. [self addSubview:self.pagView];
  95. [self.pagView mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.edges.equalTo(self);
  97. }];
  98. }
  99. return _pagView;
  100. }
  101. @end