iPhone - 处理UIViewController中的触摸幻灯片

Juk*_*rpa 2 iphone cocoa-touch uiscrollview uiviewcontroller

我想在UIViewController子类中处理触摸(特别是水平幻灯片),但问题是控制器的视图包含覆盖整个屏幕的UIScrollView,因此从不调用"touchesBegan:"和"touchesEnded:"事件在我的控制器中.scrollview只能垂直滚动.

什么是使它能够处理这些事件的最佳方法?让我的UIViewController也继承UIScrollView并根据需要处理触摸(并删除当前的滚动视图),或使用UIScrollView子类代替我当前的滚动视图,它将调用我需要的情况的委托方法?或许还有另一种方式?

这是我目前的代码:

// View creation and adding to parent
NewViewController* newViewController = [[newViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.view addSubview:newViewController.view];

// NewViewController.h
@interface NewViewController : UIViewController {
    IBOutlet UIScrollView*  scrollView;

    // other outlets and properties
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
Run Code Online (Sandbox Code Playgroud)

在IB中,NewViewController的视图包含scrollView,其中包含所有其他插座(标签,按钮)......目标是让NewViewController的scrollView调用NewViewController的touchesBegan和touchesEnded方法.

更新: ScrollViewSuite帮助我理解了如何处理触摸回调.UIScrollView只有在触摸不能滚动时才调用它们.

那么,有三种情况:

  • 倾斜滑动(一旦滚动条出现,即使稍微滑动):不touches调用任何方法
  • 完美的横向滑动(未出现滚动条)touchesBegan,touchesMovedtouchesEnded被称为
  • 任何触摸不引起随后稍微上下滑动的滚动(触摸不移动,或水平移动): touchesBegan,touchesMovedtouchesCancelled是调用;

可以通过设置UIScrollView's canCancelContentTouches来避免触摸取消,FALSE但这显然只有touchesBegan在被调用时才有效,这意味着第一种情况下仍然不会调用触摸方法.

但是UIScrollView有一个名为delaysContentTouches的属性TRUE,默认设置为,并且当触摸开始时它会等待一点,以便查看它是否会让他滚动.如果设置为FALSE,则触摸开始后立即调用touches方法,并允许子类按照自己的意愿处理它们.

TL; DR:对我来说似乎最简单的方法(如果你不能使用UIGestureRecognizer)是子类UIScrollView,根据你的需要设置它的delayContentTouches属性FALSE并覆盖touches函数(touchesShouldBegin/ touchesShouldCancel以及touchesBegan/ touchesMoved...).

End*_*mic 6

如果您正在为iPhone OS 3.2或更高版本构建,则可以使用Gesture Recognizer对象.根据您的描述,听起来您需要UIPanGestureRecognizerUISwipeGestureRecognizer.实例化适当的对象,使用该[addGestureRecognizer:]方法将其添加到viewController的视图中,并在viewController中实现委托方法.

供参考:
iPhone事件处理指南:手势识别器
UIGestureRecognizer参考(抽象超类)
UIGestureRecognizerDelegate协议参考