BDFaceRemindAnimationView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // BDFaceRemindAnimationView.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by Li,Tonghui on 2020/5/11.
  6. // Copyright © 2020 Baidu. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "BDFaceRemindAnimationView.h"
  10. #define FACESDK_ACTION_BUNDLE_NAME @"com.baidu.idl.face.live.action.image.bundle"
  11. #define FACESDK_ACTION_BUNDLE [[NSBundle alloc] initWithPath:[[NSBundle mainBundle] pathForResource:FACESDK_ACTION_BUNDLE_NAME ofType:nil]]
  12. @interface BDFaceRemindAnimationView ()
  13. @property (nonatomic, assign) BOOL isImageSuccess;
  14. @property (nonatomic, readwrite, retain) UIImageView *imageView;
  15. @property (nonatomic, strong) NSMutableDictionary *imageDict;
  16. @end
  17. @implementation BDFaceRemindAnimationView
  18. - (instancetype)initWithFrame:(CGRect)frame {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-86.7)/2
  22. , (frame.size.height-113.3)/2
  23. , 86.7, 113.3)];
  24. [self addSubview:self.imageView];
  25. self.isImageSuccess = false;
  26. }
  27. return self;
  28. }
  29. - (void)setActionImages {
  30. if (self.isImageSuccess){
  31. return;
  32. }
  33. int typeDigits[] = {18, 23, 31, 31, 31, 31};
  34. self.imageDict = [[NSMutableDictionary alloc] init];
  35. for (int i = 0; i < 6; i++){
  36. NSMutableArray<UIImage *> *imageArr = [[NSMutableArray alloc] init];
  37. for (int k = 1; k <= typeDigits[i]; k++){
  38. NSString *imageName;
  39. if (k < 10){
  40. imageName = [NSString stringWithFormat:@"%d_0%d", i, k];
  41. } else {
  42. imageName = [NSString stringWithFormat:@"%d_%d", i, k];
  43. }
  44. NSString * path = [FACESDK_ACTION_BUNDLE pathForResource:imageName ofType:@"png"];
  45. UIImage *image = [UIImage imageNamed:path];
  46. [imageArr addObject:image];
  47. }
  48. self.imageDict[[NSString stringWithFormat:@"%d", i]] = imageArr;
  49. }
  50. self.isImageSuccess = true;
  51. }
  52. - (BOOL)isActionAnimating {
  53. return [self.imageView isAnimating];
  54. }
  55. - (void)startActionAnimating:(int)type {
  56. if (!self.isImageSuccess){
  57. return;
  58. }
  59. NSMutableArray<UIImage *> *imageArr = self.imageDict[[NSString stringWithFormat:@"%d", type]];
  60. // 设置动画图片
  61. self.imageView.animationImages = imageArr;
  62. // 设置动画图片
  63. // self.imageView.animationImages = self.imageArr;
  64. // 设置动画的播放次数
  65. self.imageView.animationRepeatCount = 1;
  66. // 设置播放时长
  67. // 1秒30帧, 一张图片的时间 = 1/30 = 0.03333 20 * 0.0333
  68. self.imageView.animationDuration = 3.0;
  69. // 开始动画
  70. [self.imageView startAnimating];
  71. }
  72. - (void)stopActionAnimating {
  73. [self.imageView stopAnimating];
  74. }
  75. @end