問(wèn)題描述
我的觀點(diǎn)結(jié)構(gòu):
UITableView
UITableViewCell
UIScrollView
CustomView
UIButton
問(wèn)題是當(dāng)我觸摸 UIButton 時(shí)它不起作用.我用代碼創(chuàng)建它:
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];
我也添加了這個(gè):
scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;
但我的按鈕無(wú)論如何都不起作用.我無(wú)法為 UITableView 取消ContentTouches 或設(shè)置延遲,因?yàn)槿绻以O(shè)置它,UITableView 將停止垂直滾動(dòng).
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.
感謝您的幫助!
推薦答案
創(chuàng)建UIScrollview的子類(lèi)
Create a subclass of UIScrollview with
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return NO;
}
如果您沒(méi)有收到該消息,請(qǐng)?jiān)O(shè)置:
If you are not getting that message, set:
scrollView.delaysContentTouches = NO;
問(wèn)題在于 touchesShouldCancelInContentView
默認(rèn)的行為方式,僅當(dāng)子視圖是 UIControl 時(shí)它才返回 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.
這篇關(guān)于UIButton 在 UIScrollView 中不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!