【发布时间】:2015-09-09 00:08:06
【问题描述】:
我的工作:
我一直在尝试向应用程序添加箭头,但最困难的是让它完成它需要做的事情。箭头需要从由手指触摸表示的起点绘制,并在用户在屏幕上拖动手指时增大箭头。然后箭头必须在手指抬起时停止绘制。
我遇到的问题:
当用户触摸箭头任一端的点并沿一个方向拖动手指时,箭头必须能够围绕起点和终点旋转。旋转时,箭头必须能够增大和减小它的大小。
我可以让箭头围绕起点旋转,但有问题。围绕终点旋转会导致“鱼尾”效果。我很确定我这样做的方式是完全错误的,但我不知道任何其他方式。这就是为什么要求社区引导我朝着正确的方向前进。
@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
【问题讨论】:
-
“一个与 Skitch 应用中的箭头功能相同的箭头”你能描述一下这是什么意思吗?
-
箭头需要做到以下几点: 1. 用手指运动在屏幕上绘图。 (我得到了这个工作。) 2. 以箭头的起点或终点为轴心点在 360 度圆中平移。 (我无法正确使用它。)我以 Skitch 为例,因为它比描述更容易看到。
-
所以你误用了“pan”这个词?你真的只是指旋转?
-
是的,围绕箭头起源的中心点将箭头旋转 360 度圈,同时能够增加起点和起点的大小。
-
很抱歉,您一直在更改“规格”。这是您第一次提到“同时能够增加尺寸”。这使得问题是什么变得非常不清楚,并且无法提供帮助。请停下来想想你到底想要什么。然后编辑您的问题以清楚地描述您遇到的问题,并显示您到目前为止的代码。
标签: ios objective-c transform skitch