【问题标题】:How to Drag a UIImageView?如何拖动 UIImageView?
【发布时间】:2023-04-05 18:29:01
【问题描述】:

这是我的情况:我有 3 个 UIImageViews。其中两个将充当反应器(第三个将被拖到的反应器)。如前所述,第三个将可拖动到前面提到的两个 UIImageView 之一。

我目前正在使用 touchesBegan、touchesMoved 和 touchesEnded 来完成此操作,但存在一个问题。

在可拖动的 UIImageView 被拖动到两个 UIImageView 之一之后,我希望可拖动的图像视图缓慢(动画)回到原始位置(底部中心)。

我怎样才能做到这一点?提前致谢!

【问题讨论】:

  • 这里到底有什么问题?如何管理拖动,如何检测位置在反应器上或如何进行慢速捕捉动画?还是所有这些?
  • 非常好的问题。是的,我知道如何拖动 UImageView,但是如何将它合并到回弹中呢?所以是的,所有这些。
  • 你有什么特别的理由使用 toucesBegin/Moved/etc 而不是 UIPanGestureRecognizer?在进行拖动或检测拖动结束时,使用 UIPanGestureRecognizer 有很大帮助。

标签: ios objective-c uiimageview drag touches


【解决方案1】:

我在一个小项目中尝试使用更方便的 UIPanGestureRecognizer 进行拖动捕捉。希望它能解决您的问题,如果您需要更多信息,请发表评论(您可以从https://github.com/codedad/SO_Drag_n_Snap_iOS 下载 GitHub 项目)。

我的 ViewController.h:

@interface ViewController : UIViewController <UIGestureRecognizerDelegate> {
    CGPoint firstPos;
}

@property IBOutlet UIImageView *spot1;
@property IBOutlet UIImageView *spot2;
@property IBOutlet UIImageView *bkgr;
@property IBOutlet UIImageView *dragimg;

- (float) distanceSquared:(CGPoint) p1 p2:(CGPoint) p2;
- (IBAction)pan:(UIPanGestureRecognizer *)gesture;

我的 ViewController.m:

- (float) distanceSquared:(CGPoint) p1 p2:(CGPoint) p2 {
    float dx = (p1.x-p2.x);
    float dy = (p1.y-p2.y);
    return dx*dx+dy*dy;
}

void sort(int*a,int n,int*ndx) {
    int*b,*c,t;
    int*bi,*ci,ti;
    for(b=a+n,bi=ndx+n ; --b>a;) {
        --bi;
        for(c=b,ci=bi;--c>=a;) {
            --ci;
            if(*c>*b)t=*b,*b=*c,*c=t,ti=*bi,*bi=*ci,*ci=ti;
        }
    }
}

- (IBAction)pan:(UIPanGestureRecognizer *)gesture {
    CGPoint translatedPoint = [gesture translationInView:self.view];

    if ([gesture state] == UIGestureRecognizerStateBegan) {
        firstPos = [[gesture view] center];
    }

    translatedPoint = CGPointMake(firstPos.x+translatedPoint.x, firstPos.y+translatedPoint.y);
    [[gesture view] setCenter:translatedPoint];

    if ([gesture state] == UIGestureRecognizerStateEnded) {
        CGPoint snapPos;
        int distances[3];
        distances[0] = [self distanceSquared:dragimg.center p2:spot1.center];
        distances[1] = [self distanceSquared:dragimg.center p2:spot2.center];
        distances[2] = [self distanceSquared:dragimg.center p2:bkgr.center];
        int distances_ndx[3] = {0,1,2};
        sort(distances, 3, distances_ndx);

        switch (distances_ndx[0]) {
            case 0:
                snapPos = spot1.center;
                break;
            case 1:
                snapPos = spot2.center;
                break;
            default:
                snapPos = bkgr.center;
                break;
        }
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDelegate:self];
        dragimg.center = snapPos;
        [UIView commitAnimations];
    }
}

我的 ViewController.xib:

我包含了 4 个 UIImageView。其中 3 个是静态的(spot1,2 和“home”点背景半色调圆圈),加上“home”点内的可拖动黄色圆圈。我还添加了一个平移手势识别器。然后我设置网点:

  • uiimageviews 连接到 spot1、spot2、bkgr 和 dragimg
  • 平移手势的委托连接到视图,操作连接到“平移”方法
  • dragimg 的 gestureRecognizer 然后连接到 Pan Gesture Recognizer(之前通过拖动添加到 viewcontroller)

  • 如果您扩展距离计算并正确修改排序,您可以添加更多点

【讨论】:

    【解决方案2】:
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
    
    UITouch *touch = [[event allTouches]anyObject];
    if([touch view] == First_imageview)
    {
    }
    
    UITouch *touch1 = [[event allTouches]anyObject];
    if([touch1 view] == 2_imageview)
    {
    }
    
    UITouch *touch2 = [[event allTouches]anyObject];
    if([touch2 view] == 3_imageview)
    {
    }
    
    }
    
    
    
    
    
        - (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event
     {
    
        UITouch *touch = [[event allTouches]anyObject];
        if([touch view] == First_imageview)
        {
        }
    
        UITouch *touch1 = [[event allTouches]anyObject];
        if([touch1 view] == 2_imageview)
        {
        }
    
        UITouch *touch2 = [[event allTouches]anyObject];
        if([touch2 view] == 3_imageview)
        {
        }
      }
    

    使用这两种方法并简单地拖动您的三个图像视图。我希望这段代码对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2012-05-25
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多