Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. [self initPathLayer];
  4. [self initHandleView];
  5. [self initHandlePanGestureRecognizer];
  6. }
  7.  
  8.  
  9. - (void)initPathLayer {
  10. pathLayer_ = [CAShapeLayer layer];
  11. pathLayer_.lineWidth = 1;
  12. pathLayer_.fillColor = nil;
  13. pathLayer_.strokeColor = [UIColor lightGrayColor].CGColor;
  14. pathLayer_.lineCap = kCALineCapButt;
  15. pathLayer_.lineJoin = kCALineJoinRound;
  16. pathLayer_.frame = self.view.bounds;
  17. pathLayerCenter_ = CGPointMake(CGRectGetMidX(pathLayer_.frame), CGRectGetMidY(pathLayer_.frame));
  18. [self.view.layer addSublayer:pathLayer_];
  19. }
  20.  
  21.  
  22. - (void)initHandleView {
  23. handlePathPointIndex_ = 0;
  24. CGRect rect = CGRectMake(0, 0, 30, 30);
  25. CAShapeLayer *circleLayer = [CAShapeLayer layer];
  26. circleLayer.fillColor = [UIColor redColor].CGColor;
  27. circleLayer.strokeColor = [UIColor redColor].CGColor;
  28. circleLayer.lineWidth = 2;
  29. UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(rect, circleLayer.lineWidth, circleLayer.lineWidth)];
  30. circleLayer.frame = rect;
  31. circleLayer.path = circlePath.CGPath;
  32. handleView_ = [[UIView alloc] initWithFrame:rect];
  33.  
  34. CGRect lineRect = CGRectMake(0,0,CGRectGetMaxX([[UIScreen mainScreen] bounds]), CGRectGetMaxY([[UIScreen mainScreen] bounds]));
  35. CGPoint center = CGPointMake(CGRectGetMidX(pathLayer_.frame), CGRectGetMidY(pathLayer_.frame));
  36. lineView_ = [[UIView alloc] initWithFrame:lineRect];
  37. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  38. lineLayer.fillColor = [UIColor blueColor].CGColor;
  39. lineLayer.strokeColor = [UIColor blueColor].CGColor;
  40. lineLayer.lineWidth = 2;
  41. lineLayer.frame = self.view.bounds;
  42. UIBezierPath *linePath = [UIBezierPath bezierPath];
  43. CGPoint circlePoint = CGPointMake(handleView_.center.x, handleView_.center.y);
  44. [linePath moveToPoint:center]; //Center
  45. [linePath addLineToPoint:circlePoint];
  46. [linePath moveToPoint:circlePoint]; //Handle
  47. lineLayer.path = linePath.CGPath;
  48.  
  49. [handleView_.layer insertSublayer:circleLayer above:handleView_.layer];
  50. [handleView_.layer insertSublayer:lineLayer above:handleView_.layer];
  51. // handleView_.layer.masksToBounds = YES;
  52. float direction = DEGREES_TO_RADIANS(10);
  53.  
  54. CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(direction);
  55. [handleView_ setTransform:rotationTransform];
  56.  
  57.  
  58. [self.view addSubview:lineView_];
  59. [self.view addSubview:handleView_];
  60.  
  61. }
  62.  
  63. - (void)handleWasPanned:(UIPanGestureRecognizer *)recognizer {
  64. switch (recognizer.state) {
  65. case UIGestureRecognizerStateBegan: {
  66. desiredHandleCenter_ = handleView_.center;
  67. break;
  68. }
  69.  
  70. case UIGestureRecognizerStateChanged:
  71. case UIGestureRecognizerStateEnded:
  72. case UIGestureRecognizerStateCancelled: {
  73. CGPoint translation = [recognizer translationInView:self.view];
  74. desiredHandleCenter_.x += translation.x;
  75. desiredHandleCenter_.y += translation.y;
  76. [self moveHandleTowardPointAndRotateLine:desiredHandleCenter_];
  77. break;
  78. }
  79.  
  80. default:
  81. break;
  82. }
  83.  
  84. [recognizer setTranslation:CGPointZero inView:self.view];
  85. }
  86.  
  87. - (void)moveHandleTowardPointAndRotateLine:(CGPoint)point {
  88. CGFloat earlierDistance = [self distanceToPoint:point ifHandleMovesByOffset:-1];
  89. CGFloat currentDistance = [self distanceToPoint:point ifHandleMovesByOffset:0];
  90. CGFloat laterDistance = [self distanceToPoint:point ifHandleMovesByOffset:1];
  91. if (currentDistance <= earlierDistance && currentDistance <= laterDistance)
  92. return;
  93.  
  94. NSInteger step;
  95. CGFloat distance;
  96. if (earlierDistance < laterDistance) {
  97. step = -1;
  98. distance = earlierDistance;
  99. } else {
  100. step = 1;
  101. distance = laterDistance;
  102. }
  103.  
  104. NSInteger offset = step;
  105. while (true) {
  106. NSInteger nextOffset = offset + step;
  107. CGFloat nextDistance = [self distanceToPoint:point ifHandleMovesByOffset:nextOffset];
  108. if (nextDistance >= distance)
  109. break;
  110. distance = nextDistance;
  111. offset = nextOffset;
  112. }
  113. handlePathPointIndex_ += offset;
  114.  
  115. // Make one end of the line move with handle (point) while the other is pinned to center of pathLayer_ (ie. in figure8 its the cross point, in a circle it's center)
  116.  
  117. //CGFloat rot = [self getTouchAngle:point];
  118. CGFloat rot = atan2f((point.x - pathLayerCenter_.x), -(point.y - pathLayerCenter_.y));
  119. handleView_.layer.transform = CATransform3DMakeRotation(rot, 0., 0., 1.);
  120. [self layoutHandleView];
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement