旋转图像视图,该图像视图应位于底部固定点的圆弧内
Rotating an image view which should be inside an arc with fixed point for bottom
我正在创建一个速度计。我的图像底部必须位于圆弧的中心点,我需要使用固定的中心点旋转图像。我不知道该怎么做。
private func drawHand(center: CGPoint) {
let handImage = UIImage(named: "hand")
handImageView = UIImageView(image: handImage)
handImageView.translatesAutoresizingMaskIntoConstraints = false
handImageView.bounds = CGRect(x: center.x, y: center.y, width: 100, height: bounds.height / 3)
handImageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)
handImageView.center = CGPoint(x: bounds.midX, y: bounds.midY)
addSubview(handImageView)
}
这是我的代码,我试图将图像居中放置在圆弧内,但我的图像却在左上角。
我尝试使用旋转
handImageView.transform = CGAffineTransform(rotationAngle: deg2rad(handRotation))
但没有任何工作正常。
请帮助我将我的图像设置在圆弧中心然后旋转它。
这一行:
handImageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)
您已将锚点设置为左边缘和垂直中心
改成这样:
// set the anchorPoint to Horizontal Center / Bottom Edge
handImageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
看看你是否得到了你想要的旋转。
这是一个完整的示例...箭头从零度开始(向上)——每次点击都会增加 10 度的旋转:
extension Double {
var degreesToRadians: Self { self * .pi / 180 }
var radiansToDegrees: Self { self * 180 / .pi }
}
class ViewController: UIViewController {
var handImageView: UIImageView!
var handRotation: Double = 0
override func viewDidLoad() {
super.viewDidLoad()
let handImage = UIImage(systemName: "arrow.up")
handImageView = UIImageView(image: handImage)
// so we can see the image view frame
handImageView.backgroundColor = .systemYellow
view.addSubview(handImageView)
// set imageView frame to 80 x 160
handImageView.frame = CGRect(origin: .zero, size: CGSize(width: 80, height: 160))
// set the anchorPoint to Horizontal Center / Bottom Edge
handImageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
// set image view's Anchor Point to the center of the view
handImageView.center = view.center
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// increment rotation by 10-degrees with each touch
// Zero-degrees is pointing UP (12 o'clock)
handRotation += 10
handImageView.transform = CGAffineTransform(rotationAngle: CGFloat(handRotation.degreesToRadians))
print("rotation:", handRotation)
}
}
我正在创建一个速度计。我的图像底部必须位于圆弧的中心点,我需要使用固定的中心点旋转图像。我不知道该怎么做。
private func drawHand(center: CGPoint) {
let handImage = UIImage(named: "hand")
handImageView = UIImageView(image: handImage)
handImageView.translatesAutoresizingMaskIntoConstraints = false
handImageView.bounds = CGRect(x: center.x, y: center.y, width: 100, height: bounds.height / 3)
handImageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)
handImageView.center = CGPoint(x: bounds.midX, y: bounds.midY)
addSubview(handImageView)
}
这是我的代码,我试图将图像居中放置在圆弧内,但我的图像却在左上角。
我尝试使用旋转
handImageView.transform = CGAffineTransform(rotationAngle: deg2rad(handRotation))
但没有任何工作正常。 请帮助我将我的图像设置在圆弧中心然后旋转它。
这一行:
handImageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)
您已将锚点设置为左边缘和垂直中心
改成这样:
// set the anchorPoint to Horizontal Center / Bottom Edge
handImageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
看看你是否得到了你想要的旋转。
这是一个完整的示例...箭头从零度开始(向上)——每次点击都会增加 10 度的旋转:
extension Double {
var degreesToRadians: Self { self * .pi / 180 }
var radiansToDegrees: Self { self * 180 / .pi }
}
class ViewController: UIViewController {
var handImageView: UIImageView!
var handRotation: Double = 0
override func viewDidLoad() {
super.viewDidLoad()
let handImage = UIImage(systemName: "arrow.up")
handImageView = UIImageView(image: handImage)
// so we can see the image view frame
handImageView.backgroundColor = .systemYellow
view.addSubview(handImageView)
// set imageView frame to 80 x 160
handImageView.frame = CGRect(origin: .zero, size: CGSize(width: 80, height: 160))
// set the anchorPoint to Horizontal Center / Bottom Edge
handImageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
// set image view's Anchor Point to the center of the view
handImageView.center = view.center
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// increment rotation by 10-degrees with each touch
// Zero-degrees is pointing UP (12 o'clock)
handRotation += 10
handImageView.transform = CGAffineTransform(rotationAngle: CGFloat(handRotation.degreesToRadians))
print("rotation:", handRotation)
}
}