2013-04-04 66 views
0

我有一个一个视图与水平和垂直beizer paths.The所得视图看起来像foloowing enter image description hereiphone:如何动画uibeizserpath设置笔触颜色?

现在关于视图的触摸如果用户触摸了路径的一个,我必须要改变的,所述颜色通过动画的路径,这意味着在路径的触摸,路径的存储颜色将被改变animaticllay。

我能够用正常的方式做到这一点,这意味着当用户触摸路径我可以改变颜色,但不能与动画效果。

我已经创建了UIBeizerPath的子类,其属性类似于defaultStorkeColor,SelectedStorkeColor,我正在操作它们的drawrect。

我的drawRect是

-(void)drawRect:(CGRect)rect 

{

CGContextRef context = UIGraphicsGetCurrentContext(); 


for(BCBeizerPath * path in pathsArray) 
{ 
    if(path.selected) 
    { 
     [path.selectedStrokeColor set]; 

    } 
    else 
    { 
     [path.defaultStrokeColor set]; 
    } 

    [path stroke]; 


} 

}

请帮我执行这项。

回答

1

我没有尝试这个代码,但有相同的方案之前,我已经通过执行类似下面的解决击:

-drawRect使用UIBezierPathCAShapeLayer绘制这些图案,并把它添加到您的CustomViewlayer

[self.layer addSubLayer:self.shapeLayer_main]; 

然后得到的矩形的框架,你想提请使用

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    UITouch *touch = [[event allTouches] anyObject];  
    CGPoint touchLocation = [touch locationInView:self];  
    //Check in which rect in your view contains the touch location  
    //Then call the method to draw the stroke with the detected rect as the parameter  
    [self drawStrokeWithRect:touchedRect];  
} 
行程

在该方法中,

-(void)drawStrokeWithRect:(CGRect)touchedRect { 
//Create a shapeLayer with desired strokeColor, lineWidth and path(A UIBezierPath) 
//This is the layer your are going to animate 
//Add this shapeLayer to your self.shapeLayer_main added to the customView's layer 
CAShapeLayer * shapeLayer = [CAShapeLayer layer]; 
shapeLayer.lineWidth = 4; 
shapeLayer.strokeColor = strokeColor.CGColor; 
shapeLayer.fillColor = transparentColor.CGColor; 
shapeLayer.path = [[UIBezierPath bezierPathWithRect:touchedRect]CGPath]; 
[self.shapeLayer_main addSublayer:shapeLayer]; 

//Adding Animation to the ShapeLayer 
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
drawAnimation.duration = 1.0; 
drawAnimation.removedOnCompletion = NO; 
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 

[shapeLayer addAnimation:drawAnimation forKey:@"StrokeAnimation"]; 
} 

每当你点击的观点,一个矩形画有你的动画给了笔触颜色。