2014-02-23 18 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"]; 
} 
相關問題