2014-02-23 74 views
1

我已经创建了2个相同大小的CALayers,并将这些CALayers传递给下面的方法。但是,这两层一起运行。我怎样才能分开这些,以便两者都可见?如何把2层按照相同的bezierpath没有一个隐藏另一个

- (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"]; 
} 
+0

你有没有试过给他们一个不同的'beginTime'? –

+0

在两个方法调用与这些层之间有一个延迟。 – santhu

+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"]; 
} 
相关问题