如何从特定锚点旋转图像
How to rotate a image from a specific anchor point
我想在这段代码中旋转图片 pic1。我在下面放了一张我正在寻找的图片。我不想旋转整个图像我想从矩形的左下角旋转 90 度。你可以具体看我在绿色圆圈里说的是什么。旋转应该在按钮功能中进行。
import UIKit
class ViewController: UIViewController {
var pic1 = UIImageView()
var rot = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pic1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pic1)
rot.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(rot)
rot.backgroundColor = .blue
pic1.image = UIImage(named: "badMan.png")
NSLayoutConstraint.activate(
[
pic1.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.50),
pic1.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
pic1.topAnchor.constraint(equalTo: view.topAnchor),
pic1.leadingAnchor.constraint(equalTo: view.leadingAnchor),
rot.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.50),
rot.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
rot.topAnchor.constraint(equalTo: pic1.bottomAnchor),
rot.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
self.view.addSubview(pic2)
rot.addTarget(self, action: #selector(roate), for: .touchDown)
}
@objc func roate(){
}
}
您可以扩展 uiview 来设置锚点:
extension UIView{
func setAnchorPoint(anchorPoint: CGPoint) {
var newPoint = CGPoint(x: self.bounds.size.width * anchorPoint.x, y: self.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: self.bounds.size.width * self.layer.anchorPoint.x, y: self.bounds.size.height * self.layer.anchorPoint.y)
newPoint = newPoint.applying(self.transform)
oldPoint = oldPoint.applying(self.transform)
var position : CGPoint = self.layer.position
position.x -= oldPoint.x
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
self.layer.position = position;
self.layer.anchorPoint = anchorPoint;
}
}
然后在viewDidLoad中设置锚点:
// set anchor point, below code will set anchor point in the bottom(y) middle (x) of myView
self.myView.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))
立即呼叫轮换
func Rotate(){
let rotation = UIViewPropertyAnimator(duration: 1, curve: .linear, animations: {
self.myView.transform = self.myView.transform.rotated(by: CGFloat.pi/2)
})
rotation.startAnimation()
}
我想在这段代码中旋转图片 pic1。我在下面放了一张我正在寻找的图片。我不想旋转整个图像我想从矩形的左下角旋转 90 度。你可以具体看我在绿色圆圈里说的是什么。旋转应该在按钮功能中进行。
import UIKit
class ViewController: UIViewController {
var pic1 = UIImageView()
var rot = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pic1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pic1)
rot.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(rot)
rot.backgroundColor = .blue
pic1.image = UIImage(named: "badMan.png")
NSLayoutConstraint.activate(
[
pic1.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.50),
pic1.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
pic1.topAnchor.constraint(equalTo: view.topAnchor),
pic1.leadingAnchor.constraint(equalTo: view.leadingAnchor),
rot.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.50),
rot.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
rot.topAnchor.constraint(equalTo: pic1.bottomAnchor),
rot.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
self.view.addSubview(pic2)
rot.addTarget(self, action: #selector(roate), for: .touchDown)
}
@objc func roate(){
}
}
您可以扩展 uiview 来设置锚点:
extension UIView{
func setAnchorPoint(anchorPoint: CGPoint) {
var newPoint = CGPoint(x: self.bounds.size.width * anchorPoint.x, y: self.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: self.bounds.size.width * self.layer.anchorPoint.x, y: self.bounds.size.height * self.layer.anchorPoint.y)
newPoint = newPoint.applying(self.transform)
oldPoint = oldPoint.applying(self.transform)
var position : CGPoint = self.layer.position
position.x -= oldPoint.x
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
self.layer.position = position;
self.layer.anchorPoint = anchorPoint;
}
}
然后在viewDidLoad中设置锚点:
// set anchor point, below code will set anchor point in the bottom(y) middle (x) of myView
self.myView.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))
立即呼叫轮换
func Rotate(){
let rotation = UIViewPropertyAnimator(duration: 1, curve: .linear, animations: {
self.myView.transform = self.myView.transform.rotated(by: CGFloat.pi/2)
})
rotation.startAnimation()
}