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