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

Objective-C iPhone Programming Lesson 12 - Resizing with Multi-touch


4m read
·Nov 3, 2024

Hey guys, this is Black Hair Salon, but our 12th iPhone programming tutorial. In this video, I'm going to be showing you how to use multi-touch to resize the core graphics drawing. This is useful! I actually use this in Zoomify for selecting your cropping, so it's pretty cool.

Now, there's no way built-in to do this, but we can use Apple's built-in multi-touch APIs to resize things, do all this stuff. So let's go to File, New Project, create an application. If you-based application, I'll call it Resizer. Alright, now under classes, we're going to, like normal, create a new UIView class. Good, then I'll call it MayView, and I'll set it up in Interface Builder, so it's our main view. Subscribe this.

Alright, so now this is our main. This will get drawn. So we're going to have a CGFloat diameter, and you'll see what this is in a second. And we're going to make a void, well, and it's going to return a CGFloat. CGFloat is just, um, you know, an integer is a whole number. CGFloat, there's any kind of number; it's a floating-point number.

Alright, so our CGFloat method is going to say, it's going to be called distance(CGPoint p1, CGPoint p2), and this method is going to use the point distance formula. But before we can do anything with this, we have to understand what we're doing. So anyway, on a UIView, a method that always gets called is called touchesMoved and touchesBegan. All it does, all it's responsible for is handling multiple touch events.

Which means if you put five fingers on the screen, it's going to call one touchesMoved whenever any of them moves. But there's going to be an array of touches, and that array will contain the different touch objects. So we're going to declare our void touchesMoved, Clary, and in here is where we're going to get two points.

So touches is an NSSet. NSSet is like an unordered array; there's no order to it. So if touches.count bang goes to Anna sog, not two fingers, a little church. Otherwise, there are two touches. So we can say for each touch t in touches, and here will they get coordinates. CGPoint p1 = T.location in self.view; else p2 = T.location in here.

So talk to you, and at the end of this method, we'll say step equals one time up. Alright, so now we'll have two points, p1 and p2. These are the two points, two locations of the fingers on the screen. So now we'll get the distance between these two fingers and make it the diameter. So I'll say diameter equals self.distance(p1, p2).

Now we have to make the distance method work. So if you recall from simple algebra, to get the difference between two points, you just use the Pythagorean theorem. So we're going to return the square root of (p1.y - p2.y) squared plus (p1.x - p2.x) squared. And this works mathematically, so yeah, now these will always be positive. The square root method won't flip out; it'll give you an accurate distance.

So we're just setting the diameter, and now say self.setNeedsDisplay. This just means redraw our view. It will recall our drawRect method. In this method, we're going to draw a circle in the middle of the screen, which will have the diameter that we want. So let's say CGPoint, or let's declare CGRect circleFrame.

And let's calculate the frame. So we'll say circleFrame.origin.x = self.frame.size.width / 2 - diameter / 2, and it uses the order of operations, so this will be separate from this. It will subtract. We'll say circleFrame.origin.y = self.frame.size.height / 2 - diameter / 2, and now we'll say circleFrame.size.width = diameter, circleFrame.size.height = diameter.

Alright, so now we have to draw the circle, and you can remember this: we just get the context. Alright, it will succeed; CGContextAddEllipseInRect(context, circleFrame); and we'll set the fill color. CGContextSetFillColorWithColor(context, UIColor.yellow.CGColor); lots of colors.

Alright, so this will draw the circle that we want. Let's see if we have any errors or anything like that. Looks like we have four errors, and they're actually just two errors. So here instead of saying some Tapia, we just exit because we're actually a view ourselves, right? That's fixing.

Alright, so now we've got it all configured with our stuff. But there's one last step you have to take before it'll actually work. Because in order to detect two touches, or more like we do here, you have to enable a property on the view. So we'll open back up our view controller in Interface Builder, click on the view; we're going to go to properties, and we have to check multi-touch.

Alright, right there, it has to be checked, or else it won't work for multi-touch. So let's run it, and if we drag our fingers, we have a circle with the diameter of which, yeah, we want. As I put them together, it'll be 0. But yeah, basically, that's how multi-touch is done: just getting the distance between the two fingers.

So anyway, this is our 12th iPhone programming tutorial, so thanks for watching! Mac has a lil, unsubscribe, and goodbye.

More Articles

View All
Origins of European exploration in the Americas
When we think about European exploration in the Americas, we tend to start at 1492, with Christopher Columbus showing up at the island of Hispaniola. But in this video, I want to take a step back a few decades and talk about the conditions that led to Chr…
Derivatives of inverse functions: from equation | AP Calculus AB | Khan Academy
Let ( F(x) ) be equal to ( 12x^3 + 3x - 4 ). Let ( H ) be the inverse of ( F ). Notice that ( F(-2) ) is equal to (-14) and then they’re asking us what is ( H’(-14) ). If you’re not familiar with how functions and their derivatives relate to their invers…
The Battle of SHARKS!
While riding my bike around London, I stumbled upon this and was like, “Surprise!” Sharks raise questions that need answers. So once back home, to Google I went, with a search query that would turn the next six weeks of my life real weird with phone calls…
Writing exponential functions | High School Math | Khan Academy
G is an exponential function with an initial value of -2. So, an initial value of -2 and a common ratio of 17th. Write the formula for G of T. Well, the fact that it’s an exponential function, we know that its formula is going to be of the form G of T is…
The End Of Credit Cards | A Warning To Credit Users
What’s up, guys? It’s Graham here. So, as many of you know, I take my credit card usage very seriously. And I say the term “usage” because when I tell people I have a hobby collecting credit cards, they look at me as though I’ve gone crazy and have a pro…
Here’s EXACTLY how much I made from YouTube in 2018 (Not Clickbait)
What’s up, you guys? It’s Graham here. So I’ll just get right into it. A month ago, I posted a video showing how much I made from a 1.4 million view viral video, which if you haven’t seen that video yet, make sure to go and check that out so you can go an…