How to drag certain image in iOS?

前端 未结 2 525
野趣味
野趣味 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: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, 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
    }
    

提交回复
热议问题