【问题标题】:Handling Touch Events on iOS devices在 iOS 设备上处理触摸事件
【发布时间】:2013-01-21 09:52:28
【问题描述】:

我正在探索在 iOS 设备上处理触摸事件的方式。我参考了这本书:iOS Open Application Development。为此,我重写了 mouseDown、mouseUp 方法并简单地在警告框中显示坐标。然而,被覆盖的方法永远不会被调用。下面是从 UIView 派生的 newView 类的代码:

-(void) _mouseDown: (GSEventRef) event{
CGPoint point =GSEventGetLocationInWindow(event);
UIAlertView* alert;
NSString* alertString =[NSString stringWithFormat: @"%f %f", point.x, point.y];
alert = [[UIALertView alloc] initWithTitle:@"Success" message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}

-(BOOL) canHandleGestures{
return YES;
}

有没有我遗漏的部分?另外,是否有其他方法可以获取有关触摸事件的信息并进行处理?

【问题讨论】:

    标签: iphone ios ios4 gesture-recognition iphone-privateapi


    【解决方案1】:

    更好的起点是这里: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/multitouch_background/multitouch_background.html

    这些是您可以在 UIView 子类中覆盖的方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    

    【讨论】:

      【解决方案2】:

      你可以使用:

      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
      
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
      
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
      
      - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}
      

      用于从 UIView 子类获取触摸事件。

      【讨论】:

        【解决方案3】:

        您必须使用gesturerecognizer(如uitapgesturerecognizer、uiswipegesturerecognizer 或UITouch events)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-13
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多