【问题标题】:Dragging UIView under finger [duplicate]在手指下拖动 UIView [重复]
【发布时间】:2011-02-12 20:47:42
【问题描述】:

我想点击 UIView 并拖动并让该视图跟随我的手指,很简单。但最简单的方法是将对象中心设置为点击发生的位置(这不是我想要的),我希望它移动,就好像你在任何你点击的地方抓住了对象一样。

有一种非常有用的方法可以做到这一点,并且在其中一个 iTunes U 视频中得到了参考。该脚本没有使用 deltaX 和 deltaY 将图像拖到您点击它的下方,而不是让它在您的手指下方居中,但我不记得那个代码是什么!

有人参考过这段代码吗?或者也许有一种在手指下移动 UIView 的有效方法,而无需 uiview.center = tap.center 概念?

【问题讨论】:

  • 答案有帮助吗?如果是,请检查它是否正确或以其他方式回复...
  • 他确实回复了 - 这不是他想要的。
  • 找到解决方案了吗?我很想看到一个没有像你声称看到的那样使用 delta 的。

标签: iphone uiview itunes move drag


【解决方案1】:

以下代码是允许面板/视图移动的简单手势识别器示例。不是修改中心,而是修改原点[基本上是通过为目标视图设置一个新框架]。

您可以根据自己的情况对其进行优化,这样您就不必深入研究gesture.view...等

-(void)dragging:(UIPanGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        //NSLog(@"Received a pan gesture");
        self.panCoord = [gesture locationInView:gesture.view];


    }
    CGPoint newCoord = [gesture locationInView:gesture.view];
    float dX = newCoord.x-panCoord.x;
    float dY = newCoord.y-panCoord.y;

gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y+dY, gesture.view.frame.size.width, gesture.view.frame.size.height);
 }

斯威夫特 4:

@objc func handleTap(_ sender: UIPanGestureRecognizer) {
        if(sender.state == .began) {
            self.panCoord = sender.location(in: sender.view)
        }

        let newCoord: CGPoint = sender.location(in: sender.view)

        let dX = newCoord.x - panCoord.x
        let dY = newCoord.y - panCoord.y

        sender.view?.frame = CGRect(x: (sender.view?.frame.origin.x)!+dX, y: (sender.view?.frame.origin.y)!+dY, width: (sender.view?.frame.size.width)!, height: (sender.view?.frame.size.height)!)
    }

【讨论】:

  • 你的代码中的“panCoord”是什么?
  • 抱歉,panCoord 是本地 CGPoint,它跟踪当前平移到的位置。我没有意识到依赖存在且未解决。请添加一个 CGPoint panCoord 来存储“从哪里移动”的当前位置
【解决方案2】:

这是来自 Apple 的 MoveMe 项目的代码,关键是在 touchesMoved 方法中执行此操作。它允许UIView(PlacardView) 看到触摸并在用户触摸的任何地方移动。希望这会有所帮助。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

// If the touch was in the placardView, move the placardView to its location
if ([touch view] == placardView) {
    CGPoint location = [touch locationInView:self];
    placardView.center = location;
    return;
   }
}

【讨论】:

  • 这个问题是如果你在视图中没有开始就移动你的手指事件,它会改变它的位置,这会导致一个跳跃。
  • 它已经指向了正确的方向!但我读的时候也是这么想的:)
【解决方案3】:

我认为您在谈论跟踪应用程序...

// Tell the "stalker" rectangle to move to each touch-down
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [UIView beginAnimations:@"stalk" context:nil];
    [UIView setAnimationDuration:1];
    //[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // touches is an NSSet.  Take any single UITouch from the set
    UITouch *touch = [touches anyObject];

    // Move the rectangle to the location of the touch
    stalker.center = [touch locationInView:self];
    [UIView commitAnimations];
}

【讨论】:

  • 感谢代码,但这不是我想要完成的。我希望对象在我拖动时在我的手指下移动,以便 UIView 的中心根据我移动手指的方式而不是以我的手指为中心移动
  • @Parad0x13 嘿,您的问题有什么解决方案吗?那么请告诉我,因为我面临同样的情况,我需要解决它。
【解决方案4】:

您可以保存您触摸的点 (T) 和视图的当前位置 (O)。在 touchMoved 中,您可以通过添加 (O-T) 来根据新点移动它。

【讨论】:

  • 好吧,我可以这样做。但是还有另一种更清洁的方法可以做到这一点。 UIView 有一个原生框架,可以完成我想要的,而无需自己做数学运算
  • 请原谅。你是在问还是在肯定? :) 如果您找到了这样的框架,请在此处添加评论(或回答您自己的问题),以公开知识! ;)
猜你喜欢
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多