UIView拖动(图像和文字)

时间:2011-02-13 03:35:09

标签: iphone objective-c

是否可以在iOS屏幕上同时拖动UIView同时显示图像和文字?例如小卡片。你能指点我类似的(已解决的)主题吗?我还没找到。

6 个答案:

答案 0 :(得分:36)

根据pepouze的答案,这就是一个简洁的解决方案(测试,它有效!

- (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));
}

答案 1 :(得分:33)

虽然UIView没有内置支持在用户拖动中移动自身,但实现起来应该不那么困难。当您只处理拖动视图时,它更容易,而不是其他操作,如点击,双击,多点触摸等。

要做的第一件事是通过子类化UIView来创建自定义视图,比如说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)。

答案 2 :(得分:24)

MHC's answer的补充。

如果您不想要视图的左上角 要在你的手指下跳,你也可以覆盖touchesBegan 像这样:

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

    offset = [aTouch locationInView: self];
}

并将MHC的touchesMoved改为:

 -(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;
}

修改

Arie Litovsky提供更优雅的解决方案,让您放弃伊娃:https://stackoverflow.com/a/10378382/653513

答案 3 :(得分:5)

即使rokjarc解决方案有效,也可以使用

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

避免CGPoint offset创建和touchesBegan:withEvent:

的调用

答案 4 :(得分:2)

以下是拖动a custom UIView的解决方案(可以通过其transform进行缩放或旋转),可以保存图像和/或文本(只需根据需要编辑Tile.xib) :

app screenshot

- (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));
}

答案 5 :(得分:0)

这项工作对我而言。我的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);

}