How to drag certain image in iOS?

前端 未结 2 514
野趣味
野趣味 2020-12-22 14:42

Wanted to know how I can drag a image across screen and what code would be used. Tried looking up but only older versions of Swift have answer and no longer work. I want to

相关标签:
2条回答
  • 2020-12-22 15:03

    You need to subclass UIImageView and in the init you need to set userInteractionEnabled = true and then override this method override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) well, my code is this:

        class DraggableImage: UIImageView {
    
        var localTouchPosition : CGPoint?
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.layer.borderWidth = 1
            self.layer.borderColor = UIColor.red.cgColor
            self.isUserInteractionEnabled = true
        }
    
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            let touch = touches.first
            self.localTouchPosition = touch?.preciseLocation(in: self)
        }
    
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesMoved(touches, with: event)
            let touch = touches.first
            guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{
                return
            }
            self.frame.origin = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)
        }
    
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            self.localTouchPosition = nil
        }
        /*
         // Only override drawRect: if you perform custom drawing.
         // An empty implementation adversely affects performance during animation.
         override func drawRect(rect: CGRect) {
         // Drawing code
         }
         */
    
    }
    

    This is how it looks

    Hope this helps

    0 讨论(0)
  • 2020-12-22 15:18

    Create a Nsobject Class for moving View and add following Code

    import UIKit
    
    class objectClass: UIImageView, UIGestureRecognizerDelegate {
        /*
        // Only override draw() if you perform custom drawing.
        // An empty implementation adversely affects performance during animation.
        override func draw(_ rect: CGRect) {
            // Drawing code
        }
        */
    
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
                    let touch: UITouch = touches.first!
            self.center = touch.location(in: self.superview)
    
        }
    
    }
    

    in mainViewController make a object of NSobject class

    var newView: objectClass = objectClass()
    

    on button Action to add new View

    @IBAction func objectAdded(theButton: UIButton!) {
        let frame = CGRect(x: 100, y: 100, width: 44, height: 44)
        newView = objectClass(frame: frame)
    
    if theButton.titleLabel?.text == "image1" {
    newView.image = UIImage(named: "1")
    } else if theButton.titleLabel?.text == "image2" {
        newView.image = UIImage(named: "2")
    }else{
        newView.image = UIImage(named: "3")
    }
    newView.contentMode = .scaleAspectFill
    newView.isUserInteractionEnabled = true
    self.view .addSubview(newView)
    
    newView.alpha = 0
    UIView .animate(withDuration: 0.4) {
        self.newView.alpha = 1
    }
    
    UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut, animations: { () -> Void in
        self.sliderViewBottomLayoutConstraint.constant = self.sliderViewBottomLayoutConstraint.constant - self.sliderViewBottomLayoutConstraint.constant
        self.view.layoutIfNeeded()
    }, completion: nil)
    
    image1Button.isEnabled = false
    image2Button.isEnabled = false
    image3Button.isEnabled = false
    
    let pinchGesture: UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.recognizePinchGesture(sender:)))
    pinchGesture.delegate = self
    
    let rotateGesture: UIRotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.recognizeRotateGesture(sender:)))
    
    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.RemoveSelectedImageOnTap(sender:)))
    tapGesture.numberOfTapsRequired = 2
    
    self.newView.addGestureRecognizer(tapGesture)
    self.newView.addGestureRecognizer(pinchGesture)
    self.newView.addGestureRecognizer(rotateGesture)
    }
    
    func recognizePinchGesture(sender: UIPinchGestureRecognizer) {
        sender.view!.transform = sender.view!.transform.scaledBy(x: sender.scale, y: sender.scale)
        sender.scale = 1
    }
    
    func recognizeRotateGesture(sender: UIRotationGestureRecognizer) {
        sender.view!.transform = sender.view!.transform.rotated(by: sender.rotation)
        sender.rotation = 0
    }
    
    0 讨论(0)
提交回复
热议问题