【问题标题】:Why is CABasicAnimation not animating to the correct point?为什么 CABasicAnimation 没有动画到正确的点?
【发布时间】:2014-09-15 02:00:08
【问题描述】:

我正在向我的应用程序添加一个 containerView。此视图视图和图层位于正常的 0,0 点和 320,480 处。没有什么令人兴奋或与众不同的。

所以我将它添加到我的视图中,但我想通过它的图层在屏幕上对其进行动画处理。再次没有什么可发疯的。但我希望我添加的视图的这个视图/图层位于 20,160 点。所以从左边到窗口的一半(窗口的下半部分)有 20 个像素。我想我会将 CALayer 的 animateToPoint 设置为 20,160。但它不起作用。

下面是我的代码,它确实将添加的视图/图层设置为从左侧到屏幕中间的 20 像素。问题是,我传递给它的点是 20,160 是完全错误的,但它有效(我不得不通过试错来移动它)。我的问题是为什么 20,160 不适用于 CABasicaAnimation 点?为什么我要输入这么多的数字?

    // Place the container view
        self.callDetailView.view.frame = CGRectMake(20, 163, 280, 263);
        self.callDetailView.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        [self.view addSubview:self.callDetailView.view];
        [self addChildViewController:self.callDetailView];
        [self.callDetailView didMoveToParentViewController:self];

        [self.callDetailView configureDetails:[[STRequestManager sharedInstance] shouldGetCurrentRequest]];
        CGPoint myNewEndPoint = CGPointMake(160.0,  263.0); // <-- I would think this should be 20,160
        CABasicAnimation *slideIn = [CABasicAnimation animationWithKeyPath:@"position"];
        slideIn.duration = 0.5;
        [slideIn setToValue:[NSValue valueWithCGPoint:myNewEndPoint]];
        [slideIn setFromValue:[NSValue valueWithCGPoint:CGPointMake(160.0,  663.0)]];
        [[self.callDetailView.view layer] addAnimation:slideIn forKey:@"mySlideInAnimation"];
        [[self.callDetailView.view layer] setPosition:myNewEndPoint];

//更新

现在我看 160,263 是垂直中心点和水平中心点。我要添加的视图是 280,所以一半是 140。140 加上我左边的 20 是 160。CABasicAnimation 是否为中心设置动画?

【问题讨论】:

    标签: ios objective-c calayer cabasicanimation


    【解决方案1】:

    默认情况下,图层的AnchorPoint 为 (0.5, 0.5),即居中。因此,您的动画沿中心移动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 2013-01-31
      • 2011-08-23
      • 2018-08-26
      相关资源
      最近更新 更多