yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Objective-C iPhone Programming Lesson 18 - Finishing Multiple Buttons


5m read
·Nov 3, 2024

Hey guys, this is Mac Heads 101 with our 18th programming tutorial.

Now, in the previous tutorial, we made it so there were two buttons, but they had a little problem. That was that they bounced on the same velocity, and that resulted in all kinds of weird behavior where they could sometimes go off the screen and all of that. I also gave you guys a challenge at the end of the video. I said, uh, try to make it so that there are three buttons, which actually results in more problems because, you know, there are three buttons sharing one velocity.

But first of all, in order to add three buttons, you might have done it in a more complex way. But all you need to do here is say UIButton button3 = this and buttons addObject: button3. When we run this on the iPhone simulator, let's go ahead and see how this looks.

As you can see, there are three buttons, and there's probably more problems and all that good stuff. So, let's see. Alright, they all went off the screen except for one of them. So anyway, that is how to add three buttons. That's the answer to the challenge. Now, let me just update this to show you what it'll look like.

Now, what we're going to do in this tutorial is make it so that there is a different velocity for each and every button. So we're going to declare another array up here, and this array will be called buttonVels. What buttonVels will be is it'll be an array of stuff like this, um, and let's assume there are three buttons. So it's basically going to be an array of vectors or velocities.

The first item here is the X, and the second is the Y. First is the X, second is the Y. As buttons might be button one, button two, button three, the way this will work is this will be the velocity for the first button or for button one. This will be the velocity for button two, and this will be the velocity for button three.

Now, the way we're going to be representing these, these right here—sorry, these right here—is we're going to be using a CGPoint, which has an X and a Y. So each of these will be a CGPoint. Now, you can't put a CGPoint in an array, and I'll explain that to you in a second. But we're going to be wrapping it. So let's say buttonVels, let's keep that declaration, and let's go down here.

As we review, buttonVels equals nil right now, and we'll say buttonVels = [NSMutableArray array]. Now we'll say buttonVels = this. Alright, so for every button we add to buttons, we have to add buttonVels, or else, uh, it won't work. There has to be the same number of velocities in buttonVels as there are in buttons, otherwise serious problems will arise.

So let's add stuff to buttonVels right here. We’re populating our buttons, but let's say CGPoint v, and this will be the same velocity for everything. So all of the buttons will have 44 as their velocity X and Y—X velocity, Y velocity.

Now, this is great, but we can't put v in buttonVels. This would cause an error, and let me just show you what causes an error. First of all, uh, right here you can't do that—sorry about that—you have to say CGPointMake, because that's a function. But you really can't add a CGPoint to an array.

So in order to fix this, we have to wrap this CGPoint in something called an NSValue. So we're going to say NSValue valueWithCGPoint, and this basically returns an Objective-C object that we can put in an array. Um, it creates an Objective-C object around a C object, which is what v is—what a CGPoint is.

So we can do this three times for all three velocities, and now buttonVels equals {{44, 44}, {44, 44}, {44, 44}}, and this is our velocities. Let's build this so that way the silly error goes away.

So now what we have to do in our timer tick is we have to get this velocity, modify it, and then put it back in the array and save it. The first thing to do is we don't know right now in this loop which number button this is. We just know that this is a button. We don't know if it's the first button, if it's the second button, and we need to know that in order to get the place in buttonVels.

So we're going to say int buttonIndex = [buttons indexOfObject: clickMe], and this will tell us where in this array clickMe is. Now we're going to say NSValue *buttonVal = [buttonVels objectAtIndex: buttonIndex]. Now we're going to say CGPoint buttonVel = [buttonVal CGPointValue].

Wow, that's a mouthful! Anyway, now we can say buttonV.y and buttonV.x where it normally says velocity X and velocity Y, and we can say buttonB.x and buttonB.y.

Now we have to save this CGPoint, so we're going to say [buttonVels replaceObjectAtIndex: buttonIndex withObject: [NSValue valueWithCGPoint: buttonVel]]. This will remove the object at this index and then replace it with a new value, with our new velocity because our velocity can change if it hits the walls.

So, in order to update this in the array, we have to create a new NSValue with a new CGPoint, and we have to change this in the array. So there we go, we got the array working hopefully, and let's go ahead and build it and see how we do.

And now there's one more thing we can do is get rid of the two velocity X and velocity Y variables in the header because we really don't need those. We place it with this array. So now let's build in for the iPhone simulator.

Oh, and I have to get rid of this because we don't have these anymore, um, and let's go ahead and run it.

And woo! They're all bouncing independently. Very strange. I have to admit, this is pretty awesome. So that is how to have two or, you know, any number of separate velocities for every button that you add. To buttons, you have to also add a velocity here.

Now let's go ahead and make this more fun. Let's make a random velocity. So arc4random() % 4 + 2, and this will basically be a random number from 2 to 5, I guess. So let's copy this.

So now each of our buttons will have a separate random velocity, and this is even more strange. Yeah, so this is what I like to see—their independent buttons bouncing around with independent velocities.

This is no longer useful because we already have this. So in our next tutorial, I guess we're going to, um, be playing around with this some more. We're going to make it so it dynamically, uh, you can have the user, and they get enough points; it'll add another button or make some of the buttons smaller. There's just so much more fun we can have now that we're dynamically working with an array of buttons.

So that was our 18th programming tutorial. So thanks for watching. Mevs 101, subscribe, and goodbye!

More Articles

View All
Robot vs. Volcano: “Sometimes It’s Just Fun to Blow Stuff Up” (Exclusive) | National Geographic
It was a dedicated mission to take technology to the absolute limits and then destroy it. Oh yeah, those guys got to be careful. I don’t think we can get much closer to a big seismic event underwater than this. We were at Kavachi a couple years ago and we…
How to sell a $20,000,000 private jet 👀🛩️
What are you calling about? I’m calling about the business who’s inquiring. Although I’m not a broker, I am brokering an aircraft for a friend of mine. Okay, so 5343, what’s the price? That airplane’s price is at 99 currently over in Europe, or is it ou…
An Educational Video About Monkey Sex | National Geographic
Aside from humans, in particularly your humble narrator, what primate species spends the least amount of time climbing? Gelada monkeys have, like us, adapted for a life spent mostly on the ground. This has produced some unique aspects of gelada anatomy, s…
Regional climates | Weather and climate | Middle school Earth and space science | Khan Academy
What’s the weather usually like in the winter where you live? If you asked someone in Fairbanks, Alaska, they might describe below-freezing days and navigating through huge drifts of snow. If you asked someone else in Miami, Florida, they might tell you t…
Existence theorems intro | Existence theorems | AP Calculus AB | Khan Academy
What we’re going to talk about in this video are three theorems that are sometimes collectively known as existence theorems. So the first that we’re going to talk about is the intermediate value theorem, and the common thread here, all of the existence t…
Weather | Weather and climate | Middle school Earth and space science | Khan Academy
Have you ever gotten up in a good mood, put on your absolute favorite outfit, went outside to have a fun day in the sun, and then ended up soaking wet, caught in a sudden rainstorm? Yeah, same. Unfortunately, we can’t control the weather. Weather changes …