Cocoa touch 为什么在动画块中设置层锚点时视图会跳转?
我的iOS应用程序中的视图连接了一个UIPangestureRecognitor。我从pan处理程序的中复制了代码。当手势开始时,我的代码:Cocoa touch 为什么在动画块中设置层锚点时视图会跳转?,cocoa-touch,animation,uiview,cglayer,Cocoa Touch,Animation,Uiview,Cglayer,我的iOS应用程序中的视图连接了一个UIPangestureRecognitor。我从pan处理程序的中复制了代码。当手势开始时,我的代码: 记录原始锚点和中心 将定位点和中心更改为围绕用户手指,如下所示: CGPoint locationInView = [gestureRecognizer locationInView:target]; CGPoint locationInSuperview = [gestureRecognizer locationInView:target.superv
- 记录原始锚点和中心
- 将定位点和中心更改为围绕用户手指,如下所示:
CGPoint locationInView = [gestureRecognizer locationInView:target]; CGPoint locationInSuperview = [gestureRecognizer locationInView:target.superview]; target.layer.anchorPoint = CGPointMake( locationInView.x / target.bounds.size.width, locationInView.y / target.bounds.size.height ); target.center = locationInSuperview;
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveEaseOut animations:^{
target.center = originalCenter;
target.layer.anchorPoint = originalAnchorPoint;
}];
这确实会将视图设置回其原始起点。但是,在动画开始之前,视图会跳转到UI中的其他位置。看,放开,它跳起来,然后它又回到它所属的地方
我想也许我需要在动画外设置锚点和中心,也许将中心设置为超级视图中的位置,就像手势开始时一样,但这似乎没有什么区别
我错过了什么?当用户放手时,如何防止跳转?如果不尝试您正在做的事情,我怀疑有两个问题: 更改定位点会更改视图/图层的位置。要在不修改位置的情况下更改定位点,可以使用以下辅助对象:
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);
newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
CGPoint position = view.layer.position;
position.x -= oldPoint.x;
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
view.layer.position = position;
view.layer.anchorPoint = anchorPoint;
}
(我自己也在我的项目中使用它。在这里找到:)
动画将锚点设置回其原始值。
应使用上面的辅助对象重置定位点。这样可以确保在更改定位时视图不会移动。您必须在动画之外执行此操作。之后,使用动画块更改视图的中心,并将其设置为所需的位置。我以前尝试过此方法,但可能没有正确执行。我今晚再试一次。谢谢嗯,这使得它跳转到屏幕上完全不同的部分,但它仍然会跳转。(试图确定它是否与我设置事物的顺序有关:视图的中心、主播点和变换。答案是“是!”我先是设置中心,然后是主播点,然后是变换。我将其更改为设置主播点(使用辅助对象),然后是变换,最后是中心,现在它工作完全正确。呸!感谢您提醒我这个函数,我只需要再处理一下。请确保您了解什么是主播点: