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
Cosine equation solution set in an interval
In a previous video, we established the entire solution set for the following equation. We saw that all the x’s that can satisfy this equation are a combination of these x’s and these x’s. Here, the reason why I’m referring to each of them is numerous x’s…
The Most Misunderstood Concept in Physics
This is a video about one of the most important, yet least understood concepts in all of physics. It governs everything from molecular collisions to humongous storms. From the beginning of the universe through its entire evolution to its inevitable end. I…
2015 AP Calculus BC 2a | AP Calculus BC solved exams | AP Calculus BC | Khan Academy
At time ( T ) is greater than or equal to zero, a particle moving along a curve in the XY plane has position ( X(T) ) and ( Y(T) ). So, its x-coordinate is given by the parametric function ( X(T) ) and y-coordinate by the parametric function ( Y(T) ). Wi…
How the Rich get Richer
So, we’ve all heard the saying: the rich get richer. Looking at the data, it’s easy to see why. The top 1% of U.S. wealth has increased its net worth by 650 percent since 1989, while the bottom 50% only saw its wealth grow a measly 170 percent. The middle…
Overstimulation is Ruining Your Life
The year is 1665, and Isaac Newton is looking out his window at an apple tree standing tall in his orchard in Lincolnshire, England. All of a sudden, a ripe and lonely apple falls from the tree and makes its way to the ground. While most people would cons…
How to light a match inside a balloon - Smarter Every Day 36 LASER MONTH
So what we’re gonna do for this experiment is we are going to take a balloon, a real big clear balloon, and we’ve got a match, and we roll the string up on the match. We’re gonna start blowing the balloon up just a little bit, and we’re gonna make it wher…