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
})
}
}