2012-11-13 43 views
0

在我稱之爲「one」的類中,我有兩種觸摸方法:touchbegan和touchmoved。 在這個類的Alloc我這樣一個ImageView的:在這個類(ImageToDrag)IOS:touchbegan和touchmoved在兩個類

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

在.M我:

- (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類中的方法而不是在我的主要課程「一」,爲什麼?有沒有辦法在每個班級中識別觸摸?

回答

0

UIRespondertouchesBegan方法:

此方法的默認實現不執行任何操作。但是,UIResponder的立即UIKit子類,特別是UIView, 將消息轉發給響應者鏈。將消息轉發給 下一個響應者,將消息發送給super(超類 實現);不要將消息直接發送給下一個響應者 。例如,

所以添加[super touchesBegan:touches withEvent:event];touchesBegan方法(和其他觸摸的方法)要傳遞了響應鏈事件。

您還應該實施touchesEndedtouchesCanceled方法。