IOS:TouchStart和TouchMove分为两个类
在我称之为“一”的课堂上,我有两种触摸方法:touchStart和touchmoved。 在该类中,I以以下方式分配imageview:IOS:TouchStart和TouchMove分为两个类,ios,uiimageview,touch,Ios,Uiimageview,Touch,在我称之为“一”的课堂上,我有两种触摸方法:touchStart和touchmoved。 在该类中,I以以下方式分配imageview: imageView = [[ImageToDrag alloc] initWithImage:[UIImage imageNamed:@"machine.png"]]; imageView.center = CGPointMake(905, 645); imageView.userInteractionEnabled = YES; [s
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;
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
//当触摸开始时,获取视图中的当前位置
currentPoint=[[Touchs anyObject]locationInView:self];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
//移动时获得活动位置
CGPoint activePoint=[[toucheanyobject]locationInView:self];
//根据触摸屏现在所在的位置确定新点
CGPoint newPoint=CGPointMake(self.center.x+(activePoint.x-currentPoint.x),
self.center.y+(activePoint.y-currentPoint.y));
//--------------------------------------------------------
//确保我们保持在父视图的范围内
//--------------------------------------------------------
float midPointX=CGRectGetMidX(self.bounds);
//如果太对了。。。
if(newPoint.x>self.superview.bounds.size.width-midPointX)
newPoint.x=self.superview.bounds.size.width-midPointX;
else if(newPoint.xself.superview.bounds.size.height-midPointY)
newPoint.y=self.superview.bounds.size.height-midPointY;
else if(newPoint.y
所以我的问题是:在ImageToDrag类中,而不是在我的主类“one”中,触摸识别方法,为什么?是否有一种方法可以识别每个类中的触摸?来自
UIResponder
touchesbreated
方法:
此方法的默认实现不执行任何操作。然而
UIResponder的直接UIKit子类,特别是UIView,
将消息转发到响应程序链上。将消息转发给
下一个响应者,将消息发送到super(超类
实施);不要将消息直接发送到下一个
响应者。比如说,
因此添加[super touchesbreated:toucheevent:event]
到您的touchsbegind
方法(以及其他touchs方法),用于您希望传递给响应者链的事件
您还应该实现touchesend
和touchecanceled
方法