2 개의 뷰가 있지만 1 개의 뷰를 (사실상) 크게 만들고 싶습니다. v1에 tapGesture를 놓으면 탭 제스쳐가 더 큰 히트 영역 인 으로 작동하지만 v2에 내 tapGesture를 배치하면 실제로 작동하지 않습니다 (실제로는 원래 경계 내에서가 아니라 tapGesture를 전혀 인식하지 못합니다.) 비록 내 TestView1 hittest 메서드를 통해 루프 및 포인트 프레임에 포함 된 얻을.hitTest : WithEvent 및 Subview
#import "ViewController.h"
@interface TestView1 : UIView
@end
@implementation TestView1
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat radius = 100.0;
CGRect frame = CGRectMake(0, 0,
self.frame.size.width + radius,
self.frame.size.height + radius);
if (CGRectContainsPoint(frame, point)) {
return self;
}
return nil;
}
@end
@interface TestView2 : UIView
@end
@implementation TestView2
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat radius = 100.0;
CGRect frame = CGRectMake(0, 0,
self.frame.size.width + radius,
self.frame.size.height + radius);
if (CGRectContainsPoint(frame, point)) {
return self;
}
return nil;
}
@end
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
TestView1 *v1 = [[TestView1 alloc] initWithFrame:CGRectMake(50.f, 50.f, 100.f, 100.f)];
[self.view addSubview:v1];
TestView2 *v2 = [[TestView2 alloc] initWithFrame:CGRectMake(0.f, 0.f, 100.f, 100.f)];
v2.backgroundColor = UIColor.yellowColor;
[v1 addSubview:v2];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[v2 addGestureRecognizer:gesture];
}
- (void) panGesture:(UIPanGestureRecognizer *)recognizer
{
NSLog(@"tap");
}
@end
샘 코드는 터치 영역을 오른쪽 아래로 확장합니다. CGFloat 패딩 = 50; CGRect frame = CGRectMake (-padding, -padding, self.frame.size.width + padding, self.frame.size.height + padding); ' – Nuthatch
@Nuthatch Andy의 원래 코드도 마찬가지입니다. 그러나 좋은 지적 – LoPoBo