【发布时间】:2015-03-16 10:37:09
【问题描述】:
我有两个图像视图。第一个是蓝色箭头,第二个是白色圆圈,画了一个黑点代表圆圈的中心。
我正在尝试旋转箭头,使其锚点是图片中的黑点,如下所示
现在我正在将箭头图层的锚点设置为这样计算的点
CGFloat y = _userImageViewContainer.center.y - CGRectGetMinY(_directionArrowView.frame);
CGFloat x = _userImageViewContainer.center.x - CGRectGetMinX(_directionArrowView.frame);
CGFloat yOff = y / CGRectGetHeight(_directionArrowView.frame);
CGFloat xOff = x / CGRectGetWidth(_directionArrowView.frame);
_directionArrowView.center = _userImageViewContainer.center;
CGPoint anchor = CGPointMake(xOff, yOff);
NSLog(@"anchor: %@", NSStringFromCGPoint(anchor));
_directionArrowView.layer.anchorPoint = anchor;
由于锚点设置为视图的百分比,即中心的坐标为 (.5, .5),我正在计算箭头框架中黑点所在高度的百分比。但是我的数学,即使在手工计算之后,仍然会导致 0.5,这是不正确的,因为当箭头处于原始位置(垂直,指向向上)时,它向下超过一半。
我正在根据用户的指南针方向旋转
CLHeading *heading = [notif object];
// update direction of arrow
CGFloat degrees = [self p_calculateAngleBetween:[PULAccount currentUser].location.coordinate
and:_user.location.coordinate];
_directionArrowView.transform = CGAffineTransformMakeRotation((degrees - heading.trueHeading) * M_PI / 180);
旋转是正确的,只是锚点工作不正常。关于如何实现这一点的任何想法?
【问题讨论】:
-
_directionArrowView.center = _userImageViewContainer.center;这两者怎么可能一样呢? directionArrowView.center 将是_userImageViewContainer.center上方的一个点,对吗? -
你是对的。我认为这条线可能会适得其反。也许如果我的自动布局约束可以正确排列它们,它应该可以工作
标签: ios objective-c cgaffinetransform