問題描述
我正在嘗試在我的應用程序中創建一個類似跳板的界面.我正在嘗試使用添加到 UIScrollView 的 UIButtons.我遇到的問題是按鈕沒有將任何觸摸傳遞給 UIScrollView - 如果我嘗試輕彈/滑動并碰巧按下按鈕,它不會注冊 UIScrollView,但是如果我輕彈之間的空間按鈕它將起作用.如果我觸摸它們,按鈕會單擊/工作.
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.
是否有強制按鈕將觸摸事件發送到其父級(超級視圖)的屬性或設置?在添加 UIScrollView 之前是否需要將按鈕添加到其他內容?
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
確定點擊進入其內容視圖和觸摸轉動之間的區別滑動或捏合時,它需要延遲觸摸并查看您的手指是否在此延遲期間移動.通過在上面的示例中將 delaysContentTouches
設置為 NO
,您可以防止這種情況發生.因此,滾動視圖總是將觸摸傳遞給按鈕,而不是在用戶執行滑動手勢時取消它.嘗試將 delaysContentTouches
設置為 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
.
從結構上講,將要在滾動視圖中托管的所有視圖添加到公共內容視圖并僅使用該視圖作為滾動視圖的子視圖,這也是一個好主意.
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.
這篇關于帶有 UIButtons 的 UIScrollview - 如何重新創建跳板?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!