Madhubalan Madhubalan - 1 year ago 83
iOS Question

Add space between UIimageview layer's border and content

Hi i have the CALayer with content has images. Now i need to add the Tap gesture on it. How is is it possible please give any example for add Tap for CALayer.

This is my code:

CALayer *imageLayer=[CALayer layer];
imageLayer.borderColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1].CGColor;
CALayer *imagecontent=[CALayer layer];
imagecontent.frame=CGRectMake(16, 26, 153, 153);
UIImage *image=[self.pageImages objectAtIndex:page];
imagecontent.masksToBounds = YES;
[imageLayer addSublayer:imagecontent];
[self.scrollView.layer addSublayer:imageLayer];

Here i want add Tap gesture for "image content" please help me.

Answer Source

You cannot add gesture recognizers to layers, so you need to add a gesture recognizer to its containing view, and then perform an additional test to see if the tap happens to overlap the layer in question.

Add tap gesture recognizer to the parent view of the layer (let's assume that it's called parentView), then add this code to the selector of your gesture recognizer:

- (void)handleTap:(UITapGestureRecognizer *)sender {
    CGRect layerFrame = CGRectMake(16, 26, 153, 153);
    CGPoint tapPoint = [sender locationInView:parentView];
    if (CGRectContainsPoint(layerRect, tapPoint)) {
        // The tap happened inside the rectangle of your layer