在我称之为“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”中触摸识别方法,为什么?有没有办法在每个班级中识别触摸?