問(wèn)題描述
我有一個(gè)應(yīng)用程序,我的主視圖同時(shí)接受 touchesBegan
和 touchesMoved
,因此接受單指觸摸和拖動(dòng).我想實(shí)現(xiàn)一個(gè) UIScrollView
,我讓它工作,但它覆蓋了拖動(dòng),因此我的 contentView 永遠(yuǎn)不會(huì)收到它們.我想實(shí)現(xiàn)一個(gè) UIScrollview
,其中兩指拖動(dòng)表示滾動(dòng),而單指拖動(dòng)事件會(huì)傳遞給我的內(nèi)容視圖,因此它可以正常執(zhí)行.我需要?jiǎng)?chuàng)建自己的 UIScrollView
子類嗎?
I have an app where my main view accepts both touchesBegan
and touchesMoved
, and therefore takes in single finger touches, and drags. I want to implement a UIScrollView
, and I have it working, but it overrides the drags, and therefore my contentView never receives them. I'd like to implement a UIScrollview
, where a two finger drag indicates a scroll, and a one finger drag event gets passed to my content view, so it performs normally. Do I need create my own subclass of UIScrollView
?
這是我實(shí)現(xiàn) UIScrollView
的 appDelegate
中的代碼.
Here's my code from my appDelegate
where I implement the UIScrollView
.
@implementation MusicGridAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize scrollView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
//[application setStatusBarHidden:YES animated:NO];
//[window addSubview:viewController.view];
scrollView.contentSize = CGSizeMake(720, 480);
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.delegate = self;
[scrollView addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[scrollView release];
[window release];
[super dealloc];
}
推薦答案
你需要繼承 UIScrollView(當(dāng)然!).然后你需要:
You need to subclass UIScrollView (of course!). Then you need to:
制作單指事件以轉(zhuǎn)到您的內(nèi)容視圖(簡(jiǎn)單),并且
make single-finger events to go to your content view (easy), and
讓兩指事件滾動(dòng)滾動(dòng)視圖(可能容易,可能很難,可能不可能).
make two-finger events scroll the scroll view (may be easy, may be hard, may be impossible).
Patrick 的建議通常很好:讓您的 UIScrollView 子類了解您的內(nèi)容視圖,然后在觸摸事件處理程序中檢查手指的數(shù)量并相應(yīng)地轉(zhuǎn)發(fā)事件.只需確保 (1) 您發(fā)送到內(nèi)容視圖的事件不會(huì)通過(guò)響應(yīng)者鏈返回到 UIScrollView(即確保處理所有事件),(2) 尊重通常的觸摸事件流(即 touchesBegan,而不是一些 {touchesBegan, touchesMoved, touchesEnded},以 touchesEnded 或 touchesCancelled 結(jié)束),尤其是在處理 UIScrollView 時(shí).#2 可能很棘手.
Patrick's suggestion is generally fine: let your UIScrollView subclass know about your content view, then in touch event handlers check the number of fingers and forward the event accordingly. Just be sure that (1) the events you send to content view don't bubble back to UIScrollView through the responder chain (i.e. make sure to handle them all), (2) respect the usual flow of touch events (i.e. touchesBegan, than some number of {touchesBegan, touchesMoved, touchesEnded}, finished with touchesEnded or touchesCancelled), especially when dealing with UIScrollView. #2 can be tricky.
如果您決定事件是針對(duì) UIScrollView,另一個(gè)技巧是讓 UIScrollView 相信您的兩指手勢(shì)實(shí)際上是單指手勢(shì)(因?yàn)?UIScrollView 不能用兩根手指滾動(dòng)).嘗試只將一根手指的數(shù)據(jù)傳遞給 super(通過(guò)過(guò)濾 (NSSet *)touches
參數(shù)——注意它只包含更改的觸摸——并完全忽略錯(cuò)誤手指的事件).
If you decide the event is for UIScrollView, another trick is to make UIScrollView believe your two-finger gesture is actually a one-finger gesture (because UIScrollView cannot be scrolled with two fingers). Try passing only the data for one finger to super (by filtering the (NSSet *)touches
argument — note that it only contains the changed touches — and ignoring events for the wrong finger altogether).
如果這不起作用,你就有麻煩了.從理論上講,您可以嘗試通過(guò)創(chuàng)建一個(gè)看起來(lái)類似于 UITouch 的類來(lái)創(chuàng)建人工觸摸以提供給 UIScrollView.底層 C 代碼不檢查類型,因此將 (YourTouch *) 轉(zhuǎn)換為 (UITouch *) 可能會(huì)起作用,并且您將能夠欺騙 UIScrollView 處理實(shí)際上并沒(méi)有發(fā)生的觸摸.
If that does not work, you are in trouble. Theoretically you can try to create artificial touches to feed to UIScrollView by creating a class that looks similar to UITouch. Underlying C code does not check types, so maybe casting (YourTouch *) into (UITouch *) will work, and you will be able to trick UIScrollView into handling the touches that did not really happen.
您可能想閱讀 我關(guān)于高級(jí) UIScrollView 技巧的文章(并完全看一些不相關(guān)的 UIScrollView 示例代碼在那里).
You probably want to read my article on advanced UIScrollView tricks (and see some totally unrelated UIScrollView sample code there).
當(dāng)然,如果你不能讓它工作,總是可以選擇手動(dòng)控制 UIScrollView 的移動(dòng),或者使用完全自定義編寫(xiě)的滾動(dòng)視圖.Three20 庫(kù)中有 TTScrollView 類;用戶感覺(jué)不好,但程序員感覺(jué)不錯(cuò).
Of course, if you can't get it to work, there's always an option of either controlling UIScrollView's movement manually, or use an entirely custom-written scroll view. There's TTScrollView class in Three20 library; it does not feel good to the user, but does feel good to programmer.
這篇關(guān)于使用 UIScrollView 用兩根手指滾動(dòng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!