How to Rotate a UIimage view to the point at which you touched the screen?

Viewed 177

Im currently using IOS swift and I'm having trouble rotating the image view that I have on my screen to the point at which I touch the screen, So if tap directly underneath the image, it rotates 180 degrees to point at the touch point.

Here is the Code I have so far, I want to rotate the playerCharacter to wherever I touch the screen

import UIKit
import SpriteKit
class ViewController: UIViewController {
   
    @IBOutlet weak var playerCharacter: UIImageView!
    var enemyspeed: Timer?
    let imageName = "yourImage.png"
    let image = UIImage(named: "")
    let imageView = UIImageView(image: nil)
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView.frame = CGRect(x: 1000, y: 700, width: 100, height: 100)
        view.addSubview(imageView)
        imageView.backgroundColor = .gray
    }
    
    @IBAction func beginGame(_ sender: UIButton) {
        enemyspeed = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(astroid), userInfo: nil, repeats: true)
       
      
       
    }
   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       if let touch = touches.first {
           let position = touch.location(in: view)
           print(position)

        UIView.animate(withDuration: 1, animations: {


            self.playerCharacter.frame.origin.x = position.x
            self.playerCharacter.frame.origin.y = position.y
            
            
           })
          
       }
   }
  @objc func astroid(){

    let enemyPositionX = playerCharacter.frame.origin.x
           let enemyPositionY = playerCharacter.frame.origin.y
           UIView.animate(withDuration: 5, animations:{
               self.imageView.frame.origin.x = enemyPositionX
               self.imageView.frame.origin.y = enemyPositionY
            
           })
    }
    
    
}

1 Answers

You can use a little math to calculate the angle between the center of the imageView and the touch point:

    let pt1 = imgView.center
    let pt2 = touch.location(in: view)
    let angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x)

then rotate the image view:

    imgView.transform = CGAffineTransform(rotationAngle: angle)

Here's a full example that uses a SF Symbol "arrow.right" image (so it starts pointing at Zero degrees), then the arrow tracks the touch location:

class AngleViewController: UIViewController {
    
    let imgView: UIImageView = {
        let v = UIImageView()
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // make sure we have an image
        guard let img = UIImage(systemName: "arrow.right") else {
            fatalError("Could not load image!!!!")
        }
        
        // set the image
        imgView.image = img
        
        // add the image view to the view
        view.addSubview(imgView)

        // 100x100 image view frame
        imgView.frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)
        
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        // let's put the image view in the center
        imgView.center = view.center
    }
    
    func updateArrow(_ pt2: CGPoint) -> Void {
        let pt1 = imgView.center
        let angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x)
        imgView.transform = CGAffineTransform(rotationAngle: angle)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }
        updateArrow(touch.location(in: view))
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }
        updateArrow(touch.location(in: view))
    }
    
}
Related