簡體   English   中英

如何在Swift中以編程方式旋轉一組按鈕?

[英]how do I rotate a group of buttons programmatically in Swift?

我試圖以編程方式安排編號按鈕。 當UIView的子類創建按鈕圈時,NSLayoutConstraint錨定視圖。 框架圍繞中心旋轉但按鈕旋轉不一致。 除了一次旋轉外,文本方向相同。

  for example

在此輸入圖像描述

我安裝按鈕的Swift代碼改進了Objective C中的早期工作 我還跟進了一些有用的建議來旋轉屏幕中心的視圖,包括這個

如果有人能告訴我如何改進我的代碼,那么UI對於每個屏幕尺寸和方向都是一致的,我將不勝感激。

這是Viewcontroller

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

編輯

當iPhone被顛倒時出現旋轉4( 如上所示 ),並且與接受的答案中解釋的原因無關( 見下文 )。 如果代碼在實際設備而不是模擬器上運行,這將變得更加明顯。

帶工作按鈕的解決方案

對於按鈕保持在中心工作的解決方案,請參閱

對於iPhone,這種行為是正確的。 你通常不支持顛倒旋轉,因為如果用戶必須接聽電話會讓人感到困惑; 它在蘋果HIG指南中。 在設備方向下的常規選項卡上查看項目設置。 默認情況下,禁用“倒置”。 如果要支持倒置旋轉,請檢查它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM