代码之家  ›  专栏  ›  技术社区  ›  Jukurrpa

iPhone-UIViewController中的触摸滑动手柄

  •  2
  • Jukurrpa  · 技术社区  · 14 年前

    处理这些事件的最佳方法是什么?使我的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;
    

    目标是让NewViewController的scrollView调用NewViewController的TouchsBegind和TouchsEnded方法。

    更新: ScrollViewSuite帮助我了解了如何处理触摸回调。 UIScrollView 只在触摸不到滚动时才给他们打电话。

    所以,有三种情况:

    • 倾斜滑动(即使是轻微的,只要滚动条出现):没有 touches 方法被调用
    • touchesBegan touchesMoved touchesEnded 被称为
    • , touchesCancelled 被称为;

    通过设置 UIScrollView公司 canCancelContentTouches FALSE 已调用,这意味着对于第一种情况仍然不会调用touchs方法。

    但是 UIScrollView公司 有一个名为delaysContentTouches的属性,该属性设置为 TRUE 错误的

    对我来说最简单的方法是什么(如果你不能使用 UIGestureRecognizer UIScrollView公司 ,设置其 delayContentTouches 错误的 和覆盖功能( touchesShouldBegin touchesShouldCancel 以及 接触开始了 / 触摸移动

    2 回复  |  直到 14 年前
        1
  •  6
  •   Endemic    14 年前

    UIPanGestureRecognizer UISwipeGestureRecognizer . 实例化适当的对象,使用 [addGestureRecognizer:] 方法并在viewController中实现委托方法。


    iPhone Event Handling Guide: Gesture Recognizers
    UIGestureRecognizer Reference (抽象超类)
    UIGestureRecognizerDelegate Protocol Reference

        2
  •  1
  •   falconcreek    14 年前

    创建一个UIScrollView add作为窗口的子视图,然后创建一个具有滑块的viewController实例,并将其添加到scrollView的子视图中。

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[window bounds]];
    [window addSubView:scrollView];
    UIViewController *vc = [[UIViewController alloc] initWithNibNamed:@"MyVC" bundle:nil];
    [scrollView addSubView:[vc view]];
    

    我怀疑您没有收到事件是由于IB中缺少连接,或者scrollView正在将事件转发到newViewController视图的superview。

    在苹果文档中 UIResponder nextResponder

    UIResponder类不会自动存储或设置下一个响应程序,而是默认返回nil。子类必须重写此方法以设置下一个响应程序。UIView通过返回管理它的UIViewController对象(如果有)或它的superview(如果没有)来实现这个方法; UIWindow返回application对象,UIApplication返回nil。

    有几件事要尝试。

    1. 在NewViewController.m中,重写可以成为第一响应程序
      
      - (BOOL)canBecomeFirstResponder
      {
          return YES;
      }
      
    2. 因为您只是将新视图添加为子视图,而不是使用导航或模态视图,所以请确保newViewController的superview不再是第一个响应者。添加:
      
      // View creation and adding to parent
      NewViewController* newViewController = [[newViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
      [self.view addSubview:newViewController.view];
      [self.view resignFirstResponder];
      
    3. 确保“NextViewController”视图已连接到IB中的文件所有者。
    4. 确保视图的userInteractionEnabled属性为YES

    看一看这个 ScrollViewSuite 还有示例代码。

    在其他答案中还讨论了新的消化器的样品。注意,guesturizer被添加到视图中,guesturizer的处理被委托给另一个对象(通常是视图的控制器)

    您声明按钮和标签是作为IB中scrollview的子视图添加的。似乎您应该使用UIView作为按钮和标签的容器。只是一个想法,因为我无法从代码片段中分辨出来。

    UIScrollView
      -> UIView
          ->UIButton
          ->UILabel
          ..
          ..