What I've got working:
I have been trying to add an arrow to an app, but am having the a most difficult time getting it to do what it needs to do. The arrow needs to be drawn from a start point as represented by a finger touch and increase in size while the user drags a finger across the screen. The arrow then must stop drawing one the finger is lifted.
What I'm having trouble with:
The arrow must be able to rotate around both the start point and the end point when the user touches a point a either end of the arrow and drags a finger in a direction. While rotating, the arrow must be able to increase and decrease it's size.
I am able to get the arrow to somewhat rotate around the start point, but with issues. Rotating around the end point causes a "fish tailing" effect. I'm pretty sure the way I'm doing it is completely wrong, but I don't know of any other way. This is why asking the community to steer me in the right direction.
@implementation ViewController {
ArrowView *_selectedArrowView;
UIColor *_selectedColor;
CGFloat _selectedWeight;
CGFloat _initalAngle;
}
- (void)viewDidLoad {
[super viewDidLoad];
_selectedColor = [UIColor yellowColor];
_selectedWeight = 3;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
[self.view addGestureRecognizer:pan];
_selectedArrowView = [[ArrowView alloc] initWithFrame:CGRectMake(300, 300, 150, 25) withColor:_selectedColor withWeight:_selectedWeight withStartPoint:CGPointMake(300, 300) withEndPoint:CGPointMake(450, 300)];
_selectedArrowView.delegate = self;
_selectedArrowView.layer.anchorPoint = CGPointMake(0, 0);
_selectedArrowView.layer.position = CGPointMake(150,300);
[self.view addSubview:_selectedArrowView];
[self.view bringSubviewToFront:_selectedArrowView];
}
- (void) panHandler: (UIPanGestureRecognizer *) sender {
CGPoint touchPoint = [sender locationInView:sender.view];
if (sender.state == UIGestureRecognizerStateBegan) {
_initalAngle = atan2(touchPoint.y, touchPoint.x);
} else if (sender.state == UIGestureRecognizerStateChanged) {
CGFloat currentAngle = atan2(touchPoint.y, touchPoint.x);
CGFloat angle = _initalAngle - currentAngle;
CGAffineTransform transform = CGAffineTransformMakeRotation(angle * -1);
_selectedArrowView.transform = transform;
}
}
@end