0

在我称之为“one”的课堂上,我有两种触摸方法:touchbegan 和 touchmoved。在这个类中,我以这种方式分配一个 imageview:

imageView = [[ImageToDrag alloc] initWithImage:[UIImage imageNamed:@"machine.png"]];
    imageView.center = CGPointMake(905, 645);
    imageView.userInteractionEnabled = YES;
    [self addSubview:imageView];
    [imageView release];

在.m的这个类(ImageToDrag)中,我有:

    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // When a touch starts, get the current location in the view
    currentPoint = [[touches anyObject] locationInView:self];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Get active location upon move
    CGPoint activePoint = [[touches anyObject] locationInView:self];

    // Determine new point based on where the touch is now located
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                 self.center.y + (activePoint.y - currentPoint.y));

    //--------------------------------------------------------
    // Make sure we stay within the bounds of the parent view
    //--------------------------------------------------------
  float midPointX = CGRectGetMidX(self.bounds);
    // If too far right...
  if (newPoint.x > self.superview.bounds.size.width  - midPointX)
    newPoint.x = self.superview.bounds.size.width - midPointX;
    else if (newPoint.x < midPointX)    // If too far left...
    newPoint.x = midPointX;

    float midPointY = CGRectGetMidY(self.bounds);
  // If too far down...
    if (newPoint.y > self.superview.bounds.size.height  - midPointY)
    newPoint.y = self.superview.bounds.size.height - midPointY;
    else if (newPoint.y < midPointY)    // If too far up...
    newPoint.y = midPointY;

    // Set new center location
    self.center = newPoint;
}

所以我的问题是这样的:在 ImageToDrag 类中而不是在我的主类“one”中触摸识别方法,为什么?有没有办法在每个班级中识别触摸?

4

1 回答 1

0

UIResponder touchesBegan方法:

此方法的默认实现什么也不做。然而,UIResponder 的直接 UIKit 子类,尤其是 UIView,将消息转发到响应者链。要将消息转发给下一个响应者,请将消息发送给 super(超类实现);不要将消息直接发送给下一个响应者。例如,

因此,将要向上传递响应者链的事件添加[super touchesBegan:touches withEvent:event];到您的方法(和其他触摸方法)中。touchesBegan

您还应该实现touchesEndedandtouchesCanceled方法。

于 2012-11-13T13:39:17.230 回答