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

Objective-C iPhone Programming Lesson 13 - A Grid of Images


5m read
·Nov 3, 2024

Hey guys, this Mac has gone on with our 13th iPhone programming tutorial. In this video, I'm going to be showing you how to make an app that has a grid of images. When you click an image, it'll get big, and when you click it again, it will go back to normal size.

So, let's open Xcode, create a new project, and I'll call it Image Grid. Under Classes, we're going to create a new file, and we're going to call this file. We're going to make it a subclass of UIView and call it Touch. To make this view, it's not going to be the same as we've been doing with our Core Graphics because we're going to have multiple versions of this view, and each one of these views or instances will have an indicator.

So, we're going to have a UIImageView internal and I'm going to have a method internal which will return the internal view. Alright, so we're going to go here and I say return internal and then define it with frame. We're going to say internal equals UIImageView alloc initWithFrame. We're going to set the background color to black color and the content mode to scaleAspectFill.

Now, we’re going to add internal as a subview. Auto release means that after we added it as a subview, it will release automatically. This way, we don't have to worry about memory leaks.

Alright, now up here in our viewDidLoad of our main view controller, we're going to use something called a loop to basically lay out a bunch of views. So, I'm going to have two variables and I have to define rows and columns. Now if you recall, rows are going to be the height when columns then meet away.

So, we'll set it for int y equals 0, y less than rows, y plus plus; and for x equals 0, x less than columns, x plus plus. Now right now x and y don't represent coordinates. They'll be a number 0, 1, or 2 because the max it can be is this.

So first of all, we're going to get the size. We're going to declare a CGSize which will be the size of each column or each block.

Also, blockSize equals CGSizeMake(self.view.bounds.size.width / columns, self.view.bounds.size.height / rows). Alright, here we're going to declare our CGPoint blockOrigin equals CGPointMake(x * blockSize.width, y * blockSize.height).

Now we’re going to declare CGRect blockFrame. BlockFrame.size will equal blockSize and blockFrame.origin will equal blockOrigin. Alright, now we have to actually create and attach our image view.

So, we’re going to declare TouchImageView tiv = TouchImageView alloc initWithFrame:blockFrame. Now, we’re going to configure this. Normally, we'd set images here, but for now, I'm going to say self.view addSubview:tiv, and that's all we have to do.

Now, as you recall, we made the background color of TI be black and we're adding enough TIVs to fill the whole screen. So, when we run this, first of all, I have to import it. So, we have to import it in our header. But anyway, when we run this, I have a prediction that it will be a black screen, and I'm correct: it's a black screen.

So, I'm just going to set it so it runs on our iPhones. Next up, alright, so now let's take a look at setting some images. So I'm going to take a screenshot of some random part of my desktop and this is going to be the image that we show each image block.

So here, we're going to say UIImage *image equals [UIImage imageNamed:@"image.png"] and then say internal.image = image. Now, if you recall, internal is going to return our image view that fills up this block.

So, essentially, this will make every single block have the image we want. As you can see, it's a grid of these blocks. I'll run the iPhone simulator so you can get an idea to see that it's actually filling the full screen.

So, there you see it. Now, what we're going to do is make it so when you click on one of the blocks it makes it the full size of the screen. So, we're going to declare two methods in our class: makeFull and makeSmall, and we're going to have two more things here: we have a BOOL isLarge.

Alright, so let's go now here and let's implement makeSmall. I say UIView beginAnimations:nil context:nil because we're going to animate it. You want to set UIView setAnimationDuration and then set self.frame = original.

Now, we need to set original. We're going to copy this here along. So, superView.bounds will be the size of the original frame. And this is what will allow us to make it full or backing up the original frame.

When we make small, we restore the original frame. Now in order to call either of these methods, oh and another thing we're going to do is set that boolean: so isLarge. I guess here, and isLarge = NO here.

Now in order to call these, we have to have a method on touchesBegan. We're going to say if isLarge, then self makesSmall else self makesFull. And you'll notice that by default, isLarge will always be NO, so it will scale up when we click on it.

Now, let's try running this and we click it, and it moves there. That's because our image view isn't moving with it. So, we're setting the frame of the big view, but we have an internal image view that we're not resizing.

So, we're going to do here and make full. I say we're going to say internal setFrame and here we're going to once again say internal setImage. And we're going to do these actions in the animation block, definitely spectacular.

So, let's go ahead and run this one more time. It does resize up, but unfortunately once again it's being covered by the other views. So, the bottom one will obviously be on top of this one as you can see.

So, in order to make the view go to the top or make it full, we're going to set self.superview bringSubviewToFront:self. And that will make sure that we're on top. We need to be forward, so as you can see, if I tap anywhere it becomes full screen.

And if I have to fast, it looks awesome, but that's for another tutorial. Anyway, that is how to make a little grid app. In the next video, I'm going to be showing you how to do this with a bunch of different images so you can actually have a different one for each square.

So, thanks for watching! Be sure to subscribe and give a thumbs up!

More Articles

View All
What Actually Causes Dandruff?
Hey! This episode was sponsored by Head & Shoulders. A hundred and twenty-five million years ago, in what is now China, dinosaurs walked the earth, and a few species of small feathered dinosaurs climbed trees. This is Sinornithosaurus. Although they c…
Fishing With Dynamite Is Harmful—Why Does It Persist? | National Geographic
[Music] You can come out here on a fine morning and you know there’ll just be ramp and blasting in areas where there may be tuna feeds, or if there aren’t tuna feeds, then they may target the reefs. I would say probably for the last 5 years it’s at least …
The Ideal Digital Coin?
If you want a digital currency, you have to deal with something different. I don’t think that the stable coins are good, uh, uh, because then you’re getting a fiat currency again. I think that what you really would, what would be best, is an inflation-lin…
O'Leary Ventures President Talks Mortgages, Wines and Bags of Cash!
Now this is the story of a young man, a law school graduate, who, uh, paid off his student loan, huge student loan, with cash. How do you do something like this? We’re joined now by Alex Kenji of Toronto, president of O’Leary Ventures, a startup investmen…
Selina Tobaccowala at the Female Founders Conference 2016
All right, Cellino. We have so much to talk about, and you know I’m very excited for this because, um, I know you were just introduced as the CTO and president of SurveyMonkey, which is where you’ve been for the past most seven years now. Six now, six and…
AMA with Sal Khan on AI + Education
But for now, I want to kick things off with a question that Aaron had asked in the Q&A and got a lot of upvotes, and that was: what role do you envision generative AI having in education beyond just AI-enabled software and apps, as it pertains to the …