0

如何爲cashapelayer設置漸變填充顏色? 有更清楚的解釋相關問題: Using Cocoa To Follow A Path With A Gradient如何在不使用遮罩的情況下爲cashapelayer設置漸變填充顏色?

我需要一個漸變,不是面具,而是基於cashapelayer的路徑圖上的梯度。

我不能在頂部使用漸變遮罩,因爲我在我的遊戲中製作小地圖上的路線。所以如果玩家走過他自己的軌道,它應該是不同的顏色。

我希望像這樣的MapView的折線:
來源:http://cdn4.raywenderlich.com/wp-content/uploads/2014/06/23_multicolor_polyline.png

我所做出的小地圖路線: 記錄用戶的所有不同的方向,然後通過一個循環到貝塞爾路徑運行它們。

我附加了Bezier路徑,然後把它放在一個cashapelayer上。

在cashapelayer中有多彩的方法嗎?

是否有可以放置漸變的cabasicanimation關鍵路徑?

我的代碼在下面,還有一些圖片。

[mymapview.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)]; 
[[mymapview subviews] 
makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
int i = 0; 
int x = 17; 
int y = 272; 
int m = 16; 
UIBezierPath *kpath = [UIBezierPath bezierPath]; while (i < HistDirections.count) 
{ 
    if (i > 0) 
    { 
      UIBezierPath *path = [UIBezierPath bezierPath]; 
      [path moveToPoint:CGPointMake(x, y)]; 
      if ([[HistDirections objectAtIndex:i] intValue] ==1) 
      { 
       [path addLineToPoint:CGPointMake(x, y-m)]; 
       y = y - m; 
      } 
      else if ([[HistDirections objectAtIndex:i] intValue] ==2) 
      { 
       [path addLineToPoint:CGPointMake(x-m, y)]; 
       x = x -m; 
      } 
      else if ([[HistDirections objectAtIndex:i] intValue] ==3) 
      { 
       [path addLineToPoint:CGPointMake(x+m, y)]; 
       x = x+m; 
      } 
      else 
      { 
       [path addLineToPoint:CGPointMake(x, y+m)]; 
       y = y - m; 
      } 
     [kpath appendPath:path]; 
    } 
    i++; 
} 
[CATransaction begin]; 
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
[CATransaction setCompletionBlock:^{ 
    UIImageView *viewpulse = [[UIImageView alloc] initWithFrame:CGRectMake(x -5, y-5, 10.0, 10.0)]; 
    viewpulse.image = [UIImage imageNamed:@"arro.png"]; 
    viewpulse.backgroundColor = [UIColor clearColor]; 
    if(direction == 1) 
    { 
     viewpulse.transform = CGAffineTransformMakeRotation(-M_PI/2); 
    } 
    else if (direction == 2) 
    { 
     viewpulse.transform = CGAffineTransformMakeRotation(M_PI); 
    } 
    else if (direction == 4) 
    { 
     viewpulse.transform = CGAffineTransformMakeRotation(M_PI/2); 
    } 
    [mymapview addSubview:viewpulse]; 
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
    scaleAnimation.duration = 0.8; 
    scaleAnimation.repeatCount = HUGE_VAL; 
    scaleAnimation.autoreverses = YES; 
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.6]; 
    scaleAnimation.toValue = [NSNumber numberWithFloat:0.8]; 
    [viewpulse.layer addAnimation:scaleAnimation forKey:@"scale"]; 
}]; 
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 

kpath.lineCapStyle = kCGLineCapRound; 
kpath.lineCapStyle = kCGLineJoinRound; 

shapeLayer.path = [kpath CGPath]; 

shapeLayer.strokeColor = [[UIColor colorWithRed:51/255.0f green:(51)/255.0f blue:170/255.0f alpha:1.0f] CGColor]; 
shapeLayer.lineWidth = 4.0; 
shapeLayer.lineCap = kCALineCapRound; 
shapeLayer.fillColor = [[UIColor clearColor] CGColor]; 
[mymapview.layer addSublayer:shapeLayer]; 

CABasicAnimation *HAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
float dur = (HistDirections.count * 0.27); 
if (dur > 2) 
{ 
    dur = 2; 
} 
HAnimation.duration   = dur; 
HAnimation.repeatCount   = 1.0; 
HAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
HAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 



/* 
CAGradientLayer *gradientLayer = [CAGradientLayer layer]; 
gradientLayer.frame = mymapview.frame; 
gradientLayer.colors = @[(__bridge id)[UIColor blueColor].CGColor,(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor orangeColor].CGColor, (__bridge id)[UIColor redColor].CGColor]; 
gradientLayer.startPoint = CGPointMake(0,0.5); 
gradientLayer.endPoint = CGPointMake(1,0.5); 

[mymapview.layer addSublayer:gradientLayer]; 
gradientLayer.mask = shapeLayer;*/ 
[CATransaction commit]; 

梯度面膜:

單色線:

回答

1

沒關係!我想清楚我能做什麼。由於我從多個路徑創建圖層,因此我只是將cg路徑放入數組中,並將每個路徑循環到一個具有自己顏色的獨特cashapelayer中

相關問題