How do I make subview appear in UI immediately after adding it? - ios

Essentially I want a label to appear as a subview when I add it and not wait until the current thread has finished what it is doing. In the below example, the label appears in the UI of the app at the same time 'Awake' is printed. Do you know how I can get the subview to appear in the UI before the thread sleeps?
#IBAction func ButtonTapped(_ sender: Any) {
let label = UILabel(frame: self.view.bounds)
label.text = "Label Text"
self.view.addSubview(label) // I want this to appear on the UI before...
sleep(3)
print("Awake") // ... this is printed
}
And enclosing the addSubView() line within DispatchQueue.main.async {} doesn't fix the issue.
Thanks.

Thanks to Paulw11, I figured out that I need to do the processing on a background thread. Simple solution in swift:
#IBAction func ButtonTapped(_ sender: Any) {
let label = UILabel(frame: self.view.bounds)
label.text = "Label Text"
self.view.addSubview(label) // I want this to appear on the UI before...
DispatchQueue.global(qos: .background).async {
sleep(3)
print("Awake") // ... this is printed
}
}

Related

How do I start and stop Confetti View on a single UIbutton?

I am a beginner in Xcode. I am trying to make an app which displays confetti on a button click. However, when I press the button, the confetti is displayed but it never stops. It blocks the buttons and the view. I am using a Cocoapod - SAConfettiView.
This code in on Xcode 10. I believe that the Confetti blocks the main view. I use playConfetti as IBaction function.
#IBAction func playConfetti(_ sender: UIButton, forEvent event: SAConfettiView) {
let confettiView = SAConfettiView(frame: self.view.bounds)
confettiView.intensity = 1
confettiView.type = .Star
confettiView.startConfetti()
view.addSubview(confettiView)
}
Do I have to use the If-Else statement?
Thank you!
Updated Answer
To stop the SAConfettiView call the function,
confettiView.stopConfetti()
Change your Button Action code to the following:
let confettiView = SAConfettiView()
#IBAction func startClicked(_ sender: Any)
if confettiView.isActive() {
confettiView.stopConfetti()
confettiView.removeFromSuperview()
}
else{
confettiView = SAConfettiView(frame: self.view.bounds)
confettiView.intensity = 1
confettiView.type = .Star
confettiView.startConfetti()
view.addSubview(confettiView)
self.view.bringSubviewToFront(sender as! UIButton)
}
}

NVActivityIndicatorView not working on button press

I have the following code for a button in my application to test my implementation of NVActivityIndicatorView:
#IBAction func goButtonPressed(_ sender: Any) {
self.startAnimatingActivityIndicator()
sleep(2)
self.stopAnimatingActivityIndicator()
}
The view controllers in my application also have this extension:
extension UIViewController: NVActivityIndicatorViewable {
func startAnimatingActivityIndicator() {
let width = self.view.bounds.width / 3
let height = width
let size = CGSize(width: width, height: height)
startAnimating(size, message: "Loading...", type: NVActivityIndicatorType.circleStrokeSpin)
}
func stopAnimatingActivityIndicator() {
self.stopAnimating()
}
}
The loading animations work elsewhere in the same view controller (i.e., the viewDidLoad() function) but for some reason I'm unable to get the loading animation to work on this button. The button is connected correctly as the application does sleep for the appropriate amount of time, but the loading animations fail to run.
Thanks in advance for the help!
The Indicator won't spin because the main thread is asleep. Use a 2 second timer to turn off the spinning instead.
#FryAnEgg coming in with the solution! The sleep(2) was preventing the loading animations from running.
Here's my updated code:
#IBAction func goButtonPressed(_ sender: Any) {
self.startAnimatingActivityIndicator()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.stopAnimatingActivityIndicator()
}
}

how to create tappable text in uitextview in swift

I want to create tappable text when I tap on those text then fire some action like call any function do some operation of that text I catnap on any text not on whole uitextview
Try this -
override func viewDidLoad() {
super.viewDidLoad()
// You must set the formatting of the link manually
let linkAttributes: [NSAttributedStringKey: Any] = [
.link: NSURL(string: "https://www.apple.com")!,
.foregroundColor: UIColor.blue
]
let attributedString = NSMutableAttributedString(string: "Just click here to register")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10))
self.textView.delegate = self
self.textView.attributedText = attributedString
self.textView.isUserInteractionEnabled = true
self.textView.isEditable = false
}
If I understand your question correctly, you want to click on a text view and call a function. You can use UITapGestureRecognizer.
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelTapped))
textView.addGestureRecognizer(tapGestureRecognizer)
}
#objc func labelTapped(){
print("Do something")
}
I've faced with problem same like your and I've tried many things to resolve. The best approach will be use Atributika (or similar) library. You will not regret this decision. It's easy to use and have a lot features.
https://github.com/psharanda/Atributika
If I understand your question properly then you could just add a button then change the button text to the text you want. Then like normal text you can mess with the colour, border or fill. After you have added this to your app link it up as an action with your view controller.swift file
Here is an example from my app 'Health Dash':
Image 1
Image 2
Then the swift 4 code:
import UIKit
class FriendsViewController: UIViewController {
#IBAction func exampleText(_ sender: Any) {
print("When you click this button something happens")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Here is an image of what it should look like:
Image 3

How can i reset UIlabel attributes to default settings in swift

I created a UIlabel named etiket. then I created an action button and I made changes like color change on it. it's a sample:
#IBAction func changeColor(_ sender: Any) {
etiket.textColor = UIColor.red
then I created again an action button and now when I pressed the button I want to reset UIlabel to default settings.
I tried this method
#IBAction func sifirla(_ sender: Any) {
etiket.text = "nil"
when I tried this method UIlabel completely disappeared.
How can I reset UIlabel attributes to default settings?
I did some research, but I could not find exactly what I wanted.
You need to create a label dynamically and assign a tag value to it.
func createLabel() -> UILabel {
let lbl = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 55))
lbl.tag = 101
//set other properties of label
return lbl
}
In changeColor method you need to get label by tag value and set its color.
#IBAction func changeColor(_ sender: Any) {
if let lbl = self.view.viewWithTag(101) as? UILabel {
lbl.textColor = UIColor.red
}
}
In sifirla method you need to get label by tag value and remove it from its super view and create a new label and add it in your current view, so by adding a new UILabel instance you will get default settings of label.
#IBAction func sifirla(_ sender: Any) {
if let lbl = self.view.viewWithTag(101) as? UILabel {
lbl.removeFromSuperview()
let newLbl = self.createLabel()
self.view.addSubview(newLbl)
}
}
Note: If you are using autolayout then you need to set constraint to label instead of frame.

iOS: Swift: UISlider editingDidEnd not being called

I've set up a function for editing did end with a slider, but it doesn't look like it's being called. I've thrown a print statement into it, and a breakpoint. Is there something else that needs to be done to trigger a function when my user lets go of the slider?
#IBAction func sliderEditingDidEnd(sender: UISlider) {
print("did end");
}
Use the event "Value Changed". If you just want updates for the final value, alter UISlider.continuous
var slider = UISlider()
slider.continuous = false
//add slider to view
#IBAction func valueChanged(sender: UISlider) {
println("Value changed.")
}
//prints "Value changed." once upon releasing slider.
If the slider needs to be continuous, you can implement an event for UIControlEvent.TouchUpInside.
Or in code:
override func viewDidLoad() {
super.viewDidLoad()
mySlider.addTarget(self, action: "userReleasedSlider:", forControlEvents: UIControlEvents.TouchUpInside)
// Do any additional setup after loading the view, typically from a nib.
}
func userReleasedSlider(slider: UISlider) {
print("User released slider.")
}
I think what you are trying to do is to get the notification when the value is changed. Then you have to set event to 'Value Changed' not 'Editing Did End'.
If you set both Touch Up Inside and Touch Up Outside to point to your #IBAction then you can get the function to fire when the user finishes sliding and keep the slider as continuous.

Resources