【问题标题】:Layer Position jumps at start of (Core) Animation层位置在(核心)动画开始时跳跃
【发布时间】:2012-02-07 20:19:17
【问题描述】:

所以,我正在尝试创建一个磁贴翻转效果,就像在 Windows Phone 7 上一样。

到目前为止,我有以下代码,但我有几个查询。

CALayer *layer = self.theRedSquare.layer;
CATransform3D initialTransform = self.theRedSquare.layer.transform;
initialTransform.m34 = 1.0 / -1000;

[UIView beginAnimations:@"Scale" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
layer.transform = initialTransform;
layer.anchorPoint = CGPointMake(-0.3, 0.5);

CATransform3D rotationAndPerspectiveTransform = self.theRedSquare.layer.transform;

rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI , 0 , -self.theRedSquare.bounds.size.height/2, 0);

layer.transform = rotationAndPerspectiveTransform;

[UIView setAnimationDelegate:self];
[UIView commitAnimations];

1.我的红色方块(一个简单的 UI 视图)怎么会在动画开始时向右平移?

2。对于锚点,是否可以在父视图上设置位置?目前我是相对于当前视图任意设置的。

提前感谢您的任何指点。

动画之前

在动画期间(注意方块向右移动)

动画结束后(注意方块向右移动了)

已添加视频:example video

【问题讨论】:

标签: iphone ios ipad core-animation


【解决方案1】:
-(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;
}

-(void)animateView:(UIView *)theRedSquare
{
    CALayer *layer = theRedSquare.layer;
    CATransform3D initialTransform = theRedSquare.layer.transform;
    initialTransform.m34 = 1.0 / -1000;

    [self setAnchorPoint:CGPointMake(-0.3, 0.5) forView:theRedSquare];


    [UIView beginAnimations:@"Scale" context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    layer.transform = initialTransform;


    CATransform3D rotationAndPerspectiveTransform = theRedSquare.layer.transform;

    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI , 0 , -theRedSquare.bounds.size.height/2, 0);

    layer.transform = rotationAndPerspectiveTransform;

    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

取自此,稍作调整.. 希望对您有所帮助 Changing my CALayer's anchorPoint moves the view

【讨论】:

  • 你如何让动画只是动画? (那个翻转)我已经尝试了好几个小时来解决这个问题。
【解决方案2】:

如果您将正方形设为自己的视图,则翻转是内置的

[UIView beginAnimations:@"flip" context:nil];
[UIView setAnimationDuration:.35f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft
     forView:self.window cache:YES];

// change to the new view (make this one hidden, the other not)

[UIView commitAnimations];

【讨论】:

  • 我很欣赏从我的描述中并没有太明显,但我不是在从现场翻转之后。我想翻转我的红色方块的左侧边缘(这已经是一个 UIView)。请参阅以下视频,了解我所追求的那种效果:youtube.com/watch?v=wgoGDPF_NGo
【解决方案3】:

CALayeranchorPoint 的可接受范围是 [0,1]。在您的情况下,您尝试绕 Y 轴翻转,图层的锚点应为 [0, 0.5]。

【讨论】:

  • 我的工作正常,但红色的 UIView 甚至在开始动画之前就发生了变化。我会尝试获取视频。有趣的是,anchorPoint 工作的 -0.3,它围绕 y 轴向左偏移旋转。
  • 添加了一个视频来展示这个问题。在动画的第一次运行中,正方形向右移动了多个像素。似乎无法解决。
猜你喜欢
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多