in Education by
I'm making a little game and I'am having trouble updating an UILabel I created to display the current score. Every time I try to update the score label the entire screen freezes up for a split second, all the objects from the previous frame remain on the screen while the next frame is drawn on top of it with the score label not updating. This problem only occurs when I try to update the UILabel otherwise everything runs fine without any hiccups. I read that updating an UILabel needs to be done on the main thread and have tried doing so using Dispatch.main.async but the problem still occurs. Below I posted the code which has been causing me problems. fileprivate let scoreLabel: UILabel = { let newLabel = UILabel() newLabel.textAlignment = .center newLabel.textColor = .white newLabel.text = "0" newLabel.translatesAutoresizingMaskIntoConstraints = false return newLabel }() var score: Int = 0 fileprivate lazy var displayLink: CADisplayLink = { let newLink = CADisplayLink(target: self, selector: #selector(update)) return newLink }() @objc func update() { score += 1 //Method 1: cause screen split second screen lockup scoreLabel.text = "\(score)" //doesn't work //Method 2: cause screen split second screen lockup DispatchQueue.main.async { self.scoreLabel.text = "\(score)" //also doesn't work self.scoreLabel.setNeedsDisplay() } } func setupLayout() { view.addSubview(scoreLabel) NSLayoutConstraint.activate([ scoreLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor), scoreLabel.centerXAnchor.contraint(equalTo: view.centerXAnchor) ]) } override func viewDidLoad() { super.viewDidLoad() setupLayout() displayLink.add(to: .main, forMode: .default) } This is puzzling to me because this code worked in Objective-C and Swift below version 4. Any ideas as to what I'm doing wrong? Also this is on Swift version 4.2 UPDATE: So I found the solution. Apparently autolayout is what was causing the problems. To solve this problem I found 2 methods: Inside DispatchQueue.main.async, first deactivate your layout constraints. Then update your label and reactivate your layout constraints and call layoutIfNeeded(). Instead of using autolayout setup your label manually setting the label.frame with a CGRect and the position using label.center and a CGPoint. After that you should be able update your label without problems. Hopes this helps anyone else that might have ran into this problem. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The display link update is for custom drawing your views, not to update your subviews (they draw themselves already); do you really need your label redrawn 60 times a second? Just update the text and the system will flag the label as dirty and get around to updating it during the next redraw; you do not need to force a timing. And if you do need something like a counter, so it with a regular timer and not a displaylink timer.

Related questions

0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I make an alarm clock app, and I want when my device is locked and alarm start calling see a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I am using Realm with Swift 3 in my iOS app. I have the following code //Find all records for the day func ... { let predicate = NSPredicate(format: "date >= %@ and date...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    Recently implemented trailingSwipeActionsConfigurationForRowAt , where after swiping from right to left showing two options ... , JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    Is it possible to get access to dismiss() in an SKScene class? Dismiss is a method available from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I've inherited a codebase that implements a half-baked implementation of custom intents and shortcuts. One ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am working on a project with spark and scala and I am new to both but with lot of help from ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I already can update my data from database but the problem is i just want to update one data, but when I enter the ... all of data in the database table like this this is View:...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm implementing In App purchases which work quite well. I'm implementing both Auto-Renewable and Non- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I'm implementing In App purchases which work quite well. I'm implementing both Auto-Renewable and Non- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
...