當按鈕超過了父視圖範圍,點選是沒有反應的。因為消息的傳遞是從最下層的父視圖開始調用hittest方法。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
return view;
}
當存在view時才會傳遞對應的event,現在點選了父視圖以外的範圍,自然傳回的是nil。是以當子視圖(比如按鈕btn)因為一些原因超出了父視圖範圍,就要重寫 hittest方法,讓其傳回對應的子視圖,來接收事件。
<span style="font-family:FangSong_GB2312;font-size:18px;">- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
CGPoint tempoint = [yourBtn convertPoint:point fromView:self];
if (CGRectContainsPoint(yourBtn.bounds, tempoint))
{
view = yourBtn;
}
}
return view;
}</span>