使用UIScrollView用两根手指滚动 [英] Scrolling with two fingers with a UIScrollView

查看:117
本文介绍了使用UIScrollView用两根手指滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用,我的主视图接受 touchesBegan touchesMoved ,因此接受单指触摸和拖拉。我想实现一个 UIScrollView ,我有它的工作,但它覆盖了drags,因此我的contentView永远不会收到它们。我想实现 UIScrollview ,其中双指拖动表示滚动,单指拖动事件传递到我的内容视图,因此它正常执行。我是否需要创建自己的 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?

这是我的 appDelegate 中的代码,我实现了 UIScrollView

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子类了解你的内容视图,然后在触摸事件处理程序中检查手指的数量并相应地转发事件。只需确保(1)您发送到内容视图的事件不会通过响应程序链回送到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不能用两个手指滚动)。尝试仅将一个手指的数据传递给超级(通过过滤(NSSet *)触摸参数 - 请注意它只包含已更改的触摸 - 并忽略错误手指的事件完全)。

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).

如果这不起作用,你就麻烦了。从理论上讲,您可以尝试通过创建类似于UITouch的类来创建人工触摸以提供给UIScrollView。底层C代码不会检查类型,因此可能会将(YourTouch *)转换为(UITouch *),并且您将能够欺骗UIScrollView来处理实际没有发生的触摸。

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用两根手指滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆