iOS 關於UIView的userInteractionEnabled屬性(多層檢視到點選事件)
阿新 • • 發佈:2019-02-12
如果父檢視為ParentView包含一個Button,如果再ParentView上新增子檢視ChildView,且ChildView蓋住了Button,那麼Button就得到不響應了,為了讓Button響應,可以設定ChildView的userInteractionEnabled = NO;最近被這個問題困擾了很久,開始想用事件傳遞的方法,重寫類繼承自UIView,最後被這簡單屬性搞定了!如果想要給子檢視新增點選事件可參看程式碼:
:singleTap];
selfatIndex:_curPage];
UIView *v = [_curViewsobjectAtIndex:i];
v.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:self
action:@selector(handleTap:)];
[v addGestureRecognizer
[singleTap release];
v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0);
[_scrollViewaddSubview:v];
- (void)handleTap:(UITapGestureRecognizer *)tap {
if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
[_delegatedidClickPage:
}
點選事件可用代理模式來做,即重新寫了加入子試圖到點選事件。
}