【问题标题】:How To Apply Complex CALayer Transform Changes?如何应用复杂的 CALayer 变换更改?
【发布时间】:2015-06-22 17:59:53
【问题描述】:

我需要UIView 的复杂连续动画,其中涉及设置需要计算的CATransform3D 旋转和平移属性,因此无法选择标准动画。

我转而使用CALayer 动画。还有这个:

self.displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(update:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

- (void)update:(CADisplayLink*)sender
{
    CGFloat elapsedTime = sender.timestamp - self.lastTimestamp;
    self.lastTimestamp = sender.timestamp;

    self.rotation += elapsedTime * 0.1;  // Factor determines speed.

    // This is just example for SO post; the real animation is more complicated!
    CATransform3D transform;
    transform = CATransform3DMakeRotation(self.rotation, 1.0, 0.0, 0.0);
    self.imageLayer.transform = transform;
}

这里的self.imageLayer 是一个CALayer,它的contents 已经设置了一个图像并作为子层添加到我的基本视图中。

它确实会旋转“一点”,但不是连续旋转,有时它似乎会停止或向后旋转一点。

分配一个新的transform 似乎经常没有任何效果,因为self.rotation 的值增加了很多。添加[self setNeedsDisplay] 没有帮助。

我还没有对CALayers 做太多事情,所以我想我错过了一些非常基本的东西。试图找到它一段时间,但我找到的所有示例似乎都离我想要的太远了。

【问题讨论】:

  • @GeneratorOfOne 旋转速度必须是动画的;随着时间的推移,它必须放慢速度。那么这个方法正确吗?我应该使用NSTimer吗?

标签: ios core-animation calayer catransform3d cadisplaylink


【解决方案1】:

当您创建自己的图层然后修改其属性时,它会隐式地为它们设置动画(请参阅“Animating Simple Changes to a Layer’s Properties”。您看不到预期的更改,因为 Core Animation 正在为更改设置动画而不是立即应用它们。

您需要禁用隐式动画。有几种方法可以禁用它;见“Changing a Layer’s Default Behavior”。或search stack overflow for “disable implicit animations”。这是一种方法:

NSDictionary *actions = @{ @"transform": [NSNull null] };
self.imageLayer.actions = actions;

只需执行一次,创建imageLayer

【讨论】:

  • 这是我一直在想的。但是因为没想到​​隐式动画,所以继续搜索(徒劳)。惊人的;非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
相关资源
最近更新 更多