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
15 Ways To Start A New, Better Life
While you’re busy thinking of a better life, your current and only life you have slowly passes you by. And while lifetimes are measured in decades, progress is measured in days. Welcome to Alux! Who knew that your physical health has positive effects thro…
Band of Sisters | Explorer
The Peshmerga number roughly 150,000, and they’re revered in Kurdish society. When ISIS first attacked, they were taken by surprise and driven back in some places. Since those early days, they’ve transformed themselves into a powerful fighting force—one o…
Using recursive formulas of geometric sequences | Mathematics I | High School Math | Khan Academy
The geometric sequence ( a_i ) is defined by the formula where the first term ( a_1 ) is equal to -1⁄8 and then every term after that is defined as being so ( a_i ) is going to be two times the term before that. So, ( ai ) is ( 2 \times a{i-1} ). What is…
Work at a Startup Expo 2019
So thank you so much. Quick round of applause for making it out here for all these companies that we’re going to be having a walk across here. It’s two o’clock, we want to keep it on time because we have a lot of great stuff to get through. So this is wh…
The 2020 Mortgage Crisis Explained
What’s up guys? It’s Graham here. So lately I’ve been hearing a lot of talk about this upcoming mortgage crisis. Not to mention, pretty much every single news website in existence is mentioning it. So let’s talk about it. And instead of creating some sor…
Should You Move Your Company to Silicon Valley? - Eric Migicovsky, Pebble Founder
Today I want to talk about the question: Should you move your company to Silicon Valley? This is a question that’s pretty close to my heart because I started my company, Pebble, in Waterloo, Ontario, and I decided to move my company to Silicon Valley afte…