123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //
- // FUCaptureButton.m
- // FULiveDemo
- //
- // Created by 项林平 on 2022/8/10.
- //
- #import "FUCaptureButton.h"
- #import "FUCircleProgressView.h"
- #import "UIButton+FU.h"
- @interface FUCaptureButton ()
- @property (nonatomic, strong) UIButton *button;
- @property (nonatomic, strong) FUCircleProgressView *circleProgress;
- @property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
- @property (nonatomic, strong) NSTimer *timer;
- @end
- @implementation FUCaptureButton {
- NSInteger timeCount;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self addSubview:self.button];
- [self addSubview:self.circleProgress];
-
- // 点击拍照
- __weak typeof(self) weakSelf = self;
- [self.button addCommonActionWithDelay:0.1 actionHandler:^{
- __strong typeof(weakSelf) strongSelf = weakSelf;
- if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(captureButtonDidTakePhoto)]) {
- [strongSelf.delegate captureButtonDidTakePhoto];
- }
- }];
-
- // 长按录制视频
- self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
- self.longPress.minimumPressDuration = 0.3;
- [self addGestureRecognizer:self.longPress];
-
- _recordVideo = YES;
- }
- return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- self.circleProgress.frame = CGRectMake(1, 1, CGRectGetWidth(self.frame) - 2, CGRectGetHeight(self.frame) - 2);
- }
- #pragma mark - Private methods
- - (void)startRecording {
- [UIView animateWithDuration:0.5 animations:^{
- self.transform = CGAffineTransformMakeScale(1.1, 1.1);
- }];
- if (self.delegate && [self.delegate respondsToSelector:@selector(captureButtonDidStartRecording)]) {
- [self.delegate captureButtonDidStartRecording];
- }
- }
- - (void)stopRecording {
- self.transform = CGAffineTransformIdentity;
- if (self.delegate && [self.delegate respondsToSelector:@selector(captureButtonDidFinishRecording)]) {
- [self.delegate captureButtonDidFinishRecording];
- }
- }
- - (void)invalidateTimer {
- [self.timer invalidate];
- self.timer = nil;
- timeCount = 0;
- self.circleProgress.percent = 0;
- }
- #pragma mark - Event response
- - (void)longPressAction:(UILongPressGestureRecognizer *)sender {
- if (sender.state == UIGestureRecognizerStateBegan) {
- self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimeAction) userInfo:nil repeats:YES];
- [self.timer fire];
- [self startRecording];
- } else if (sender.state == UIGestureRecognizerStateEnded) {
- [self stopRecording];
- [self invalidateTimer];
- }
- }
- - (void)updateTimeAction {
- timeCount += 1;
- self.circleProgress.percent += 0.01;
- if (timeCount > 100) {
- // 自动结束录制
- [self stopRecording];
- [self invalidateTimer];
- }
- }
- #pragma mark - Setters
- - (void)setRecordVideo:(BOOL)recordVideo {
- _recordVideo = recordVideo;
- self.longPress.enabled = recordVideo;
- }
- #pragma mark - Getters
- - (UIButton *)button {
- if (!_button) {
- _button = [UIButton buttonWithType:UIButtonTypeCustom];
- _button.frame = self.bounds;
- [_button setImage:[UIImage imageNamed:@"render_camera_capture"] forState:UIControlStateNormal];
- _button.adjustsImageWhenHighlighted = NO;
- _button.adjustsImageWhenDisabled = NO;
- }
- return _button;
- }
- - (FUCircleProgressView *)circleProgress {
- if (!_circleProgress) {
- _circleProgress = [[FUCircleProgressView alloc] initWithFrame:CGRectMake(1, 1, CGRectGetWidth(self.frame) - 2, CGRectGetHeight(self.frame) - 2)];
- _circleProgress.progressColor = [UIColor colorWithRed:92 / 255.0 green:181 / 255.0 blue:249 / 255.0 alpha:1];
- _circleProgress.backgroundColor = [UIColor clearColor];
- _circleProgress.progressBackgroundColor = [UIColor clearColor];
- _circleProgress.userInteractionEnabled = NO;
- }
- return _circleProgress;
- }
- @end
|