【问题标题】:Dragging a specific UIImageView拖动特定的 UIImageView
【发布时间】:2012-02-16 15:30:20
【问题描述】:

在我的代码中,我有一个对象数组,它们是 UIImageViews,其中包含 UIImages。

我在-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 中使用了一个 for 循环来将数组中的所有对象一个接一个地移动。

一旦它们被移动到主视图上的不同位置,我希望能够抓取任何这些对象并将它们移动到其他地方。

我不确定如何最好地做到这一点。有没有办法可以使用 touchesbegin 以便只能拖动我点击的项目?

基本上,现在有五个 UIImageViews,每个都有 UIImage,我试图达到当我点击其中任何一个时它们能够移动的地步。

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    编辑:John Muchow 在博客 here 上发布了这篇文章。你应该能够适应你自己的目的。

    //UIDraggableImageView.h
    #import <UIKit/UIKit.h>
    
    @interface UIDraggableImageView : UIImageView
    {
        CGPoint currentPoint;
    }
    
    @end
    
    //UIDraggableImageView.m
    #import "UIDraggableImageView.h"
    
    @implementation UIDraggableImageView
    
    - (id)initWithImage:(UIImage *)image
    {
        if (self = [super initWithImage:image]) {
            self.userInteractionEnabled = YES;
        }
        return self;
    }
    
    -(void)dealloc {
        [super dealloc];
    }
    
    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        // When a touch starts, get the current location in the view
        currentPoint = [[touches anyObject] locationInView:self];
    }
    
    - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
    {
        // Get active location upon move
        CGPoint activePoint = [[touches anyObject] locationInView:self];
    
        // Determine new point based on where the touch is now located
        CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
                                       self.center.y + (activePoint.y - currentPoint.y));
    
        //--------------------------------------------------------
        // Make sure we stay within the bounds of the parent view
        //--------------------------------------------------------
        float midPointX = CGRectGetMidX(self.bounds);
        // If too far right...
        if (newPoint.x > self.superview.bounds.size.width  - midPointX)
            newPoint.x = self.superview.bounds.size.width - midPointX;
        else if (newPoint.x < midPointX)  // If too far left...
            newPoint.x = midPointX;
    
        float midPointY = CGRectGetMidY(self.bounds);
        // If too far down...
        if (newPoint.y > self.superview.bounds.size.height  - midPointY)
            newPoint.y = self.superview.bounds.size.height - midPointY;
            else if (newPoint.y < midPointY)  // If too far up...
            newPoint.y = midPointY;
    
        // Set new center location
        self.center = newPoint;
    }
    
    @end
    

    【讨论】:

      【解决方案2】:

      我在可拖动视图中使用UIPanGestureRecognizer 做了类似的事情并取得了巨大成功。要连接它,它可能看起来像这样:

      @implementation DraggableView
      
      -(id)initWithFrame:(CGRect)frame
      {
         ...
         ...
         UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)] autorelease];
         [self addGestureRecognizer:panGesture];
         ..
         return self;
      }
      @end
      

      在您的pan: 方法中,您可以检查手势的状态,甚至捕获手势的位置并使用它来重新定位您的视图。下面是一个粗略示例:

      -(void)pan:(UIPanGestureRecognizer *)gesture
      {
      
         if (gesture.state==UIGestureRecognizerStateChanged)
         {
            //We're moving!
            UIView *aRootView = <perhaps your root view controller's view>;
            CGPoint currentOrigin = [gesture translationInView:aRootView];
            self.frame = CGRectMake(currentOrigin.x,currentOrigin.y,self.frame.size.width,self.frame.size.hight);
            ...
            ...
         }
      
      }
      

      Here is more readingUIPanGestureRecognizer

      【讨论】:

        【解决方案3】:

        您可能需要考虑将手势识别器添加到要移动的视图中,这样您就可以轻松判断它们何时被点击,并且可以使用 touchesMoved 来移动它们...

        【讨论】:

          【解决方案4】:

          单击按钮将图像从相机胶卷添加到图像视图。图片可以放大和缩小,并且应该将该图片拖到视图中的任何位置。

          【讨论】:

          • 请逐步做出您的答案,并在您的答案中添加您已经尝试过的代码,以便能够成为有用的答案
          猜你喜欢
          • 2023-04-05
          • 2012-05-25
          • 1970-01-01
          • 1970-01-01
          • 2019-11-13
          • 1970-01-01
          • 1970-01-01
          • 2020-10-08
          • 1970-01-01
          相关资源
          最近更新 更多