gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:40:20 25 4
gpt4 key购买 nike

这是我的代码:

[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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com