FUPhotoButton.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // PhotoButton.m
  3. // FULiveDemo
  4. //
  5. // Created by liuyang on 2016/12/27.
  6. // Copyright © 2016年 liuyang. All rights reserved.
  7. //
  8. #import "FUPhotoButton.h"
  9. #import "FUCircleProgressView.h"
  10. #import <AudioToolbox/AudioToolbox.h>
  11. #import <MediaPlayer/MediaPlayer.h>
  12. #import <AVFoundation/AVFoundation.h>
  13. #import "FUVolumeObserver.h"
  14. @interface FUPhotoButton ()<FUVolumeObserverProtocol>
  15. {
  16. NSTimer *timer;
  17. NSInteger time;
  18. CGAffineTransform originTransform ;
  19. }
  20. @property (nonatomic, strong) FUCircleProgressView *circleProgress;
  21. @property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
  22. @end
  23. @implementation FUPhotoButton
  24. -(instancetype)initWithFrame:(CGRect)frame{
  25. if (self = [super initWithFrame:frame]) {
  26. time = 0;
  27. _type = FUPhotoButtonTypeRecord | FUPhotoButtonTypeTakePhoto;
  28. _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
  29. _longPress.minimumPressDuration = 0; //定义按的时间
  30. [self addGestureRecognizer:_longPress];
  31. [self addSubview:self.circleProgress];
  32. // [FUVolumeObserver sharedInstance].delegate = self;
  33. }
  34. return self;
  35. }
  36. - (void)awakeFromNib
  37. {
  38. [super awakeFromNib];
  39. time = 0;
  40. _type = FUPhotoButtonTypeRecord | FUPhotoButtonTypeTakePhoto;
  41. _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
  42. _longPress.minimumPressDuration = 0; //定义按的时间
  43. [self addGestureRecognizer:_longPress];
  44. [self addSubview:self.circleProgress];
  45. // [FUVolumeObserver sharedInstance].delegate = self;
  46. }
  47. - (void)layoutSubviews
  48. {
  49. [super layoutSubviews];
  50. self.circleProgress.frame = CGRectMake(1, 1, self.bounds.size.width - 2, self.bounds.size.height - 2);
  51. }
  52. - (FUCircleProgressView *)circleProgress
  53. {
  54. if(!_circleProgress)
  55. {
  56. _circleProgress = [[FUCircleProgressView alloc] initWithFrame:CGRectMake(1, 1, self.bounds.size.width - 2, self.bounds.size.height - 2)];
  57. _circleProgress.backgroundColor = [UIColor clearColor];
  58. _circleProgress.progressColor = [UIColor colorWithRed:92 / 255.0 green:181 / 255.0 blue:249 / 255.0 alpha:1];
  59. _circleProgress.progressWidth = 3;
  60. _circleProgress.progressBackgroundColor = [UIColor clearColor];
  61. _circleProgress.clockwise = 0;
  62. _circleProgress.percent = 0;
  63. }
  64. return _circleProgress;
  65. }
  66. -(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
  67. if (gestureRecognizer.state == 1) {
  68. self.selected = YES;
  69. timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTime) userInfo:NULL repeats:YES];
  70. [timer fire];
  71. [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
  72. }else if(gestureRecognizer.state == 3)
  73. {
  74. [self photoButtonFinishRecord];
  75. }
  76. }
  77. -(void)photoButtonFinishRecord{
  78. if (timer) {
  79. if (time <= 3) {
  80. [self takePhoto];
  81. }else
  82. {
  83. [self stopRecord];
  84. }
  85. [timer invalidate];
  86. timer = nil;
  87. time = 0;
  88. _circleProgress.percent = 0;
  89. self.selected = NO;
  90. NSLog(@"time invalidate");
  91. }
  92. }
  93. - (void)updateTime
  94. {
  95. time ++;
  96. if (time - 4 >=0) {
  97. if (time - 4 == 0) {
  98. [self startRecord];
  99. }
  100. _circleProgress.percent += 0.01;
  101. }
  102. if (time - 4 >= 100 ) {
  103. [self stopRecord];
  104. [timer invalidate];
  105. timer = nil;
  106. time = 0;
  107. _circleProgress.percent = 0;
  108. self.selected = NO;
  109. }
  110. }
  111. -(void)takePhoto{
  112. if (!(_type & FUPhotoButtonTypeTakePhoto)) return;
  113. if ([self.delegate respondsToSelector:@selector(takePhoto)]) {
  114. [self.delegate takePhoto];
  115. }
  116. }
  117. -(void)startRecord{
  118. if (!(_type & FUPhotoButtonTypeRecord)) return;
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. originTransform = self.transform ;
  121. [UIView animateWithDuration:0.5 animations:^{
  122. // self.transform = CGAffineTransformMakeTranslation(0, -5);
  123. self.transform = CGAffineTransformScale(self.transform, 1.1, 1.1);
  124. }];
  125. if ([self.delegate respondsToSelector:@selector(startRecord)]) {
  126. [self.delegate startRecord];
  127. }
  128. });
  129. }
  130. -(void)stopRecord{
  131. if (!(_type & FUPhotoButtonTypeRecord)) return;
  132. dispatch_async(dispatch_get_main_queue(), ^{
  133. if ([self.delegate respondsToSelector:@selector(stopRecord)]) {
  134. [self.delegate stopRecord];
  135. }
  136. // [UIView animateWithDuration:0.5 animations:^{
  137. self.transform = originTransform;
  138. // }];
  139. });
  140. }
  141. -(void)setType:(FUPhotoButtonType)type{
  142. _type = type;
  143. if (type & FUPhotoButtonTypeRecord){
  144. _circleProgress.hidden = NO;
  145. [self addGestureRecognizer:_longPress];
  146. }else{
  147. _circleProgress.hidden = YES;
  148. [self removeGestureRecognizer:_longPress];
  149. [self addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
  150. }
  151. }
  152. -(void)dealloc{
  153. NSLog(@"FUPhotoButton dealloc-----");
  154. }
  155. @end