【问题标题】:drag and drop uiimage into another uiimageview将 uiimage 拖放到另一个 uiimageview
【发布时间】:2013-05-15 11:49:21
【问题描述】:

我正在使用下面的代码sn-p来拖放uiimageview

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[myImageView addGestureRecognizer:panRecognizer];

-(void)move:(id)sender {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        firstX = [myImageView  center].x;
        firstY = [myImageView  center].y;
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [myImageView  setCenter:translatedPoint];

}

此代码是拖动整个 myImageView ,但我的要求是只需拖动 uiimage 并将其放到另一个 uiimagview 中。拖动后 myImageView 应该保持原样。只是我需要拖动 myImageView 层。可拖动的图像应该是透明。任何想法都将不胜感激。

【问题讨论】:

    标签: objective-c ipad drag-and-drop uiimageview


    【解决方案1】:

    我几乎没有付出任何努力来实现您的输出。试试看

    Step 1:在你的.h文件中定义这3个变量

    UIImageView *ivSource1, *ivDestination2, *tempIV;
    

    Step 2 :初始化所有三个UIImageView 并添加到您的ViewController 中写入viewDidLoad 方法

    ivSource1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
    [ivSource1 setFrame:CGRectMake(100, 100, 100, 100)];
    [ivSource1 setTag:100];
    [ivSource1 setUserInteractionEnabled:YES];    
    [self.view addSubview:ivSource1];
    
    ivDestination2 = [[UIImageView alloc] init];
    [ivDestination2 setFrame:CGRectMake(200, 300, 100, 100)];
    [ivDestination2 setTag:101];
    [ivDestination2 setUserInteractionEnabled:YES];
    [self.view addSubview:ivDestination2];
    
    tempIV = [[UIImageView alloc] init];
    [tempIV setFrame:CGRectMake(0, 300, 100, 100)];
    [tempIV setTag:102];
    [tempIV setUserInteractionEnabled:YES];
    [self.view addSubview:tempIV];
    

    Step 3:定义以下触摸方法来处理拖放图像的移动

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
    
        if([[touch view] tag] == 100)
        {
            [tempIV setImage:ivSource1.image];
            [tempIV setCenter:[touch locationInView:self.view]];
        }
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
    
        [tempIV setCenter:[touch locationInView:self.view]];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        [tempIV setCenter:[touch locationInView:self.view]];
    
        if(CGRectContainsPoint(ivDestination2.frame, [touch locationInView:self.view]))
        {
            [ivDestination2 setImage:tempIV.image];
        }
        // Remove image from dragable view
        [tempIV setImage:[UIImage imageNamed:@""]];    
    }
    

    【讨论】:

      【解决方案2】:

      在 iOS 13 中,您可以使用拖放和复制/粘贴 API 来执行将 UIImage 从一个 UIImageView 拖放到另一个 UIImageView 的操作。根据您的需要,您可以选择以下两种 Swift 5.1 实现之一。


      #1。使用UIDragInteractionUIDragInteractionDelegateUIPasteConfiguration

      import UIKit
      
      class ViewController: UIViewController {
      
          let imageView1 = UIImageView()
          let imageView2 = UIImageView()
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              imageView1.image = UIImage(named: "image")
              imageView1.contentMode = .scaleAspectFit
              imageView1.isUserInteractionEnabled = true
              let dragInteraction = UIDragInteraction(delegate: self)
              dragInteraction.isEnabled = true
              imageView1.addInteraction(dragInteraction)
      
              imageView2.contentMode = .scaleAspectFit
              imageView2.isUserInteractionEnabled = true
              let configuration = UIPasteConfiguration(forAccepting: UIImage.self)
              imageView2.pasteConfiguration = configuration
      
              let stackView = UIStackView(arrangedSubviews: [imageView1, imageView2])
              view.addSubview(stackView)
              stackView.distribution = .fillEqually
              stackView.frame = view.bounds
              stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
          }
      
          override func paste(itemProviders: [NSItemProvider]) {
              _ = itemProviders.first?.loadObject(ofClass: UIImage.self, completionHandler: { (image: NSItemProviderReading?, error: Error?) in
                  DispatchQueue.main.async {
                      self.imageView2.image = image as? UIImage
                  }
              })
          }
      
      }
      
      extension ViewController: UIDragInteractionDelegate {
      
          func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
              guard let image = imageView1.image else { return [] }
              let item = UIDragItem(itemProvider: NSItemProvider(object: image))
              return [item]
          }
      
      }
      

      #2。使用UIDragInteractionUIDragInteractionDelegateUIDropInteractionUIDropInteractionDelegate

      import UIKit
      
      class ViewController: UIViewController {
      
          let imageView1 = UIImageView()
          let imageView2 = UIImageView()
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              imageView1.image = UIImage(named: "image")
              imageView1.contentMode = .scaleAspectFit
              imageView1.isUserInteractionEnabled = true
      
              imageView2.contentMode = .scaleAspectFit
              imageView2.isUserInteractionEnabled = true
      
              let dragInteraction = UIDragInteraction(delegate: self)
              dragInteraction.isEnabled = true
              imageView1.addInteraction(dragInteraction)
      
              let dropInteraction = UIDropInteraction(delegate: self)
              imageView2.addInteraction(dropInteraction)
      
              let stackView = UIStackView(arrangedSubviews: [imageView1, imageView2])
              view.addSubview(stackView)
              stackView.distribution = .fillEqually
              stackView.frame = view.bounds
              stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
          }
      
      }
      
      extension ViewController: UIDragInteractionDelegate {
      
          func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
              guard let image = imageView1.image else { return [] }
              let item = UIDragItem(itemProvider: NSItemProvider(object: image))
              item.localObject = image
              return [item]
          }
      
      }
      
      extension ViewController: UIDropInteractionDelegate {
      
          func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
              return session.canLoadObjects(ofClass: UIImage.self) && session.items.count == 1
          }
      
          func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
              let dropLocation = session.location(in: view)
              let operation: UIDropOperation
              if imageView2.frame.contains(dropLocation) {
                  operation = session.localDragSession == nil ? .copy : .move
              } else {
                  operation = .cancel
              }
      
              return UIDropProposal(operation: operation)
          }
      
          func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
              session.loadObjects(ofClass: UIImage.self) { imageItems in
                  guard let images = imageItems as? [UIImage] else { return }
                  self.imageView2.image = images.first
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多