我正在尝试以编程方式将编号按钮排列在一个圆圈中。 NSLayoutConstraint 锚定视图,而 UIView 的子类创建按钮圈。框架绕中心旋转,但按钮旋转不一致。除一次旋转外,所有旋转中的文本方向均相同。
for example
我用于排列按钮的 Swift 代码得到了改进Objective C 的早期成果。我还跟进了一些关于围绕屏幕中心旋转视图的有用建议,包括this one.
如果有人能告诉我如何改进我的代码,使 UI 对于每个屏幕尺寸和方向都保持一致,我将不胜感激。
这里是视图控制器
import UIKit
class ViewController: UIViewController {
var circle: CircleView?
override func viewDidLoad() {
super.viewDidLoad()
let circle = CircleView()
circle.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(circle)
let horizontalConstraint = circle.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let verticalConstraint = circle.centerYAnchor.constraint(equalTo: view.centerYAnchor)
NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint])
}
}
...以及UIView 的子类
import UIKit
class CircleView: UIView {
// MARK: Initialization
let points: Int = 10 // 80 25 16 10 5
let dotSize: CGFloat = 60 // 12 35 50 60 100
let radius: CGFloat = 48 // 72 70 64 48 45
var arcPoint = CGFloat(M_PI * -0.5) // clockwise from 12+ (not 3+)!
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
drawUberCircle()
drawBoundaryCircles()
}
...倒数第二个函数绘制彩色圆形背景
func drawUberCircle() {
// Create a CAShapeLayer
let shapeLayer = CAShapeLayer()
// give Bezier path layer properties
shapeLayer.path = createBezierPath().cgPath
shapeLayer.strokeColor = UIColor.cyan.cgColor
shapeLayer.fillColor = UIColor.cyan.cgColor
shapeLayer.lineWidth = 1.0
self.layer.addSublayer(shapeLayer)
}
func createBezierPath() -> UIBezierPath {
let path = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0),
radius: radius * 2,
startAngle: CGFloat(M_PI * -0.5),
endAngle: CGFloat(M_PI * 1.5),
clockwise: true)
return path
}
...而最后一个函数以圆弧绘制按钮
func drawBoundaryCircles() {
for index in 1...points {
let point: CGPoint = makeBoundaryPoint()
drawButton(point: point, index: index)
}
}
func makeBoundaryPoint() -> (CGPoint) {
arcPoint += arcAngle()
print(arcPoint)
let point = CGPoint(x: 0 + (radius * 2 * cos(arcPoint)), y: 0 + (radius * 2 * sin(arcPoint)))
return (point)
}
func arcAngle() -> CGFloat {
return CGFloat(2.0 * M_PI) / CGFloat(points)
}
func drawButton(point: CGPoint, index: Int) {
let myButton = UIButton(type: .custom) as UIButton
myButton.frame = CGRect(x: point.x - (dotSize/2), y: point.y - (dotSize/2), width: dotSize, height: dotSize)
myButton.backgroundColor = UIColor.white
myButton.layer.cornerRadius = dotSize / 2
myButton.layer.borderWidth = 1
myButton.layer.borderColor = UIColor.black.cgColor
myButton.clipsToBounds = true
myButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Thin", size: dotSize/2)
myButton.setTitleColor(UIColor.red, for: .normal)
myButton.setTitle(String(index), for: .normal)
myButton.tag = index;
myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
addSubview(myButton)
}
func buttonAction(myButton: UIButton) {
let sender:UIButton = myButton
print("Button \(sender.tag) was tapped")
}
}
EDIT
旋转 4 (如上所示)当 iPhone 被倒置时出现,并且由于接受的答案中解释的原因而无关紧要(见下文)。如果代码在实际设备而不是模拟器上运行,这一点会变得更加明显。
带有工作按钮的解决方案
对于按钮保持在中心位置的解决方案and工作,参考this