ios - UIGestureRecognizer 只为 subView 添加,但它也适用于 parentView?

标签 ios objective-c uigesturerecognizer uibezierpath cashapelayer

这是我的代码:

[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);



}

所以它只能在 colorView 上工作,但发生的是 enter image description here ,那个触摸也在 self.view 里面工作,如何解决这个问题。

最佳答案

您可以添加:

 self.colorView.clipsToBounds = YES;

它将解决您的问题。

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

所以因为它不是。因此交互将扩展到 super View 。当您设置为 YES 时。它只适用于您的 subview 。

关于ios - UIGestureRecognizer 只为 subView 添加,但它也适用于 parentView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690263/

相关文章:

ios - 如何在 iOS 中创建多行多列的按钮?

iOS7:Tableview 不加载附件更改

android - 使用 Phonegap/Cordova 相机插件从相机或图库中选择照片

iphone - 推送通知在 iOS 5.0.1 上不起作用

ios - 当手指离开 View 时停止 UILongPressGestureRecognizer

ios - 将 enumerateObjectsUsingBlock 转换为快速枚举 - Swift

ios - 创建更好的面向对象设计

objective-c - 设置 IBOutletCollection 的图层属性

iphone - 添加平移手势识别器后,我的按钮不会保持突出显示状态?

iphone - UILongPressGestureRecognizer 不删除 View