iPhone:如何动画uibezierpath设置笔触颜色?

时间:2013-04-04 06:29:21

标签: iphone uibezierpath stroke cashapelayer

我有一个带有水平和垂直beizer路径的视图。结果视图看起来像下面的enter image description here

现在触摸视图如果用户触摸了其中一条路径,我必须通过动画更改该路径的颜色,这意味着在触摸路径时,路径的storke颜色将被更改为animaticllay。

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

我已经使用defaultStorkeColor,SelectedStorkeColor等属性创建了UIBeizerPath的子类,并且我在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 个答案:

答案 0 :(得分:1)

我没有尝试过这段代码,但之前使用的是同样的方案,我通过执行以下操作解决了这个问题:

-drawRect中使用CAShapeLayerUIBezierPath上绘制这些模式,并将其添加到CustomView的{​​{1}}

layer

然后使用

获取想要绘制笔划的矩形框
[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];    
}

每次点击视图时,都会使用动画中的笔触颜色绘制矩形。