0

Here is my code :

[super viewDidLoad];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    [self.colorView setUserInteractionEnabled:YES];
    [self.colorView addGestureRecognizer:tapGesture];

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [[event allTouches] anyObject];
    touchPoint = [touch locationInView:self.colorView];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(touchPoint.x,touchPoint.y)];
    [path addLineToPoint:CGPointMake(startingPoint.x,startingPoint.y)];
    startingPoint=touchPoint;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
    shapeLayer.lineWidth = 3.0;
    shapeLayer.fillColor = [[UIColor redColor] CGColor];
    [self.colorView.layer addSublayer:shapeLayer];

    NSLog(@"Touch moving point =x : %f Touch moving point =y : %f", touchPoint.x, touchPoint.y);



}

so it have to only work on the colorView but what happens is enter image description here,that touch is working on self.view inside also ,how to solve this .

Arpit Dongre
  • 1,683
  • 19
  • 30
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47

2 Answers2

3

You can add:

 self.colorView.clipsToBounds = YES;

It will resolve your issue.

A Boolean value that determines whether subviews are confined to the bounds of the view.

Declaration OBJECTIVE-C @property(nonatomic) BOOL clipsToBounds Discussion Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.

Availability Available in iOS 2.0 and later. Link: Reference

So because it's NO. So interaction will extent to super view. When you set to YES. It only work on your subview.

vien vu
  • 4,277
  • 2
  • 17
  • 30
0

Views track touches that started inside of them even when the touch goes outside their bounds (that's why you can hold a button, drag out and it'll still be selected while you are within a certain distance)

You can use CGRectContainsPoint to only add a point if it's inside the view.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50