拖动手指下的 UIView

发布于 2024-10-17 01:39:11 字数 299 浏览 5 评论 0原文

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

有一种非常有用的方法可以做到这一点,并且在 iTunes U 视频之一中进行了参考。该脚本没有使用 deltaX、deltaY 将图像拖动到您点击的位置下方,而不是将其放在手指下方的中心,但我不记得该代码是什么!

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

I want to tap on a UIView and drag and have that view follow my finger, simple enough. But the easiest way of doing this is by setting the objects center to where the tap happened (Which is not what I want), I want it to move as if you grabbed the object wherever you tapped it.

There was a very useful way of doing this and it was reference in one of the iTunes U videos. The script didn't use deltaX, deltaY for dragging the image underneath where you tapped on it instead of having it center underneath your finger but I can't remember what that code was!

Does anyone have a reference to this code? Or perhaps have an efficient way of moving UIViews under a finger without having the uiview.center = tap.center concept?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷月断魂刀 2024-10-24 01:39:11

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

您可以根据您的情况对此进行优化,这样您就不必深入研究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);
 }

Swift 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)!)
    }

The following code is an example of a simple gesture recognizer that will allow panel / view movement. Instead of modifying the center, you're modifying the origin point [basically by setting a new frame for the target view].

You can optimize this in your situation so you're not having to drill down into the gesture.view... etc

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

Swift 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)!)
    }
情话墙 2024-10-24 01:39:11

这是来自 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;
   }
}

Here is the code from Apple's MoveMe project the key is to do this in the touchesMoved method. It allows the UIView(PlacardView) to see the touch and move wherever the user's touch goes. Hope this helps.

- (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;
   }
}
迷乱花海 2024-10-24 01:39:11

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

// 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];
}

I think you are talking about the stalker app...

// 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];
}
恍梦境° 2024-10-24 01:39:11

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

You can save both the point where you touched (T) and the current position of the view (O). In touchMoved, you can move it based on the new point by adding (O-T).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文