Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何扩大UIGestureRecognitor的命中区域?_Iphone_Ios_Ipad_Cocoa Touch_Uikit - Fatal编程技术网

Iphone 如何扩大UIGestureRecognitor的命中区域?

Iphone 如何扩大UIGestureRecognitor的命中区域?,iphone,ios,ipad,cocoa-touch,uikit,Iphone,Ios,Ipad,Cocoa Touch,Uikit,我在一些视图上使用了一些手势识别器,但有时视图太小,很难找到。使用识别器是必要的,因此如何扩大命中区域?如果您是为自定义的UIView执行此操作,您应该能够覆盖hitTest:withEvent:方法: - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGRect frame = CGRectInset(self.bounds, -20, -20); return CGRectContainsPoi

我在一些视图上使用了一些手势识别器,但有时视图太小,很难找到。使用识别器是必要的,因此如何扩大命中区域?

如果您是为自定义的
UIView
执行此操作,您应该能够覆盖
hitTest:withEvent:
方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGRect frame = CGRectInset(self.bounds, -20, -20);

    return CGRectContainsPoint(frame, point) ? self : nil;
}

上述代码将在视图周围添加20点边框。点击该区域中的任何位置(或视图本身上)将表示点击。

如果您使用
UIImageView
作为按钮,则可以使用以下扩展名(Swift 3.0):

扩展UIImageView{ 打开覆盖函数hitTest(uPoint:CGPoint,带有事件:UIEvent?->UIView{ 如果self.ishiden | | | |!self.isUserInteractionEnabled | | self.alpha<0.01{返回nil} let minimumHitArea=CGSize(宽:50,高:50) 让buttonSize=self.bounds.size 让widthToAdd=最大值(minimumHitArea.width-buttonSize.width,0) let heightToAdd=最大值(最小HitArea.height-按钮大小.height,0) 设largerFrame=self.bounds.insertby(dx:-宽度到添加/2,dy:-高度到添加/2) //在较大的帧上执行命中测试 返回(大帧包含(点))?自身:无 } }
类似于@rmaddy应答的
ui按钮
扩展

Swift版本:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    let frame = self.bounds.insetBy(dx: -20, dy: -20);
    return frame.contains(point) ? self : nil;
}

那么,你是在问如何放大视图?不,我当然知道如何放大视图框架:)我需要放大命中帧,而不影响其真实帧。我认为最好重写pointInside,因为hitTest在子视图上递归调用pointInside:。错误:扩展中的声明不能重写yet@MatthewBarker请参见此处的答案:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    let frame = self.bounds.insetBy(dx: -20, dy: -20);
    return frame.contains(point) ? self : nil;
}