0

私が "one" と呼んでいるクラスでは、touchbegan と touchmoved という 2 つのタッチ メソッドがあります。このクラスでは、次の方法でイメージビューを割り当てます。

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;
}

だから私の問題はこれです:私のメインクラス「1」ではなく、ImageToDragクラス内のタッチ認識メソッド、なぜですか?各クラスでタッチを認識する方法はありますか?

4

1 に答える 1

0

UIResponder touchesBeganメソッドから:

このメソッドのデフォルトの実装は何もしません。ただし、UIResponder の直接の UIKit サブクラス、特に UIView は、メッセージをレスポンダー チェーンに転送します。メッセージを次のレスポンダーに転送するには、メッセージを super (スーパークラスの実装) に送信します。メッセージを次の応答者に直接送信しないでください。例えば、

そのため、レスポンダー チェーンに渡したいイベントのメソッド (および他の touches メソッド) に追加[super touchesBegan:touches withEvent:event];します。touchesBegan

touchesEndedおよびメソッドも実装する必要がありtouchesCanceledます。

于 2012-11-13T13:39:17.230 に答える