Objective c 拖动触摸开始后创建的UIView
我有几个UIView子类,它们存在于一种目录中。当你点击其中一个时,另一个可拖动的版本会放在上面。我希望库存版本在您拖动其他版本时保持不变。复制的代码可以工作,但拖动手指不会移动它 如果我释放并开始拖动新创建的版本,它会按预期移动。我认为这是因为最初的触摸(制作了复制品)在应答器链中没有可拖动的版本 一点代码 在我静止的“图标” 在我的viewController中Objective c 拖动触摸开始后创建的UIView,objective-c,cocoa-touch,uitouch,uiresponder,Objective C,Cocoa Touch,Uitouch,Uiresponder,我有几个UIView子类,它们存在于一种目录中。当你点击其中一个时,另一个可拖动的版本会放在上面。我希望库存版本在您拖动其他版本时保持不变。复制的代码可以工作,但拖动手指不会移动它 如果我释放并开始拖动新创建的版本,它会按预期移动。我认为这是因为最初的触摸(制作了复制品)在应答器链中没有可拖动的版本 一点代码 在我静止的“图标” 在我的viewController中 - (void)placeDraggableItem:(Item *)item WithPoint:(CGPoint)point
- (void)placeDraggableItem:(Item *)item WithPoint:(CGPoint)point
{
DraggableItem *draggableItem = [[DraggableItem alloc] initWithImage:[UIImage imageNamed:item.graphic]];
draggableItem.frame = CGRectMake(point.x, scrollView.frame.origin.y + point.y, 64.0f, 64.0f);
[self.view addSubview:draggableItem];
[draggableItem release];
}
在我的DragableItem中
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] 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.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(self.bounds);
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;
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
现在再次创建可拖动的版本。可拖动的版本可以在你第一次触摸后移动。但我认为我需要让新创建的UIView响应最初为“图标”制作的触摸
有什么想法吗
我知道这个问题有点类似于这个问题:但在这种情况下,应该接收触摸的视图已经存在,而我需要将触摸传递到新创建的视图。我不知道有什么方法可以将触摸传递到新创建的视图,尽管这并不意味着没有方法可以这样做。看起来你真的只想让你的可拖动视图来处理触摸,我可能会在创建不可拖动视图的同时创建每个可拖动视图,alpha值为0,在touches中,开始将其设置为1。如果您的不可拖动视图不需要处理触摸,那么让它们处理触摸只是为了传递它们是没有意义的。这实际上是一个很好的方式来看待它,可能比我现在做的更有逻辑。唯一的区别是,我可能会把它们都摆出来,当你点击拖动时,一个新的会掉在下面。
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] 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.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(self.bounds);
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;
self.center = newPoint;
}