DKCameraView.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // BQCameraView.m
  3. // carame
  4. //
  5. //
  6. #import "DKCameraView.h"
  7. #define BQCameraViewW 60
  8. @interface DKCameraView ()
  9. @property (strong, nonatomic) CADisplayLink *link;
  10. @property (assign, nonatomic) NSInteger time;
  11. @property (assign, nonatomic) CGPoint point;
  12. @property (assign, nonatomic) BOOL isPlayerEnd;
  13. @end
  14. @implementation DKCameraView
  15. - (CADisplayLink *)link{
  16. if (!_link) {
  17. self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(refreshView:)];
  18. }
  19. return _link;
  20. }
  21. - (void) refreshView : (CADisplayLink *) link{
  22. [self setNeedsDisplay];
  23. self.time++;
  24. }
  25. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  26. if (self.isPlayerEnd) return;
  27. self.isPlayerEnd = YES;
  28. UITouch *touch = [touches anyObject];
  29. CGPoint point = [touch locationInView:touch.view];
  30. self.point = point;
  31. [self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  32. if ([self.delegate respondsToSelector:@selector(cameraDidSelected:)]) {
  33. [self.delegate cameraDidSelected:self];
  34. }
  35. }
  36. -(void)drawRect:(CGRect)rect{
  37. [super drawRect:rect];
  38. if (self.isPlayerEnd) {
  39. CGFloat rectValue = BQCameraViewW - self.time % BQCameraViewW;
  40. CGRect rectangle = CGRectMake(self.point.x - rectValue / 2.0, self.point.y - rectValue / 2.0, rectValue, rectValue);
  41. //获得上下文句柄
  42. CGContextRef currentContext = UIGraphicsGetCurrentContext();
  43. if (rectValue <= 30) {
  44. self.isPlayerEnd = NO;
  45. [self.link removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  46. self.link = nil;
  47. self.time = 0;
  48. CGContextClearRect(currentContext, rectangle);
  49. }else{
  50. //创建图形路径句柄
  51. CGMutablePathRef path = CGPathCreateMutable();
  52. //设置矩形的边界
  53. //添加矩形到路径中
  54. CGPathAddRect(path,NULL, rectangle);
  55. //添加路径到上下文中
  56. CGContextAddPath(currentContext, path);
  57. // //填充颜色
  58. [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:0] setFill];
  59. //设置画笔颜色
  60. [[UIColor yellowColor] setStroke];
  61. //设置边框线条宽度
  62. CGContextSetLineWidth(currentContext,1.0f);
  63. //画图
  64. CGContextDrawPath(currentContext, kCGPathFillStroke);
  65. /* 释放路径 */
  66. CGPathRelease(path);
  67. }
  68. }
  69. }
  70. @end