美文网首页
iOS 扩大有效触摸 区域

iOS 扩大有效触摸 区域

作者: woo_5857 | 来源:发表于2023-03-07 09:12 被阅读0次

1.扩大有效 点击区域

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {

    CGRectbounds =self.bounds;

    //扩大原热区直径至26,可以暴露个接口,用来设置需要扩大的半径。

    CGFloatwidthDelta =600;

    CGFloatheightDelta =600;

    bounds =CGRectInset(bounds, -0.5* widthDelta, -0.5* heightDelta);

    return CGRectContainsPoint(bounds, point);

}

2.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    CGRect rectRange = CGRectInset(self.bounds, -30.0, -30.0);

    if(CGRectContainsPoint(rectRange, point)) {

        returnself;

    }else{

        returnnil;

    }

    return self;

}

//有效触摸 区域

- (BOOL)pointInside:(CGPoint)pointwithEvent:(UIEvent*)event {

    CGFloatminx =self.startTouchView.frame.origin.x;

    CGFloat maxx = CGRectGetMaxX(self.endTouchView.frame);

    if(!self.isSelect) {

        minx =CGRectGetMaxX(self.startTouchView.frame);

        maxx =self.endTouchView.frame.origin.x;

    }

    returnCGRectContainsPoint([selfHitTestingBounds:self.boundsminx:minxmaxx:maxx], point);

}

- (CGRect)HitTestingBounds:(CGRect)boundsminx:(CGFloat)minxmaxx:(CGFloat)maxx {

    CGRecthitTestingBounds = bounds;

    hitTestingBounds.origin.x= minx;

    hitTestingBounds.size.width= maxx - minx;

    returnhitTestingBounds;

}

UITableView  点击事件被拦截处理

UITableView 拥有属于自己的点击事件,在将一个UITableView 的控件放在其它视图上, 并且其它视图需要添加手势进行操作的情况下,我们会发现我们点击UITableView的cell的时候, 并没有出发方法:

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath; 是直接进入到了手势的方法中。 这是由于手势的冲突引起的,解决方法是调用UIGestureRecognizer的大力方法:-(BOOL)gestureRecognizer:

//手势单击事件- (void) addTapGestureRecognizer:(void(^)(UITapGestureRecognizer* gesture))block {        self.tapGestureEvent = block; 

  UITapGestureRecognizer* noticeRecognizer;   

noticeRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(apGestureClick:)];    noticeRecognizer.numberOfTapsRequired = 1; // 单击 

  [noticeRecognizer setDelegate:self]; 

  [self addGestureRecognizer:noticeRecognizer];

    [self setUserInteractionEnabled:YES];

}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件

    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {

        returnNO;

    }

    return  YES;

}

相关文章

网友评论

      本文标题:iOS 扩大有效触摸 区域

      本文链接:https://www.haomeiwen.com/subject/qxqtoctx.html