如何将2层放在相同的bezier路径上而不隐藏另一层

时间:2014-02-23 15:33:23

标签: ios calayer uibezierpath

我创建了2个相同大小的CALayer,并将它们传递给下面的方法。但是,这两个层一起运行。我怎样才能将它们分开以便两者都可见?

- (void) myAnimation : (CALayer *) sublayer {
    UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(30, 100, 270, 270)];

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anim.path = aPath.CGPath;
    anim.rotationMode = kCAAnimationRotateAuto;
    anim.repeatCount = HUGE_VALF;
    anim.duration =35.0;
    [sublayer addAnimation:anim forKey:@"race"];
}

1 个答案:

答案 0 :(得分:0)

您的路径在同一点开始和结束。假设两个开始时间相同且持续时间相同,则您的图层将精确重叠。您可以更改开始时间或轮换UIBezierPath* aPath以下是旋转UIBezierPath* aPath并更改持续时间的示例。它应该让你知道如何改变持续时间,开始时间,轮换等等。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor blackColor]];
    CALayer * layer1 = [CALayer layer];
    CALayer * layer2 = [CALayer layer];
    [layer1 setFrame:CGRectMake(0, 0, 5, 50)];
    [layer2 setFrame:CGRectMake(0, 0, 5, 100)];
    layer1.backgroundColor = [[UIColor colorWithRed:.1 green:.5 blue:1 alpha:.35] CGColor];
    layer2.backgroundColor = [[UIColor colorWithRed:.9 green:.2 blue:.5 alpha:.35] CGColor];
    [self.view.layer addSublayer:layer1];
    [self.view.layer addSublayer:layer2];
    [self myAnimation:layer1 andRotation:0 andDuration:35.0];
    [self myAnimation:layer2 andRotation:10 andDuration:10.0];
}
- (void) myAnimation:(CALayer *)sublayer andRotation:(CGFloat)rot andDuration:(CFTimeInterval)dur {
    CGRect rect = CGRectMake(30, 100, 270, 270);
    UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:rect];
    // Creating a center point around which we will transform the path
    CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformTranslate(transform, center.x, center.y);
    transform = CGAffineTransformRotate(transform, rot);
    // Recenter the transform
    transform = CGAffineTransformTranslate(transform, -center.x, -center.y);
    // This is the new path we will use.
    CGPathRef rotated =  CGPathCreateCopyByTransformingPath(aPath.CGPath, &transform);
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anim.path = rotated;
    anim.rotationMode = kCAAnimationRotateAuto;
    anim.repeatCount = HUGE_VALF;
    anim.duration =dur;
    [sublayer addAnimation:anim forKey:@"race"];
}