두 개의 하위 뷰 (다른 하나의 상단 (topChild
) 중 하나 (bottomChild
))가있는 뷰 (parent
)가 있습니다.형제 하위 뷰는 iPhone에서 접촉하지 않습니다.
화면을 탭하면 topChild
과 parent
만 터치 이벤트를 수신합니다.
터치 이벤트를 bottomChild
으로 전파하려면 어떻게해야합니까?
코드 : MYView
만 touchesBegan
를 기록 UIView
의 서브 클래스가
- (void)viewDidLoad
{
[super viewDidLoad];
MYView* parent = [[MYView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
parent.tag = 3;
parent.backgroundColor = [UIColor redColor];
MYView* bottomChild = [[MYView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
bottomChild.tag = 2;
bottomChild.backgroundColor = [UIColor blueColor];
[parent addSubview:bottomChild];
MYView* topChild = [[MYView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
topChild.tag = 1;
topChild.backgroundColor = [UIColor greenColor];
[parent addSubview:topChild];
[self.view addSubview:parent];
}
.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%d", self.tag);
[super touchesBegan:touches withEvent:event];
}
결과 :
TouchTest[25062:f803] 1
TouchTest[25062:f803] 3
내 첫번째 생각은 parent
이 아이들에게 모든 touchesSomething
통화를 전파하는 것이었다 :
녹색 영역을 터치하면 다음과 같은 로그를 생성 하지만 (A) 더 쉬운 해결책이있을 거라고 생각합니다. n 및 (B) 어떤 자녀가 부모에게 이벤트를 보냈는지 모르겠으며 같은보기에 두 번 메시지 을 보내면 속임수가 발생할 수 있습니다.
질문을 한 후 나는이 post을 찾은 후 hitTest
을 덮어 쓰면 터치를받는보기가 변경됩니다. 이 접근법을 시도하고 그것이 효과가 있다면 질문을 업데이트 할 것입니다.
감사 아담을 설정! 이 솔루션은 제스처 인식기와 호환 될 수 있습니까?언뜻 보면이 길로 가면 휠을 다시 구현해야합니다. – hpique
그리고 나는 물건을 재 구조화하는 것을 아주 좋아할 것이지만, 나의 경우에는 어떻게 보이지 않습니다. topChild는 투명도를 가지고 있습니다. topChild와 bottomChild는 "움직일"수 있습니다 (따라서 topChild는 bottomChild의 하위 뷰일 수 없습니다). 둘 다 접촉에 응답해야합니다. – hpique
콜백에 의해 수신 된 gestureRecognizer 객체를 간단한 터치가 아닌 부모에게 전달하는 것이 가능해야합니다. 적어도이 프로젝트에서는 UIPinchGestureRecognizer에서 유용 할 것입니다. –