問(wèn)題描述
我正在嘗試在我的應(yīng)用程序中創(chuàng)建一個(gè)類似跳板的界面.我正在嘗試使用添加到 UIScrollView 的 UIButtons.我遇到的問(wèn)題是按鈕沒(méi)有將任何觸摸傳遞給 UIScrollView - 如果我嘗試輕彈/滑動(dòng)并碰巧按下按鈕,它不會(huì)注冊(cè) UIScrollView,但是如果我輕彈之間的空間按鈕它將起作用.如果我觸摸它們,按鈕會(huì)單擊/工作.
I'm trying to create a springboard-like interface within my app. I'm trying to use UIButtons added to a UIScrollView. The problem I'm running in to is with the buttons not passing any touches to the UIScrollView - if I try to flick/slide and happen to press on the button it doesn't register for the UIScrollView, but if I flick the space between buttons it will work. The buttons do click/work if I touch them.
是否有強(qiáng)制按鈕將觸摸事件發(fā)送到其父級(jí)(超級(jí)視圖)的屬性或設(shè)置?在添加 UIScrollView 之前是否需要將按鈕添加到其他內(nèi)容?
Is there a property or setting that forces the button to send the touch events up to its parent (superview)? Do the buttons need to be added to something else before being added the UIScrollView?
這是我的代碼:
//init scrolling area
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 480, 480)];
scrollView.contentSize = CGSizeMake(480, 1000);
scrollView.bounces = NO;
scrollView.delaysContentTouches = NO;
//create background image
UIImageView *rowsBackground = [[UIImageView alloc] initWithImage:[self scaleAndRotateImage:[UIImage imageNamed:@"mylongbackground.png"]]];
rowsBackground.userInteractionEnabled = YES;
//create button
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn.frame = CGRectMake(100, 850, 150, 150);
btn.bounds = CGRectMake(0, 0, 150.0, 150.0);
[btn setImage:[self scaleAndRotateImage:[UIImage imageNamed:@"basicbutton.png"]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
//add "stuff" to scrolling area
[scrollView addSubview:rowsBackground];
[scrollView addSubview:btn];
//add scrolling area to cocos2d
//this is just a UIWindow
[[[Director sharedDirector] openGLView] addSubview:scrollView];
//mem-mgmt
[rowsBackground release];
[btn release];
[scrollView release];
推薦答案
為了讓 UIScrollView
確定點(diǎn)擊進(jìn)入其內(nèi)容視圖和觸摸轉(zhuǎn)動(dòng)之間的區(qū)別滑動(dòng)或捏合時(shí),它需要延遲觸摸并查看您的手指是否在此延遲期間移動(dòng).通過(guò)在上面的示例中將 delaysContentTouches
設(shè)置為 NO
,您可以防止這種情況發(fā)生.因此,滾動(dòng)視圖總是將觸摸傳遞給按鈕,而不是在用戶執(zhí)行滑動(dòng)手勢(shì)時(shí)取消它.嘗試將 delaysContentTouches
設(shè)置為 YES
.
In order for UIScrollView
to determine the difference between a click that passes through to its content view(s) and a touch that turns into a swipe or pinch, it needs to delay the touch and see if your finger has moved during that delay. By setting delaysContentTouches
to NO
in your above example, you're preventing this from happening. Therefore, the scroll view is always passing the touch to the button, instead of canceling it when it turns out that the user is performing a swipe gesture. Try setting delaysContentTouches
to YES
.
從結(jié)構(gòu)上講,將要在滾動(dòng)視圖中托管的所有視圖添加到公共內(nèi)容視圖并僅使用該視圖作為滾動(dòng)視圖的子視圖,這也是一個(gè)好主意.
It might also be a good idea, structurally, to add all the views to be hosted in your scroll view to a common content view and only use that one view as the scroll view's subview.
這篇關(guān)于帶有 UIButtons 的 UIScrollview - 如何重新創(chuàng)建跳板?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!