FUCaptureButton.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // FUCaptureButton.m
  3. // FULiveDemo
  4. //
  5. // Created by 项林平 on 2022/8/10.
  6. //
  7. #import "FUCaptureButton.h"
  8. #import "FUCircleProgressView.h"
  9. #import "UIButton+FU.h"
  10. @interface FUCaptureButton ()
  11. @property (nonatomic, strong) UIButton *button;
  12. @property (nonatomic, strong) FUCircleProgressView *circleProgress;
  13. @property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
  14. @property (nonatomic, strong) NSTimer *timer;
  15. @end
  16. @implementation FUCaptureButton {
  17. NSInteger timeCount;
  18. }
  19. - (instancetype)initWithFrame:(CGRect)frame {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. [self addSubview:self.button];
  23. [self addSubview:self.circleProgress];
  24. // 点击拍照
  25. __weak typeof(self) weakSelf = self;
  26. [self.button addCommonActionWithDelay:0.1 actionHandler:^{
  27. __strong typeof(weakSelf) strongSelf = weakSelf;
  28. if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(captureButtonDidTakePhoto)]) {
  29. [strongSelf.delegate captureButtonDidTakePhoto];
  30. }
  31. }];
  32. // 长按录制视频
  33. self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  34. self.longPress.minimumPressDuration = 0.3;
  35. [self addGestureRecognizer:self.longPress];
  36. _recordVideo = YES;
  37. }
  38. return self;
  39. }
  40. - (void)layoutSubviews {
  41. [super layoutSubviews];
  42. self.circleProgress.frame = CGRectMake(1, 1, CGRectGetWidth(self.frame) - 2, CGRectGetHeight(self.frame) - 2);
  43. }
  44. #pragma mark - Private methods
  45. - (void)startRecording {
  46. [UIView animateWithDuration:0.5 animations:^{
  47. self.transform = CGAffineTransformMakeScale(1.1, 1.1);
  48. }];
  49. if (self.delegate && [self.delegate respondsToSelector:@selector(captureButtonDidStartRecording)]) {
  50. [self.delegate captureButtonDidStartRecording];
  51. }
  52. }
  53. - (void)stopRecording {
  54. self.transform = CGAffineTransformIdentity;
  55. if (self.delegate && [self.delegate respondsToSelector:@selector(captureButtonDidFinishRecording)]) {
  56. [self.delegate captureButtonDidFinishRecording];
  57. }
  58. }
  59. - (void)invalidateTimer {
  60. [self.timer invalidate];
  61. self.timer = nil;
  62. timeCount = 0;
  63. self.circleProgress.percent = 0;
  64. }
  65. #pragma mark - Event response
  66. - (void)longPressAction:(UILongPressGestureRecognizer *)sender {
  67. if (sender.state == UIGestureRecognizerStateBegan) {
  68. self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimeAction) userInfo:nil repeats:YES];
  69. [self.timer fire];
  70. [self startRecording];
  71. } else if (sender.state == UIGestureRecognizerStateEnded) {
  72. [self stopRecording];
  73. [self invalidateTimer];
  74. }
  75. }
  76. - (void)updateTimeAction {
  77. timeCount += 1;
  78. self.circleProgress.percent += 0.01;
  79. if (timeCount > 100) {
  80. // 自动结束录制
  81. [self stopRecording];
  82. [self invalidateTimer];
  83. }
  84. }
  85. #pragma mark - Setters
  86. - (void)setRecordVideo:(BOOL)recordVideo {
  87. _recordVideo = recordVideo;
  88. self.longPress.enabled = recordVideo;
  89. }
  90. #pragma mark - Getters
  91. - (UIButton *)button {
  92. if (!_button) {
  93. _button = [UIButton buttonWithType:UIButtonTypeCustom];
  94. _button.frame = self.bounds;
  95. [_button setImage:[UIImage imageNamed:@"render_camera_capture"] forState:UIControlStateNormal];
  96. _button.adjustsImageWhenHighlighted = NO;
  97. _button.adjustsImageWhenDisabled = NO;
  98. }
  99. return _button;
  100. }
  101. - (FUCircleProgressView *)circleProgress {
  102. if (!_circleProgress) {
  103. _circleProgress = [[FUCircleProgressView alloc] initWithFrame:CGRectMake(1, 1, CGRectGetWidth(self.frame) - 2, CGRectGetHeight(self.frame) - 2)];
  104. _circleProgress.progressColor = [UIColor colorWithRed:92 / 255.0 green:181 / 255.0 blue:249 / 255.0 alpha:1];
  105. _circleProgress.backgroundColor = [UIColor clearColor];
  106. _circleProgress.progressBackgroundColor = [UIColor clearColor];
  107. _circleProgress.userInteractionEnabled = NO;
  108. }
  109. return _circleProgress;
  110. }
  111. @end