BDFaceRemindAnimationView.m 3.0 KB

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