Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用另一个视图旋转视图';s中心作为锚点_Ios_Objective C_Cgaffinetransform - Fatal编程技术网

Ios 使用另一个视图旋转视图';s中心作为锚点

Ios 使用另一个视图旋转视图';s中心作为锚点,ios,objective-c,cgaffinetransform,Ios,Objective C,Cgaffinetransform,我有两个图像视图。第一个是蓝色箭头,第二个是白色圆圈,画一个黑点代表圆圈的中心 我试着旋转箭头,所以它的固定点是图片中的黑点,像这样 CGFloat y = _userImageViewContainer.center.y - CGRectGetMinY(_directionArrowView.frame); CGFloat x = _userImageViewContainer.center.x - CGRectGetMinX(_directionArrowView.frame); CGFl

我有两个图像视图。第一个是蓝色箭头,第二个是白色圆圈,画一个黑点代表圆圈的中心

我试着旋转箭头,所以它的固定点是图片中的黑点,像这样

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;

现在我将箭头图层的锚点设置为这样计算的点

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),因此我正在计算黑点落下的箭头帧中高度的百分比。但是我的数学,即使是在手工计算之后,仍然会得到.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);

旋转是正确的,只是锚点工作不正常。关于如何实现这一点有什么想法吗?

也许最好使用更简单的解决方案-使箭头图像的大小更大、更方正。因此,黑点将位于图像的中心

请比较所附图片,你明白我在说什么

中间有黑点的新图像

带移位点的旧图像


现在,您可以轻松地使用标准定位点(0.5,0.5)旋转编辑后的图像

我总是发现定位点的内容不稳定,尤其是旋转。我想试试这样的

    CGPoint convertedCenter = [_directionArrowView convertPoint:_userImageViewContainer.center fromView:_userImageViewContainer ];

     CGSize offset = CGSizeMake(_directionArrowView.center.x - convertedCenter.x, _directionArrowView.center.y - convertedCenter.y);
 // I may have that backwards, try the one below if it offsets the rotation in the wrong direction..
//  CGSize offset = CGSizeMake(convertedCenter.x -_directionArrowView.center.x , convertedCenter.y - _directionArrowView.center.y); 
     CGFloat rotation = 0; //get your angle (radians)

     CGAffineTransform tr = CGAffineTransformMakeTranslation(-offset.width, -offset.height);
          tr = CGAffineTransformConcat(tr, CGAffineTransformMakeRotation(rotation) );
          tr = CGAffineTransformConcat(tr, CGAffineTransformMakeTranslation(offset.width, offset.height) );


    [_directionArrowView setTransform:tr];

注意。UIView上的transform属性是可设置动画的,因此如果需要,可以将最后一行放在动画块中。

\u directionalRowView.center=\u userImageViewContainer.center这两者怎么可能相同?DirectionRowView.center将位于用户图像视图容器.center上方的一个点上,对吗?你说得对。我认为这句话可能会适得其反。也许如果我的自动布局约束可以正确排列它们,它应该会起作用。这似乎是一个不错的解决方案,但由于自动布局的原因,它正在崩溃:
无法同时满足约束
。我将仔细研究一下,看看是否可以修复约束以使用此功能关闭em并对这两个视图使用UIViewAutoResizingMask。。有时候,自动布局在iOs上太聪明了,不利于自己(在cocoa中太棒了,这在cocoa中是非常需要的…)我刚刚实现了这个功能。对于偏移量,我没有使用
convertedCenter
,而是比较了superview框架中两个视图的中心:
CGSize offset=CGSizeMake(_userImageViewContainer.center.x-_directionalRowView.center.x,_userImageViewContainer.center.y-_directionalRowView.center.y)谢谢你的帮助