IOS:touchbegan和touchmoved分为两类

时间:2012-11-13 11:58:47

标签: ios uiimageview touch

在我的班级中,我称之为“一”,我有两种触摸方法: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类中的方法而不是我的主类“一”,为什么?有没有办法识别每个班级的触摸?

1 个答案:

答案 0 :(得分:0)

来自UIResponder touchesBegan方法:

  

此方法的默认实现不执行任何操作。然而   UIR应答器的UIKit子类,特别是UIView,   将消息转发到响应者链。要将邮件转发到   下一个响应者,将消息发送到super(超类   实现);不要将消息直接发送到下一个   响应者。例如,

因此,要为您要传递响应者链的事件添加[super touchesBegan:touches withEvent:event];到您的touchesBegan方法(以及另一个触及方法)。

您还应该实施touchesEndedtouchesCanceled方法。