Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self initPathLayer];
- [self initHandleView];
- [self initHandlePanGestureRecognizer];
- }
- - (void)initPathLayer {
- pathLayer_ = [CAShapeLayer layer];
- pathLayer_.lineWidth = 1;
- pathLayer_.fillColor = nil;
- pathLayer_.strokeColor = [UIColor lightGrayColor].CGColor;
- pathLayer_.lineCap = kCALineCapButt;
- pathLayer_.lineJoin = kCALineJoinRound;
- pathLayer_.frame = self.view.bounds;
- pathLayerCenter_ = CGPointMake(CGRectGetMidX(pathLayer_.frame), CGRectGetMidY(pathLayer_.frame));
- [self.view.layer addSublayer:pathLayer_];
- }
- - (void)initHandleView {
- handlePathPointIndex_ = 0;
- CGRect rect = CGRectMake(0, 0, 30, 30);
- CAShapeLayer *circleLayer = [CAShapeLayer layer];
- circleLayer.fillColor = [UIColor redColor].CGColor;
- circleLayer.strokeColor = [UIColor redColor].CGColor;
- circleLayer.lineWidth = 2;
- UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(rect, circleLayer.lineWidth, circleLayer.lineWidth)];
- circleLayer.frame = rect;
- circleLayer.path = circlePath.CGPath;
- handleView_ = [[UIView alloc] initWithFrame:rect];
- CGRect lineRect = CGRectMake(0,0,CGRectGetMaxX([[UIScreen mainScreen] bounds]), CGRectGetMaxY([[UIScreen mainScreen] bounds]));
- CGPoint center = CGPointMake(CGRectGetMidX(pathLayer_.frame), CGRectGetMidY(pathLayer_.frame));
- lineView_ = [[UIView alloc] initWithFrame:lineRect];
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.fillColor = [UIColor blueColor].CGColor;
- lineLayer.strokeColor = [UIColor blueColor].CGColor;
- lineLayer.lineWidth = 2;
- lineLayer.frame = self.view.bounds;
- UIBezierPath *linePath = [UIBezierPath bezierPath];
- CGPoint circlePoint = CGPointMake(handleView_.center.x, handleView_.center.y);
- [linePath moveToPoint:center]; //Center
- [linePath addLineToPoint:circlePoint];
- [linePath moveToPoint:circlePoint]; //Handle
- lineLayer.path = linePath.CGPath;
- [handleView_.layer insertSublayer:circleLayer above:handleView_.layer];
- [handleView_.layer insertSublayer:lineLayer above:handleView_.layer];
- // handleView_.layer.masksToBounds = YES;
- float direction = DEGREES_TO_RADIANS(10);
- CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(direction);
- [handleView_ setTransform:rotationTransform];
- [self.view addSubview:lineView_];
- [self.view addSubview:handleView_];
- }
- - (void)handleWasPanned:(UIPanGestureRecognizer *)recognizer {
- switch (recognizer.state) {
- case UIGestureRecognizerStateBegan: {
- desiredHandleCenter_ = handleView_.center;
- break;
- }
- case UIGestureRecognizerStateChanged:
- case UIGestureRecognizerStateEnded:
- case UIGestureRecognizerStateCancelled: {
- CGPoint translation = [recognizer translationInView:self.view];
- desiredHandleCenter_.x += translation.x;
- desiredHandleCenter_.y += translation.y;
- [self moveHandleTowardPointAndRotateLine:desiredHandleCenter_];
- break;
- }
- default:
- break;
- }
- [recognizer setTranslation:CGPointZero inView:self.view];
- }
- - (void)moveHandleTowardPointAndRotateLine:(CGPoint)point {
- CGFloat earlierDistance = [self distanceToPoint:point ifHandleMovesByOffset:-1];
- CGFloat currentDistance = [self distanceToPoint:point ifHandleMovesByOffset:0];
- CGFloat laterDistance = [self distanceToPoint:point ifHandleMovesByOffset:1];
- if (currentDistance <= earlierDistance && currentDistance <= laterDistance)
- return;
- NSInteger step;
- CGFloat distance;
- if (earlierDistance < laterDistance) {
- step = -1;
- distance = earlierDistance;
- } else {
- step = 1;
- distance = laterDistance;
- }
- NSInteger offset = step;
- while (true) {
- NSInteger nextOffset = offset + step;
- CGFloat nextDistance = [self distanceToPoint:point ifHandleMovesByOffset:nextOffset];
- if (nextDistance >= distance)
- break;
- distance = nextDistance;
- offset = nextOffset;
- }
- handlePathPointIndex_ += offset;
- // 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)
- //CGFloat rot = [self getTouchAngle:point];
- CGFloat rot = atan2f((point.x - pathLayerCenter_.x), -(point.y - pathLayerCenter_.y));
- handleView_.layer.transform = CATransform3DMakeRotation(rot, 0., 0., 1.);
- [self layoutHandleView];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement