Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

swift CATextLayer string does not update the value

I'm trying to update circular bar CATextLayer every time button is pressed, but somehow it is not changing. Here is my code:`import UIKit

class ProgressBar: UIView {

private var backgroundLayer: CAShapeLayer!
private var foregroundLayer: CAShapeLayer!
private var progressTextLayer: CATextLayer!
private var progressDescriptionTextLayer: CATextLayer!

var progress: Int = 0 {
    didSet {
        didProgressUpdated()
    }
}

//MARK:- Custom shape drawing

override func draw(_ rect: CGRect) {
    let center = UIView(frame: rect).center
    let radius = CGFloat(55)
    let startAngle = -CGFloat.pi / 2
    let endAngle = startAngle + 2 * CGFloat.pi
    let lineWidth = CGFloat(12.0)
    
    let strokeShapeColor = UIColor(red: 73.0/255, green: 159.0/255.0, blue: 213.0/255.0, alpha: 1)
    
    let circularPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
    
    backgroundLayer = CAShapeLayer()
    backgroundLayer.path = circularPath.cgPath
    backgroundLayer.strokeColor = UIColor.lightGray.cgColor
    backgroundLayer.lineWidth = lineWidth
    backgroundLayer.fillColor = .none
    backgroundLayer.lineCap = .round

    foregroundLayer = CAShapeLayer()
    foregroundLayer.path = circularPath.cgPath
    foregroundLayer.strokeColor = strokeShapeColor.cgColor
    foregroundLayer.lineWidth = lineWidth
    foregroundLayer.fillColor = .none
    foregroundLayer.lineCap = .round
    foregroundLayer.strokeEnd = 0.5
    
    progressTextLayer = createProgressTextLayer(rect: rect)
    progressDescriptionTextLayer = createProgressTextDescriptionLayer(rect: rect)
    
    layer.addSublayer(progressDescriptionTextLayer)
    layer.addSublayer(progressTextLayer)
    layer.addSublayer(backgroundLayer)
    layer.addSublayer(foregroundLayer)
}

//MARK:- Progress text layer

private func createProgressTextLayer(rect: CGRect) -> CATextLayer {
    let width = rect.width
    let height = rect.height
    let fontSize = CGFloat(30)
    let offset = min(width, height) / 1.6
    
    let layer = CATextLayer()
    layer.string = "(progress)/12"
    layer.backgroundColor = .none
    layer.foregroundColor = UIColor.black.cgColor
    layer.fontSize = fontSize
    layer.frame = CGRect(x: 0, y: (height - fontSize - offset), width: width, height: fontSize + offset)
    layer.alignmentMode = .center
    
    
    return layer
}

//MARK:- Progress text description layer

private func createProgressTextDescriptionLayer(rect: CGRect) -> CATextLayer {
    let width = rect.width
    let height = rect.height
    let fontSize = CGFloat(18)
    let offset = min(width, height) / 2.2
    
    let layer = CATextLayer()
    layer.string = "Progresas"
    layer.backgroundColor = .none
    layer.foregroundColor = UIColor.black.cgColor
    layer.fontSize = fontSize
    layer.frame = CGRect(x: 0, y: (height - fontSize - offset), width: width, height: fontSize + offset)
    layer.alignmentMode = .center
    
    
    return layer
}

//MARK:- Did progress updated func

 func didProgressUpdated() {
    progressTextLayer?.string = "(Int(progress))/12"
    foregroundLayer?.strokeEnd = CGFloat(progress)
    print("(progress) from progress bar") // just checking if its updating value here
}

}

And here is Home view controller where button is

import UIKit import Foundation

class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let progressBar = ProgressBar()

@IBOutlet var studentLabel: UILabel!
@IBOutlet var studentsTableView: UITableView!

//MARK:- Button implementation

@IBOutlet var pickGroupButton: UIButton! {
    didSet {
        pickGroupButton.layer.cornerRadius = 25.0
        pickGroupButton.layer.masksToBounds = true
        pickGroupButton.layer.backgroundColor = CGColor(red: 73.0/255, green: 159.0/255.0, blue: 213.0/255.0, alpha: 1)
    }
}

@IBAction func pickGroupButtonPressed(_ sender: UIButton) {
    switch students.isEmpty {
    case false:
        progressBar.progress += 1
        studentLabel.isHidden = false
        addStudentToTheStage()
    case true:
        progressBar.progress = 0
        studentLabel.isHidden = true
        pickGroupButton.titleLabel?.text = "I? NAUJO"
    }
    print("(progressBar.progress)")
}

//MARK:- Students list

var students: [Student] = [
    Student(name: "hd", surname: "fdgh"),
    Student(name: "dfgh", surname: "fdgh"),
    Student(name: "dfgh", surname: "fdgh"),
    Student(name: "fgh", surname: "dfgh"),
    Student(name: "fdgh", surname: "fdgh"),
    Student(name: "fdh", surname: "fdgh"),
    Student(name: "fdh fdhg", surname: "fgh"),
    Student(name: "fdgh", surname: "fdgh"),
    Student(name: "fdh", surname: "dfgh"),
    Student(name: "fgh", surname: "fgh"),
    Student(name: "dfgh", surname: "dhf"),
    Student(name: "dfgh", surname: "dhfg")
]

override func viewDidLoad() {
    super.viewDidLoad()
    studentsTableView.delegate = self
    studentsTableView.dataSource = self

}

} //MARK:- Table view implementation

extension HomeViewController {

func numberOfSections(in tableView: UITableView) -> Int {
   // #warning Incomplete implementation, return the number of sections
   return 1

}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let header = "Studentai"
    
    return header.uppercased()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   // #warning Incomplete implementation, return the number of rows
   return students.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "StudentListCell", for: indexPath)

   let studentRow = students[indexPath.row]
    cell.textLabel?.text = studentRow.name + " " + studentRow.surname
   
   return cell

}

//MARK:- pick student

func addStudentToTheStage() {
    guard let randomStudent = students.randomElement() else { return }
    studentLabel.text = randomStudent.name + " " + randomStudent.surname
}

} `

question from:https://stackoverflow.com/questions/65895597/swift-catextlayer-string-does-not-update-the-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...