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
Why I Founded an Ocean Exploration Organization
When I was growing up, Jac Kisto had a big effect on my life. Fast forward, I learned how to dive. Um, and then about 11 years ago, I bought an ocean exploration vehicle. It’s a two-man submersible that goes down 1,000 meters, and I knew that I could give…
We deleted social media for 3 days- Mental Glow Up Diaries Episode 3
Social media is the best example of a double-edged sword. If you can use it effectively for your favor, it can be life-changing. You can learn a bunch of new things, you can make friends, you can even make money out of it. But social media facilitates an …
Free Solo 360 | National Geographic
Anybody could conceivably die on any given day, and we’re all gonna die eventually. [Applause] So, Lange just makes it far more immediate. You accept the fact that if anything goes wrong, you’re going to die, and that’s that. [Music] I wasn’t the kind of…
Hunted in the Arctic | Edge of the Unknown on Disney+
I was 8 and my brother was about 10. We really wanted to go camping without any adults. My parents agreed as long as we trained. We were living in the Arctic, so it was cold temperatures and storms and blizzards and bears. But we wanted them to succeed in…
Exclusive: Colombian President Strives to Make His Country Greener | National Geographic
Colombia is one of the richest countries in terms of biodiversity, and we are also one of the most vulnerable countries in terms of climate change. That gives us a special responsibility, and we need to protect, as soon as possible, the largest amount of …
The Possibility of Moving to Mars | StarTalk
The recent discovery of liquid water on Mars! We knew it had water in the distant past. There are dried meandering riverbeds, river deltas, and flood plains, and all the telltale signs of water moving either slowly or quickly in the history of the Martian…