0

I have an one view with horizontal and vertical beizer paths.The resulting view looks like the foloowingenter image description here

Now on touch of the view if user has touched the one of the path,I have to change the color of that path by animation that means on touch of a path ,path's storke color will be changed animaticllay.

I am able to do it with the normal way ,that means when user touch the path I am able to change the color but not with the animation effect.

I have created the sub class of UIBeizerPath with properties like defaultStorkeColor,SelectedStorkeColor and I am manipulating them in the drawrect.

My drawrect is

-(void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();


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

    }
    else 
    {
        [path.defaultStrokeColor set];   
    }

    [path stroke];


}

}

Please help me in implementing that.

Ajit
  • 47
  • 6

1 Answers1

1

I didn't try this code but hit with a same scenario before, which I had resolved by doing something like the below:

In -drawRect draw these patterns over a CAShapeLayer using UIBezierPath and add it to your CustomView's layer

[self.layer addSubLayer:self.shapeLayer_main];

Then get the frame of the rect you wish to draw the stroke using

-(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];    
}

In the method,

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

Every time you tap the view, a rect is drawn with the stroke color you gave with the animation.

Ram Gandhi
  • 707
  • 8
  • 24