0

我有一个带有水平和垂直 beizer 路径的视图。生成的视图看起来像下面的在此处输入图像描述

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

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

我已经创建了具有 defaultStorkeColor、SelectedStorkeColor 等属性的 UIBeizerPath 的子类,并且我在 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];


}

}

请帮助我实施。

4

1 回答 1

1

我没有尝试此代码,但之前遇到了相同的情况,我通过执行以下操作解决了这个问题:

在使用-drawRect中绘制这些模式并将其添加到您的CAShapeLayerUIBezierPathCustomViewlayer

[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"];
}

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

于 2013-09-03T13:53:55.097 回答