問題描述
我的觀點結構:
UITableView
UITableViewCell
UIScrollView
CustomView
UIButton
問題是當我觸摸 UIButton 時它不起作用.我用代碼創建它:
The problem is UIButton doesn't work when I touch it. I create it with code:
btn = [[UIButton alloc] init];
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tileTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
btn.layer.zPosition = 1;
[self addSubview:btn];
我也添加了這個:
scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;
但我的按鈕無論如何都不起作用.我無法為 UITableView 取消ContentTouches 或設置延遲,因為如果我設置它,UITableView 將停止垂直滾動.
But my button doesn't work anyway. I can't cancelContentTouches or set delay for UITableView because if I set it, UITableView stop scrolling verticaly.
感謝您的幫助!
推薦答案
創建UIScrollview的子類
Create a subclass of UIScrollview with
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return NO;
}
如果您沒有收到該消息,請設置:
If you are not getting that message, set:
scrollView.delaysContentTouches = NO;
問題在于 touchesShouldCancelInContentView
默認的行為方式,僅當子視圖是 UIControl 時它才返回 NO,但您的按鈕隱藏在 CustomView 中,但不是.
The problem is because of the way touchesShouldCancelInContentView
behaves by default, it returns NO only if the subview is an UIControl, but your button is hidden inside the CustomView which isn't.
這篇關于UIButton 在 UIScrollView 中不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!