Swift Animations: Custom Counting Label - CADisplayLink

  Рет қаралды 34,997

Lets Build That App

Lets Build That App

Күн бұрын

Пікірлер: 142
@CodeWithChris
@CodeWithChris 6 жыл бұрын
Very cool! I've always wondered how that works. Thanks for demonstrating!!
@logicalsense6141
@logicalsense6141 5 жыл бұрын
CodeWithChris wow, amazing to see 2 of my favorite developers in 1 video. Note: I am not a developer but enjoyed watching both of your videos very much. Thank you for keeping me interested!
@sulemanali7268
@sulemanali7268 3 жыл бұрын
both of you worked great i always wondered
@mcnimi
@mcnimi 6 жыл бұрын
thank you for your content brian. i have learned most of my mobile app programming knowledge from you and i have been freelancing successfully for two years now. thank you.
@w0mblemania
@w0mblemania 6 жыл бұрын
Good video Brian, thanks. To remove that wobbling text (due to variable-width font digits), we can use: label.font = UIFont.monospacedDigitSystemFont(ofSize: 20, weight: .medium) This will use a monospace font _only_ for the digits, preventing the wobble. Cheers.
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Awesome, I didn't know about that.
@iamsanketray
@iamsanketray 5 жыл бұрын
Did not know that either....awesome
@shehzadali7087
@shehzadali7087 6 жыл бұрын
Once I was thinking about this animation and realized that UIView.animate will work for it but after watching your video I’m speechless 😶. Thanks a lot for this.
@keeper_scratch
@keeper_scratch 6 жыл бұрын
So I came up with this solution for animate text challenge, using string range. Declare endStringValue variable, and here is the code that goes into the else block: let stringOffset = Int(percentage * Double(endStringValue.count)) let stringEndIndex = endStringValue.index(endStringValue.startIndex, offsetBy: stringOffset) let stringRange = endStringValue.startIndex..
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Looks good, there's probably a cleaner way to write the above code in a few more lines instead of stuffing everything in one line though.
@keeper_scratch
@keeper_scratch 6 жыл бұрын
Yesss, indeed, edited the solution 😉
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Looks great, my version looks very similar to this. I hate that I have to google this offset/index code every time I need a substring.
@alcprado
@alcprado 6 жыл бұрын
I got into swift about 2 months ago and I'm loving your tutorials. I have been into many languagues and different channels in KZbin but to me you are in the top 10 in the whole KZbin. Thank you Brian.
@YogeshPateliOS
@YogeshPateliOS 6 жыл бұрын
Andre Prado Please Once check my channel too !! I hope my videos are helpful for you :-)
@fhdmsd
@fhdmsd 2 жыл бұрын
Really helpful to animate text changes. Thanks for the tutorial. One tip: Instead of using custom logic to control animation time, use CADynamicLink's preferredFramesPerSecond or the newly introduced in iOS 15 preferredFrameRateRange. This will make controlling animation time much easier.
@dimiutube
@dimiutube 4 жыл бұрын
Thanks for showing this, Brian! It's always a pleasure see you coding! Great job!
@katoji810
@katoji810 6 жыл бұрын
Now, Im a ios dev because of...you !!! Thanks you so much.
@udaykumareega5333
@udaykumareega5333 6 жыл бұрын
I like background score a lot. It will remember you whenever or wherever I listen ha ha..Have been watching your videos since my iOS training.😍😍
@alexN350z
@alexN350z 6 жыл бұрын
Very excellent tutorial, learnt a lot of tricks from this video.
@PtolemysEye
@PtolemysEye 6 жыл бұрын
I've been wanting to add some unique animations to my apps. Thanks Brian!
@MrBlaq
@MrBlaq 6 жыл бұрын
Great tutorial! More animation videos please!
@hectormejia499
@hectormejia499 6 жыл бұрын
I love this animations videos! This is the one thing I am the weakest when it comes to programming. Are you thinking about making an advanced (or complete) course on animations? That would be amazing, since I've seen very few (if any) out in the wild. Keep up the great work man!
@NITINAGAM
@NITINAGAM 6 жыл бұрын
Brian, again you did a great job. Nice and useful animation. Thank you for sharing!!!! Keep it up!
@shingetzu88
@shingetzu88 6 жыл бұрын
Thank you for the great tutorial Brian! So glad to see you making iOS stuff again. I hope you keep your focus on iOS and come out w/ more stuff!!!
@laurencastillo5104
@laurencastillo5104 4 жыл бұрын
Great video! You explained everything very well! How do you display the information as an Int instead of a Double?
@JohanAlbrectsen
@JohanAlbrectsen 6 жыл бұрын
Love this! More animation stuff please!! Arguably the most important thing in an app!
@awaisfayyaz7671
@awaisfayyaz7671 6 жыл бұрын
Great video Brain, I had seen this video a week ago. Then i wanted to find this video. I forgot that this was posted by you. I tried to find it using titles "animating numbers in swift" but could not find. Then i recalled that it was from your channel and finally found it. I would be great if you rename this video title something like "animating numbers in swift using CADisplayLink". Doing this will help others to find it easily. Especially those that are trying to animate numbers. As always, Great video. :). Keep up the good work Cheers
@coderacjhp9282
@coderacjhp9282 6 жыл бұрын
Thanks Brian 👍🏼 very nice explanation. I used this method for applying face morphing and adjusting the value with UISlider from zero to one in 2.5 seconds. Count-up working well but countdown finishing before animation duration! Do you have any suggestion about fixing this issue?
@coder_acjhp
@coder_acjhp 6 жыл бұрын
Thank you Brain. Nice job👍
@viralchauhan9397
@viralchauhan9397 6 жыл бұрын
Thanks Sir, I found new area of experiment..
@santoshjayaswal2451
@santoshjayaswal2451 6 жыл бұрын
You are amazing Brian 😍
@juanfrancisco9039
@juanfrancisco9039 5 жыл бұрын
Very cool animation!! How could we make to restart the animation when I tap on the view again?
@salihgole3990
@salihgole3990 6 жыл бұрын
Great! Thanks Brian.
@MoPharaoh
@MoPharaoh 6 жыл бұрын
Great video on one of the most fun topics to learn in iOS! keep them coming :)
@omidrostami2208
@omidrostami2208 2 жыл бұрын
Hi Brian, do you have the source Code answer for animating the Text challenge as well? my approach is kind of long code to show the codes here, looking to your approach for compare.
@P_F_D
@P_F_D 6 жыл бұрын
Very informative video! I learned a lot from this, and already started incorporating CADisplayLink in my projects! One question though: since CADisplayLink ties the animations to the framerate, what would happen if the framerate goes down and you start dropping frames? (let's say you're on a slow device, lots of activity, etc)
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
If your device is slow, your animation will be slow as well using CADisplayLink. Not much you can do about this.
@kamrandehghan7645
@kamrandehghan7645 4 жыл бұрын
Grrrrreat. thanx bro. saved me the day
@danuff
@danuff 5 жыл бұрын
OMG....Thank you, this is exactly what I need!!!
@christofzirkler3369
@christofzirkler3369 6 жыл бұрын
Hey Brian, great Video, thanks! I think we should call displayLink.invalidate() once we are done with the animation. Otherwise the handleUpdate() method gets called over and over again.
@Pepejeria
@Pepejeria 5 жыл бұрын
or just use pause if you are going to use the displayLink again. Invalidate should be called when the view controller is de-initialized.
@SevenDeMagnus
@SevenDeMagnus 6 жыл бұрын
Thanks, prof. Brian. God bless, Proverbs 31
@cinquain0
@cinquain0 5 жыл бұрын
I’m feeling the hat Brian
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
Nike: just do it.
@nizz4m
@nizz4m 6 жыл бұрын
You're amazing as always 🎊👌🏻
@tmn28100
@tmn28100 6 жыл бұрын
Thanks, Brian! :)
@needmore7249
@needmore7249 5 жыл бұрын
I love your Cap
@th3suffering
@th3suffering 5 жыл бұрын
How can you get this to work in a loop for a score counter that can go up and down and changes? Im running into a problem where this wont work past the initial animationDuration. How can i get it to rerun in my loop when the score goes up or down?
@SudhanshuSrivastavaIndia
@SudhanshuSrivastavaIndia 6 жыл бұрын
That's really awesome...
@한정희-i1r
@한정희-i1r 6 жыл бұрын
형 사랑합니다 매번 고마워요
@mukulbakshi28
@mukulbakshi28 6 жыл бұрын
Hello sir. Can you please give a tutorial on the demo you have shown like the text animation of cadisplay link like the textbis written. Please tell me how to do using this.
@adminoudom
@adminoudom 6 жыл бұрын
Nice video ever. Hello sir I am from Cambodia. If sir don't mind. I would you to make more video regarding clear code architecture of IOS development.Thank you.
@javishfri
@javishfri 6 жыл бұрын
like before watching only for Brian
@hamzaalmass8826
@hamzaalmass8826 6 жыл бұрын
Thank you very much , you’re amazing
@3gzotic
@3gzotic 6 жыл бұрын
Great video again thanks a lot Brian :) Also I cant find any good video about emitter layer animations flame, burning, confetti, smoke kind of particle animations so I cant figure it out them completely If you have time could you prepare basic video about emitter layer :)
@MrMm2004
@MrMm2004 6 жыл бұрын
kzbin.info/www/bejne/eZiYeK2BqLJ5ba8
@3gzotic
@3gzotic 6 жыл бұрын
Thanks a lot marcus for advise :) I watched video before Its a very good video to figure out how emitter works but I am trying to find some deeper video about emitter layer. I checked a lot in web but i couldnt fing I think Its time to get my hands dirty :)
@lennartphilipp6699
@lennartphilipp6699 6 жыл бұрын
Can you please make a video about the challenges? I think some people would really appreciate this (including me)
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
All the answers are already on google, it only takes a few seconds to search for them. I strongly encourage people to learn how to go google for answers.
@juanfrancisco9039
@juanfrancisco9039 5 жыл бұрын
Hey, I don't know if you still check the comments, but I'm trying to relaunch this animation after clicking a button or something else, but no luck, how could I do this?
@ModMyTech
@ModMyTech 6 жыл бұрын
Awesome brudda!
@JunaidKhan-ny8tu
@JunaidKhan-ny8tu 6 жыл бұрын
hi Brian , how I can do the animation that you described from 2:17 to 2:42 . Its amazing but didn't figure it out :-(
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
I remember using a percentage to calculate the range of the string that I'm animation, its somewhat tricky if you don't get the new line characters correct.
@linhnguyenthe5727
@linhnguyenthe5727 6 жыл бұрын
For second challenge i think i will add each character like you add from start to end value.
@kelvinfok
@kelvinfok 6 жыл бұрын
Brian, when can we see some of that styled hair in the "Obj-C Getting Started" video?
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
I've been on a lazy hair month, finally got a hair cut though.
@pradeepchandrasekaran272
@pradeepchandrasekaran272 6 жыл бұрын
Hi, How to animate the label on viewwillappear?
@godwinebikwo6544
@godwinebikwo6544 5 жыл бұрын
Any new courses on LBTA coming soon?
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
Doing a little bit of contracting work nowadays. Might be taking a break from doing courses for a month or two.
@godwinebikwo6544
@godwinebikwo6544 5 жыл бұрын
@@LetsBuildThatApp ok bro Good luck and anticipating your return.
@JacksMacintosh
@JacksMacintosh 6 жыл бұрын
So I may be misunderstanding this, but it seems like the display link, once the label reaches the target value, is just constantly setting the label's text to the endvalue, 60 times a second. Does that have any hit on performance? can you just call a sort of "break" statement once it reaches the end? Thanks! :)
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Yes, at the end of the video I mention as a challenge to remove the display link once the animation is done.
@user-pr6up2vw9r
@user-pr6up2vw9r 6 жыл бұрын
But what is about performance? Why do you need this to be called each 60 times per a second. Is not it better to use NSTimer or dispatch_after with certain amount of time you need? I guess this is better for performance.
@kav04
@kav04 6 жыл бұрын
you should read documentation - it says it doesnt make animation perself, it just synchronize drawing to the refresh rate of the display. The refreshing 60 fr per second already exist actually it always exist
@user-pr6up2vw9r
@user-pr6up2vw9r 6 жыл бұрын
This is not animation actually. You don't need this periodic code be called so often. Otherwise there should not be needed in NSDate to check time.
@thetechsack
@thetechsack 6 жыл бұрын
displayLink.remove(from: .main, forMode: .defaultRunLoopMode)
@ibrshimnasser2001
@ibrshimnasser2001 6 жыл бұрын
Can I do something like this for x in 1...999{ label.text = "\(x!)" }
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Sure, you can give that a try and see what happens :)
@P_F_D
@P_F_D 6 жыл бұрын
that entire loop would execute almost instantly. You would need some sort of mechanism to delay each loop execution. One possible way to do it would be to call a function recursively using a DispatchQueue.main.AsyncAfter or "performSelector after delay" method, but CADisplayLink is certainly preferred and a lot less "hackey"
@BloodletterQuill
@BloodletterQuill 6 жыл бұрын
Wait...did I miss the point were you actually stop the animation? Right now it will just keep running forever in the background? Sure we don't see it counting up but it will never stop looping?
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Yes, at the very end of the video I challenge you to figure that step out on your own. I think I'm moving away from providing all the answers and requiring people to think a little on these exercises. I want this to be an engagement experience after learning the core concepts.
@BloodletterQuill
@BloodletterQuill 6 жыл бұрын
Ok must have missed it :) thx
@TheSmellOfCode
@TheSmellOfCode 6 жыл бұрын
That's a great approach.
@P_F_D
@P_F_D 6 жыл бұрын
You can add a check to call .invalidate() on the displaylink when elapsedTime passes the duration
@YonghuaChen-b9t
@YonghuaChen-b9t 6 жыл бұрын
animation definitely can be a university half-year course TnT
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Yeah definitely a lot of fun can be had with animations. It’s one of my favorite topics.
@yashbhatt8581
@yashbhatt8581 6 жыл бұрын
brian please provide link for the intro music .
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Its called Dream, it is available for free on youtube.
@SSA-X002
@SSA-X002 6 жыл бұрын
good stuff, thank you!
@magic0x0
@magic0x0 6 жыл бұрын
Great!!
@heyrahulrs
@heyrahulrs 6 жыл бұрын
Awesome!
@user-pr6up2vw9r
@user-pr6up2vw9r 6 жыл бұрын
Why don't use dispatch after or NSTimer for that?
@Pepejeria
@Pepejeria 5 жыл бұрын
Never use NSTimer for animations.
@bdj0107tt
@bdj0107tt 6 жыл бұрын
Please do a “How to make a game on IOS with Swift” tutorial.
@mryup6100
@mryup6100 6 жыл бұрын
yah i agree
@MrSaud58
@MrSaud58 6 жыл бұрын
Please I want an example 2:17 to 2:42 Show the letters, character, and the next character
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
For the character animation, you can do it with 3 more lines of code. Its honestly pretty simple if you just think about it a little bit.
@MrSaud58
@MrSaud58 6 жыл бұрын
Thank you, I do not care about animation I want to write a letter and then next letter to the end Letters
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Isn’t that the animation? One char after another.
@MrSaud58
@MrSaud58 6 жыл бұрын
I want a code : char after another
@shink303
@shink303 5 жыл бұрын
Please make a bonus video on how to display the text like you showed as an example. i'm having the hardest time trying to figure it out.
@akilawasala
@akilawasala 6 жыл бұрын
Thanks a lot
@iwintersun
@iwintersun 5 жыл бұрын
Sup dude, I don't know if you still check the comments because the video is almost a year old but something I noticed is the handleupdate function is still getting called each frame and that's not good. How to stop it from running after it finished counting?!
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
You would invalidate the timer. Google search the answer bud.
@iwintersun
@iwintersun 5 жыл бұрын
Lets Build That App Thx for answering dude. Keep up the awesome work.
@alexN350z
@alexN350z 6 жыл бұрын
It probably should be countingLabel.frame = view.bounds, just keep good practice for everyone.
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Definitely. I didn’t want to get into what bounds vs frame meant.
@arindamsantra2148
@arindamsantra2148 6 жыл бұрын
sir need some tutorial on animation.
@ammarahmad9004
@ammarahmad9004 6 жыл бұрын
I use this "dispalyLink.preferredFramesPerSecond = " to reduce the frame rate in text animation, and it's work, did this effective way to do it???
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Good job, lots of different properties to explore.
@mehmedtukulic3376
@mehmedtukulic3376 3 жыл бұрын
Guys , don' forget to remove the link from main thread after animation.
@samsonlohasaptawee531
@samsonlohasaptawee531 5 жыл бұрын
how to reset elapsedTime
@samirdesigner3324
@samirdesigner3324 6 жыл бұрын
how to change app language without changing device language in ios
@kav04
@kav04 6 жыл бұрын
I end up with this solution for the second challenge - @objc func handleUpdate2() { let now = Date() let elapsedTime = now.timeIntervalSince(animationStartTime) let countingText = myText.allPrefixes1[ Int(elapsedTime) * 4 ] countingLabel.text = "\(countingText)" } extension String { var allPrefixes1: [Substring] { return [""] + self.indices.map { index in self[...index] } } }
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Looks good, I'd recommend use the range and index methods of String to calculate the amount of text that should be shown.
@zhuzi_2037
@zhuzi_2037 6 жыл бұрын
怎么又回到Swift了,不是React Native吗?
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
如果没记错的话 我是没拍过RN对吧
@rickywitherspoon2861
@rickywitherspoon2861 4 жыл бұрын
Here it is in SwiftUI:
@rickywitherspoon2861
@rickywitherspoon2861 4 жыл бұрын
struct ContentView : View { @ObservedObject var displayLink = DisplayLinkManager() var body : some View{ Text(displayLink.value).onAppear{ self.displayLink.run(startValue: 0, endValue: 2000000, duration: 5) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } class DisplayLinkManager: ObservableObject{ @Published var value = "" var startValue: Double! var endValue: Double! var duration: Double! var asInteger : Bool! let animationStartDate = Date() var displayLink: CADisplayLink? func configureDisplayLink(){ } func run(startValue : Double, endValue : Double, duration: Double, asInteger : Bool = true){ self.startValue = startValue self.endValue = endValue self.duration = duration self.asInteger = asInteger let displayLink = CADisplayLink(target: self, selector: #selector(handleUpdate)) displayLink.add(to: .main, forMode: .default) } @objc func handleUpdate() { let now = Date() let elapsedTime = now.timeIntervalSince(animationStartDate) if elapsedTime > duration { value = String(format: "%.\(asInteger ? 0 : 3)f", endValue) displayLink?.invalidate() displayLink = nil } else { let percentage = elapsedTime / duration let newValue = startValue + percentage * (endValue - startValue) value = String(format: "%.\(asInteger ? 0 : 3)f", newValue) } } }
@fawadfarooq
@fawadfarooq 6 жыл бұрын
Both challenge in one code var startText = " " let endText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged." var startValue = 0 @objc func handleUpdate(){ let endTextValue = endText.count - 1 let index = endText.index(endText.startIndex, offsetBy: startValue) self.countinglabel.text = "\(startText)" startText += String(endText[index]) startValue += 1 if startValue > endTextValue{ displayLink?.invalidate() } print(endTextValue) }
@MrSaud58
@MrSaud58 6 жыл бұрын
let alertController = UIAlertController(title: "Thanks", message: "Excellent teacher", preferredStyle: .Alert) let defaultAction = UIAlertAction(title: "I love you", style: .Default, handler: nil) alertController.addAction(defaultAction) presentViewController(alertController, animated: true, completion: nil)
@David-pi9dl
@David-pi9dl 5 жыл бұрын
Thanks a lot
@Sam-lk1ci
@Sam-lk1ci 6 жыл бұрын
Awesome!
@Sam-lk1ci
@Sam-lk1ci 6 жыл бұрын
Awesome!
@alexeykhomych1743
@alexeykhomych1743 2 жыл бұрын
thanks a lot
Swift Animations: Facebook Shimmer using Gradient Masks
20:44
Lets Build That App
Рет қаралды 43 М.
Swift Animations: Hit Testing Facebook Like (Ep 2)
37:43
Lets Build That App
Рет қаралды 12 М.
Mastering Generics: Is This The Most Advanced Swift Code You've Seen?
26:56
Lets Build That App
Рет қаралды 43 М.
Advanced Swift: Making Programmatic Auto Layout Easy through Extensions
25:38
Mastering Bezier Curves in iOS | Swift
19:13
iCode
Рет қаралды 10 М.
Swift Grouped Messages: Easy Auto Sizing Cells (Ep 1)
27:12
Lets Build That App
Рет қаралды 50 М.
Swift Animations: Awesome Facebook Pop Up! Press and Hold (Ep 1)
25:16
Lets Build That App
Рет қаралды 33 М.
Advanced Swift Generics: Best Solution to Eliminate Code Duplication!
17:18
Lets Build That App
Рет қаралды 38 М.
Swift: How to Migrate MVC to MVVM & Intro Unit Testing
33:11
Lets Build That App
Рет қаралды 116 М.