問題描述
我有一個滾動視圖,當(dāng)它上面沒有按鈕時會滾動.現(xiàn)在確實(shí)如此,并且當(dāng)拖動鼠標(biāo)(在模擬器上)時,什么也沒有發(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) 正在跟蹤觸摸而不是滾動視圖.UIScrollView
方法 touchesShouldCancelInContentView
是這里的關(guān)鍵.根據(jù)其描述:如果view不是UIControl
對象,則默認(rèn)返回YES;否則返回NO
.",即對于UIControl
對象(按鈕),UIScrollView
不會嘗試取消阻止?jié)L動的觸摸.
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.
所以,要允許使用按鈕滾動:
So, to allow scrolling with buttons:
- 確保
UIScrollView
屬性canCancelContentTouches
設(shè)置為YES
. - 子類
UIScrollView
并覆蓋touchesShouldCancelInContentView
以在內(nèi)容視圖對象是UIButton
時返回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)于拖動按鈕時ScrollView不滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!