123ZDP
123ZDP

Reputation: 137

How to drag certain image in iOS?

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 drag the image, but not place finger on screen and it goes to that spot. Just drag.

Gives me the error:

"Use of undeclared type 'uitouch'"

import UIKit

class DraggableImage: UIImageView {
    override func touchesMoved(touches: Set<uitouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.locationInView(superview)
            center = CGPointMake(position.x, position.y)
        }
    }
}

Upvotes: 0

Views: 1665

Answers (2)

Reinier Melian
Reinier Melian

Reputation: 20804

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

enter image description here

Hope this helps

Upvotes: 2

iOS Geek
iOS Geek

Reputation: 4855

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
}

Upvotes: 0

Related Questions