問題描述
我有一個應用程序,我的主視圖同時接受 touchesBegan
和 touchesMoved
,因此接受單指觸摸和拖動.我想實現(xiàn)一個 UIScrollView
,我讓它工作,但它覆蓋了拖動,因此我的 contentView 永遠不會收到它們.我想實現(xiàn)一個 UIScrollview
,其中兩指拖動表示滾動,而單指拖動事件會傳遞給我的內容視圖,因此它可以正常執(zhí)行.我需要創(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
?
這是我實現(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(當然!).然后你需要:
You need to subclass UIScrollView (of course!). Then you need to:
制作單指事件以轉到您的內容視圖(簡單),并且
make single-finger events to go to your content view (easy), and
讓兩指事件滾動滾動視圖(可能容易,可能很難,可能不可能).
make two-finger events scroll the scroll view (may be easy, may be hard, may be impossible).
Patrick 的建議通常很好:讓您的 UIScrollView 子類了解您的內容視圖,然后在觸摸事件處理程序中檢查手指的數(shù)量并相應地轉發(fā)事件.只需確保 (1) 您發(fā)送到內容視圖的事件不會通過響應者鏈返回到 UIScrollView(即確保處理所有事件),(2) 尊重通常的觸摸事件流(即 touchesBegan,而不是一些 {touchesBegan, touchesMoved, touchesEnded},以 touchesEnded 或 touchesCancelled 結束),尤其是在處理 UIScrollView 時.#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.
如果您決定事件是針對 UIScrollView,另一個技巧是讓 UIScrollView 相信您的兩指手勢實際上是單指手勢(因為 UIScrollView 不能用兩根手指滾動).嘗試只將一根手指的數(shù)據(jù)傳遞給 super(通過過濾 (NSSet *)touches
參數(shù)——注意它只包含更改的觸摸——并完全忽略錯誤手指的事件).
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).
如果這不起作用,你就有麻煩了.從理論上講,您可以嘗試通過創(chuàng)建一個看起來類似于 UITouch 的類來創(chuàng)建人工觸摸以提供給 UIScrollView.底層 C 代碼不檢查類型,因此將 (YourTouch *) 轉換為 (UITouch *) 可能會起作用,并且您將能夠欺騙 UIScrollView 處理實際上并沒有發(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.
您可能想閱讀 我關于高級 UIScrollView 技巧的文章(并完全看一些不相關的 UIScrollView 示例代碼在那里).
You probably want to read my article on advanced UIScrollView tricks (and see some totally unrelated UIScrollView sample code there).
當然,如果你不能讓它工作,總是可以選擇手動控制 UIScrollView 的移動,或者使用完全自定義編寫的滾動視圖.Three20 庫中有 TTScrollView 類;用戶感覺不好,但程序員感覺不錯.
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.
這篇關于使用 UIScrollView 用兩根手指滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!