問(wèn)題描述
我有一個(gè)滾動(dòng)視圖,當(dāng)它上面沒(méi)有按鈕時(shí)會(huì)滾動(dòng).現(xiàn)在確實(shí)如此,并且當(dāng)拖動(dòng)鼠標(biāo)(在模擬器上)時(shí),什么也沒(méi)有發(fā)生(我認(rèn)為是因?yàn)榘粹o被按下了).我怎樣才能做到這一點(diǎn)?
I have a scroll view that used to scroll when it didn't have buttons all over it. Now it does, and when dragging the mouse (on simulator) nothing happens (i think because the buttons are being pushed). How can I make this right?
推薦答案
發(fā)生這種情況是因?yàn)?UIScrollView
的 UIButton
子視圖(我假設(shè)按鈕作為子視圖添加到您的case) 正在跟蹤觸摸而不是滾動(dòng)視圖.UIScrollView
方法 touchesShouldCancelInContentView
是這里的關(guān)鍵.根據(jù)其描述:如果view不是UIControl
對(duì)象,則默認(rèn)返回YES;否則返回NO
.",即對(duì)于UIControl
對(duì)象(按鈕),UIScrollView
不會(huì)嘗試取消阻止?jié)L動(dòng)的觸摸.
This is happening because UIButton
subviews of the UIScrollView
(I assume buttons are added as subviews in your case) are tracking the touches and not the scroll view. UIScrollView
method touchesShouldCancelInContentView
is the key here. According to its description: "The default returned value is YES if view is not a UIControl
object; otherwise, it returns NO
.", i.e. for UIControl
objects (buttons), UIScrollView
does not attempt to cancel touches which prevents scrolling.
所以,要允許使用按鈕滾動(dòng):
So, to allow scrolling with buttons:
- 確保
UIScrollView
屬性canCancelContentTouches
設(shè)置為YES
. - 子類(lèi)
UIScrollView
并覆蓋touchesShouldCancelInContentView
以在內(nèi)容視圖對(duì)象是UIButton
時(shí)返回YES
,如下所示:
- Make sure
UIScrollView
propertycanCancelContentTouches
is set toYES
. - Subclass
UIScrollView
and overridetouchesShouldCancelInContentView
to returnYES
when content view object is aUIButton
, like this:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
if ( [view isKindOfClass:[UIButton class]] ) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
}
這篇關(guān)于拖動(dòng)按鈕時(shí)ScrollView不滾動(dòng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!