UIView拖动(图像和文字)

有可能拖动UIView围绕iOS屏幕,同时它有图像和文字? 例如小卡片。 你能把我指向类似(解决)的话题吗? 我还没有find。

虽然UIView没有内置的支持来拖拽用户,但实现它并不困难。 当您只处理拖放视图时,更容易,而不是其他操作,如敲击,双击,多触摸等。

首先要做的是通过inheritanceUIView来创build一个自定义的视图,比如DraggableView 。 然后重写UIView的touchesMoved:withEvent:方法,你可以得到一个当前的拖动位置,然后移动DraggableView。 看看下面的例子。

 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *aTouch = [touches anyObject]; CGPoint location = [aTouch locationInView:self.superview]; [UIView beginAnimations:@"Dragging A DraggableView" context:nil]; self.frame = CGRectMake(location.x, location.y, self.frame.size.width, self.frame.size.height); [UIView commitAnimations]; } 

并且因为DraggableView对象的所有子视图也将被移动。 因此,把所有的图像和文本作为DraggableView对象的子视图。

我在这里实现的非常简单。 但是,如果您想要拖动更复杂的行为(例如,用户必须点击视图几秒钟来移动视图),那么您将不得不重写其他事件处理方法(touchesBegan:withEvent:和touchesEnd:withEvent)。

这就是基于pepouze的答案,一个整洁的解决scheme,看起来像( testing,它的工作!

 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *aTouch = [touches anyObject]; CGPoint location = [aTouch locationInView:self]; CGPoint previousLocation = [aTouch previousLocationInView:self]; self.frame = CGRectOffset(self.frame, (location.x - previousLocation.x), (location.y - previousLocation.y)); } 

除了MHC的答案 。

如果你不想让视图的左上angular在你的手指下跳跃,你也可以重写touchesBegan像这样:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *aTouch = [touches anyObject]; offset = [aTouch locationInView: self]; } 

并改变MHC的动作到:

  -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *aTouch = [touches anyObject]; CGPoint location = [aTouch locationInView:self.superview]; [UIView beginAnimations:@"Dragging A DraggableView" context:nil]; self.frame = CGRectMake(location.x-offset.x, location.y-offset.y, self.frame.size.width, self.frame.size.height); [UIView commitAnimations]; } 

您还应该在界面中定义CGPoint offset

 @interface DraggableView : UIView { CGPoint offset; } 

编辑:

阿里·利托夫斯基提供更优雅的解决scheme,让你沟渠伊娃: https : //stackoverflow.com/a/10378382/653513

即使rokjarc解决scheme工作,使用

 CGPoint previousLocation = [aTouch previousLocationInView:self.superview]; 

避免创buildCGPoint offset和调用touchesBegan:withEvent:

这里是一个解决scheme来拖动自定义的UIView (它可以缩放或旋转通过它的transform ),它可以保存图像和/或文本(只需要编辑Tile.xib ):

应用截图

 - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; CGPoint previous = [touch previousLocationInView:self]; if (!CGAffineTransformIsIdentity(self.transform)) { location = CGPointApplyAffineTransform(location, self.transform); previous = CGPointApplyAffineTransform(previous, self.transform); } self.frame = CGRectOffset(self.frame, (location.x - previous.x), (location.y - previous.y)); } 

这为我工作。 我的UIView旋转和缩放

 - (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; CGPoint previous = [touch previousLocationInView:self]; if (!CGAffineTransformIsIdentity(self.transform)) { location = CGPointApplyAffineTransform(location, self.transform); previous = CGPointApplyAffineTransform(previous, self.transform); } CGRect newFrame = CGRectOffset(self.frame, (location.x - previous.x), (location.y - previous.y)); float x = CGRectGetMidX(newFrame); float y = CGRectGetMidY(newFrame); self.center = CGPointMake(x, y); 

}