【问题标题】:Finding touch point in Objective c/cocoa touch在 Objective c/cocoa touch 中寻找接触点
【发布时间】:2009-10-01 05:34:02
【问题描述】:

我无法找到接触点。

我有一个函数 loadView(),我在其中设置了 16 个图块(16 个 UIImageView)。

然后在函数中:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Retrieve the touch point

    UITouch *touch=[[event allTouches]anyObject];
    CGPoint point= [touch locationInView:touch.view];
    CGRect frame = [self frame];  // Error this Line
}

我已使用 frame-origin 来识别按下了哪些框架/图块。

但是这一行:

CGRect frame = [self frame];

让我发疯。请有人告诉我该怎么做。(解释和为什么不工作)。请。

【问题讨论】:

  • 你为什么不告诉我们你得到什么样的错误?另外,告诉我们这个方法在哪个类中
  • 是的,您需要提供更多详细信息,我们才能解决您的问题。

标签: iphone objective-c uiview uitouch


【解决方案1】:

使用这个:

UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:self.view];

【讨论】:

    【解决方案2】:

    您似乎正试图在 UIViewController 子类的方法中执行此操作。该行应该是:

    [self.view frame];
    

    【讨论】:

      【解决方案3】:

      我仍然不清楚你想要什么,但让我们看看这是否对你有帮助。

      如果您要移动不同的对象,那么为什么不创建另一个类并使用 UIView 继承它并在子类中定义触摸功能。 示例标题

      ...
      Tile : UIView
      ...
      

      示例类实现

      ...
      -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
      
      }
      
      -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
          UITouch *touch = [touches anyObject];
          CGPoint touchPoint = [touch locationInView:[self superview]];
      
      }
      
      -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
          UITouch *touch = [touches anyObject];
          CGPoint touchPoint = [touch locationInView:[self superview]];
          self.center = touchPoint;
      }
      ...
      

      或者,如果您只想要 UIViewController 中的触摸点。

      UITouch *touch = [touches anyObject];
      CGPoint touchPoint = [touch locationInView:[self.view]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        相关资源
        最近更新 更多