背景视图的旋转动画与前景中的按钮重叠

时间:2014-12-09 17:01:24

标签: ios objective-c cocoa-touch core-animation

我创建了一个动画来切换位置。动画工作正常,但它会破坏前景中的按钮。

该按钮不应受影响。造成这种情况的原因是什么?

enter image description here

video

 CGFloat speedFactor = 3.0;
 CABasicAnimation *slotAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
 [slotAnimation setToValue:[NSNumber numberWithFloat:M_PI_2]];
 [slotAnimation setRemovedOnCompletion:NO];
 [slotAnimation setFillMode:kCAFillModeBoth];
 slotAnimation.duration = .3 / speedFactor;
 [[self.fromPositionTextField layer] addAnimation:slotAnimation forKey:@"slotAnimation"];

 CABasicAnimation *slotAnimation2 = [slotAnimation copy];
 [slotAnimation2 setBeginTime:CACurrentMediaTime()+0.3 / speedFactor];
 [[self.toPositionTextField layer] addAnimation:slotAnimation2 forKey:@"slotAnimation"];
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.6 / speedFactor * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     NSArray *positions = @[self.viewModel.fromPosition, self.viewModel.toPosition];
     self.viewModel.fromPosition = positions[1];
     self.viewModel.toPosition = positions[0];

     CABasicAnimation *slotAnimation3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
     [slotAnimation3 setToValue:[NSNumber numberWithFloat: M_PI *.125/4.0]];
     [slotAnimation3 setRemovedOnCompletion:NO];
     [slotAnimation3 setFillMode:kCAFillModeBoth];
     slotAnimation3.duration = .3 / speedFactor;
     [slotAnimation3 setBeginTime:CACurrentMediaTime()+0.3 / speedFactor];
     [[self.fromPositionTextField layer] addAnimation:slotAnimation3 forKey:@"slotAnimation2"];

     CABasicAnimation *slotAnimation4 = [slotAnimation3 copy];
     [slotAnimation4 setBeginTime:CACurrentMediaTime()+0.6 /speedFactor];
     [[self.toPositionTextField layer] addAnimation:slotAnimation4 forKey:@"slotAnimation2"];

     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2/speedFactor * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [self.fromPositionTextField.layer removeAllAnimations];
         [self.toPositionTextField.layer removeAllAnimations];

     });
 });

1 个答案:

答案 0 :(得分:1)

尝试使用按钮图层的zPosition属性(它是CALayer对象)来更改按钮的z-index。

myButton.layer.zPosition = 100;

较大的值是“在顶部”。默认值为0.

您需要导入QuartzCore框架才能访问该层。

[edit]将答案更改为100,只是复制而不是阅读评论