1

On UIView there are number of CALayers. Each CALayer consists of CAShapeLayers. CAShapeLayers' path property consists of UIBezierPath.

My objective is when i tap on CALayer i want to get points which i used in the drawing of UIBezierPath. For that i thought of subclassing CALayer which has NSMutableArray to store points. I will be saving points when i am drawing in particular CALayer. So whenever i tap on particular CALayer i will get points associated to that. Also i want to give tag to each CALayer. I do not know how to do this.

The second way is to use CALayer's inbuilt property which will give me points captured in CALayer. But i am not aware of this kind of property.

Please share your ideas how to accomplish this task?

Harsh
  • 666
  • 1
  • 10
  • 21

1 Answers1

2

You will need to use the UIView that contains the CALayer to handle the touch events, there is no built in Touch events for CALayers. - (CALayer *)hitTest:(CGPoint)thePoint returns the "deepest" CALayer that the point from the touch event is within. So, if you call [self.layer hitTest:point] it will check all your sublayers and return the correct CALayer

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
    CALayerSubclass *taplayer = [self.layer hitTest:point]
    NSArray *points = [taplayer getPoints];
}

There is no way to get out of a CGPath the points you fed into it. The best you can do are these methods about getting info from the path. So, like you said, your best bet is to subclass CALayer and put all the info you need in a data structure to retrieve later.

// .h
@interface CALayerSubclass : CALayer

@property (nonatomic, strong) NSMutableArray *points;

@end

// .m
-(void)drawInContext:(CGContextRef)ctx {
    ...
    [bezierPath addCurveToPoint:controlPoint1:point1 controlPoint2:point2];
    [points addObject:[NSValue valueWithCGPoint:point1]];  
    [points addObject:[NSValue valueWithCGPoint:point2]];  
    ...
}

Just store all the CGPoints (or most other Core Graphics structure) in an NSValue and throw it into an array.

Kevin
  • 3,111
  • 1
  • 19
  • 26
  • I actually want to retrieve all the points from CALayer which contains path drawing. How to do that? – Harsh Jan 20 '12 at 05:20
  • Whoops, sorry about that. Misread your question. Hope my edits help. – Kevin Jan 20 '12 at 14:10
  • Note that -drawInContext: does not get called in CAShapeLayer. – Proud Member Nov 06 '12 at 18:20
  • Care to elaborate? It isn't explicitly called but if used properly, it would be after calling 'setNeedsDisplay' on a CALayerSubclass object. – Kevin Nov 06 '12 at 20:47
  • @ProudMember -drawInContext does get called in CAShapeLayer if you set its frame size (width/height) to non zero values – artkoenig Feb 27 '13 at 10:19