Objective c 如何在特定索引处添加动画层
我将向视图添加两个CAText层,并为其中一个设置动画。我想在另一层上设置一个层的动画,但在动画完成之前,它无法在层层次中正确定位。谁能看出我做错了什么?动画工作,它只是在“topcharlayer2”后面运行,直到动画完成Objective c 如何在特定索引处添加动画层,objective-c,core-animation,Objective C,Core Animation,我将向视图添加两个CAText层,并为其中一个设置动画。我想在另一层上设置一个层的动画,但在动画完成之前,它无法在层层次中正确定位。谁能看出我做错了什么?动画工作,它只是在“topcharlayer2”后面运行,直到动画完成 - (CABasicAnimation *)topCharFlap { CABasicAnimation *flipAnimation; flipAnimation = [CABasicAnimation animationWithKeyPath:@"transform
- (CABasicAnimation *)topCharFlap
{
CABasicAnimation *flipAnimation;
flipAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
flipAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(1.57f, 1, 0, 0)];
flipAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0, 1, 0, 0)];
flipAnimation.autoreverses = NO;
flipAnimation.duration = 0.5f;
flipAnimation.repeatCount = 10;
return flipAnimation;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setBackgroundColor:[UIColor clearColor]]; //makes this view transparent other than what is drawn.
[self initChar];
}
return self;
}
static CATransform3D CATransform3DMakePerspective(CGFloat z)
{
CATransform3D t = CATransform3DIdentity;
t.m34 = - 1. / z;
return t;
}
-(void) initChar
{
UIFont *theFont = [UIFont fontWithName:@"AmericanTypewriter" size:FONT_SIZE];
self.layer.sublayerTransform = CATransform3DMakePerspective(-1000.0f);
topHalfCharLayer2 = [CATextLayer layer];
topHalfCharLayer2.bounds = CGRectMake(0.0f, 0.0f, CHARACTERS_WIDTH, 100.0f);
topHalfCharLayer2.string = @"R";
topHalfCharLayer2.font = theFont.fontName;
topHalfCharLayer2.fontSize = FONT_SIZE;
topHalfCharLayer2.backgroundColor = [UIColor blackColor].CGColor;
topHalfCharLayer2.position = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
topHalfCharLayer2.wrapped = NO;
topHalfCharLayer1 = [CATextLayer layer];
topHalfCharLayer1.bounds = CGRectMake(0.0f, 0.0f, CHARACTERS_WIDTH, 100.0f);
topHalfCharLayer1.string = @"T";
topHalfCharLayer1.font = theFont.fontName;
topHalfCharLayer1.fontSize = FONT_SIZE;
topHalfCharLayer1.backgroundColor = [UIColor redColor].CGColor;
topHalfCharLayer1.position = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
topHalfCharLayer1.wrapped = NO;
//topHalfCharLayer1.zPosition = 100;
[topHalfCharLayer1 setAnchorPoint:CGPointMake(0.5f,1.0f)];
[[self layer] addSublayer:topHalfCharLayer1 ];
[[self layer] insertSublayer:topHalfCharLayer2 atIndex:0];
[topHalfCharLayer1 addAnimation:[self topCharFlap] forKey:@"anythingILikeApparently"];
}
包含此代码的视图由loadView中的视图控制器加载。在视图的initWithFrame方法中调用initChar方法。目标是iOS4。我没有使用SetWantLayer,因为我读到iOS中的UIView是自动分层备份的,不需要这样做。我想到了几个想法:
- 在开始动画之前,请尝试将“R”层添加到层层次
- 使用
添加它,然后使用[[self layer]addSublayer:topHalfCharLayer1],而不是在索引1处插入“T”层
[[self layer]insertSublayer:topHalfCharLayer2 atIndex:0]为“R”层进行插入代码>
- 你有没有试过玩ZZ层的位置?这决定了层的视觉外观。它实际上不会改变图层顺序,但会改变它们的显示方式——例如,哪个图层在哪个图层的前面/后面
- 我还建议您删除动画代码,直到对层视图顺序进行排序。完成后,动画应该可以正常工作
致以最诚挚的问候。来自quartz dev apple邮件列表: 通常在2D情况下,addSublayer将在 以前的不过,我相信这个执行机制是可行的 独立于zPosition,可能只是使用 画家的算法。但是当你加上zPositions和3D的时候,我不知道 我认为你可以完全依靠图层排序。但我其实不清楚 如果苹果保证在你没有设置的情况下 Z在图层上放置位置,但具有3D变换矩阵集
因此,在对层应用3D变换时,似乎必须明确设置zPosition 你好-谢谢你的回复!我已经编辑了上面的代码。无论何时/何地插入图层,似乎都没有什么不同。zPosition确实会产生差异,但如果我设置了一个高值,层会因我设置的透视图而收缩。如果我删除动画,层层次结构是正确的。其他人提到不应该将CALayers与UIView混合。这就是我在做的吗?UIView的图层属性在某种程度上是特殊的吗?UIView都有一个背景图层。这是一个CALayer,所以说你不应该混合CALayer和UIView是胡说八道。没有UIView,您不能使用CALayer。图层特性的特殊之处在于它是视图的根图层。它有一些特殊的特性,但在大多数情况下,它的工作原理与任何其他层相同。您的CATTransferM3dMakePerspective方法是什么样子的?你是在执行平移还是仅仅是缩放?再次感谢-我已经添加了上面的其他方法。你不能只设置一个zposition,这个zposition非常小,这样就不会对透视变换产生太大影响吗?因为当涉及到构图时,常规的“CALayer”基本上是2D的事情,所以你不必担心3D变换“穿过”另一层或任何东西的层。
/* Insert 'layer' at position 'idx' in the receiver's sublayers array.
* If 'layer' already has a superlayer, it will be removed before being
* inserted. */
open func insertSublayer(_ layer: CALayer, at idx: UInt32)