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
Meet the Heroes Who Protect the Last Northern White Rhinos in the World | Short Film Showcase
The most dangerous thing in the bush is humanity. Your life is always at risk; you can die at any time. [Music] My name is Jacob. I work in open data as our knowledge. I know Gattaca, a minute. Oh Peter, Yannick, you owe me money. Another white rain. M…
15 Ways To Achieve Peak Performance
When it comes to achieving peak performance in any field and surpassing your competition, there’s no such thing as luck. Luck is what happens when preparation meets opportunity. So how do you prepare accordingly, and what strategies do successful people e…
From Summit to Subterranean: Chasing Adventure in San Antonio, Texas | National Geographic
When you’re in the cave, you’re so hyper-focused because there’s no distractions, and so for me, it’s almost meditative. [Music] I started in adventure photography with winter sports. Now I’m here in Texas to find that adventure, but underground. Hi, it…
Millennium Falcon or Starship Enterprise? - Fan Question | StarTalk
[Music] Oh, that’s easy. Oh my gosh, no, the Enterprise! There’s no question. No question! The Enterprise has the benefit of being real, in the sense that there are real scientists and real engineers on staff on the ship monitoring its engines, its warp …
How Governments and Banks Keep You Poor
You’ve just graduated college and worked your first month at your new job. You’ve worked extremely hard to get this position, and getting that first paycheck feels like such a triumphant moment. The possibilities of what you can do with your income are ex…
Politics of Climate Change | Years of Living Dangerously
BRADLEY WHITFORD: I want to know why there aren’t more Republicans in Congress willing to come forward on climate. So I’m going to meet the GOP’s most outspoken critic, Democratic Senator Sheldon Whitehouse. SHELDON WHITEHOUSE: I am back to again urge my…