【问题标题】:How to Rotate a UIimage view to the point at which you touched the screen?如何将 UIimage 视图旋转到您触摸屏幕的位置?
【发布时间】:2021-03-12 17:46:20
【问题描述】:

我目前正在使用 IOS swift,我无法将屏幕上的图像视图旋转到我触摸屏幕的位置,因此,如果直接在图像下方点击,它会旋转 180 度以指向接触点。

这是我到目前为止的代码,我想将 playerCharacter 旋转到我触摸屏幕的任何位置

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

【问题讨论】:

    标签: ios swift uiimageview


    【解决方案1】:

    你可以用一点数学来计算 imageView 的中心和触摸点之间的角度:

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

    然后旋转图像视图:

        imgView.transform = CGAffineTransform(rotationAngle: angle)
    

    这是一个使用 SF 符号“arrow.right”图像的完整示例(因此它开始指向零度),然后箭头跟踪触摸位置:

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

    【讨论】:

    • 我能够向下旋转并完成,但是每次触摸屏幕时它都会随机改变宽度和高度,有没有办法解决这个问题
    • @AaronJeffries - 你需要更具体一点。在我发布的示例代码中,imageView NOT 改变了大小。你还在做什么可能会导致这种情况?
    • 对我有用的好答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多