I have to admit, I’m writing these things for purely selfish reasons. Same reason I write books. You learn WAY more by teaching. If you think you are getting some knowledge from reading these, realize that I am getting SO much more knowledge and understanding from writing them. 🙂
Now let’s use one of the really neat features of the iPhone, that lovely touch screen. There are three methods we can add to our view controller to listen for touch events: touchesBegan, touchesMoved, and touchesEnded. It inherits these from the UIResponder class, so you don’t need to declare them, just implement them. Note that UIView is a UIResponder too, so you could put touches methods in there, but it makes more sense in this app to have it in the view controller, where we have a constant reference to the ball. To start with, add these method to GravityTutorialViewController.m:
[c]- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@“touches began”);
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@“touches moved”);
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@“touches ended”);
}[/c]
Open up your console and run the app and see that it does indeed respond to these actions.
You see that each method gets passed an NSSet of touches as a first parameter. An NSSet is “an unordered collection of distinct elements”. Realize that the touch screen is multitouch, so a touch event may have several simultaneous touches going on – one for each finger. Here of course, we’re really only concerned with a single touch. We get that by calling the anyObject method of the set. This will give us a single UITouch object. We can then call the locationInView method of the UITouch object to get a CGPoint showing the x, y location of that touch.
We’ll store this point in a class variable, but let’s actually make two, one for the last touch, and one for the current touch, so we can track movement and velocity. Declare these two in the view controller’s .h file:
[c]#import <uikit/UIKit.h>
#import “Ball.h”
@interface GravityTutorialViewController : UIViewController {
Ball *ball;
CGPoint lastTouch;
CGPoint currentTouch;
}
– (void)onTimer;
@end[/c]
We don’t need to make properties or synthesize these, as they are only used in the class. Now, we can grab the current touch location in each of the touch methods. Actually, we don’t really need it in touchesEnded, as you’ll see in a moment.
[c]- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];
}[/c]
First off, in touchesBegan, we need to see whether or not the user has touched the ball. Unlike in ActionScript where you have handy mouse down and mouse up and click events that tell you whether or not you clicked on something, here you have to do it all by hand. We’ll just check the currentTouch location’s distance from the ball’s location. If that is less than the ball’s radius, the user touched the ball.
Oddly enough, there doesn’t even seem to be a built in distance method here, so we have to roll our own for that too. No problem though, we know Pythagorus quite well, right? A squared plus B squared and all that.
[c]- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];
CGFloat dx = currentTouch.x – ball.position.x;
CGFloat dy = currentTouch.y – ball.position.y;
CGFloat dist = sqrt(dx * dx + dy * dy);
if(dist < ball.radius) { ball.velocity = CGPointMake(0.0, 0.0); ball.dragging = YES; } lastTouch = currentTouch; }[/c] So, if the user has touched the ball, what do we do? First of all, we kill the velocity by setting it to 0.0. That way if he touches and releases it without moving, it will just drop to the floor, rather than continue on the way it was going. Then we set the ball’s dragging property to YES (true). I know, that property doesn’t exist yet, but we’ll be adding it soon. But first, let’s wrap up the touch stuff. Finally, in this method, we assign the currentTouch to lastTouch so we know how far it moved, if and when it moves. In touchesMoved, we need to move the ball to the current touch. Simple enough. We also need to update the velocity by subtracting the lastTouch from the currentTouch. In other words, how far did the touch move since last time? Well that’s the ball’s current velocity. If the user releases it, that’s how fast it should go, and in what direction. [c]- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; currentTouch = [touch locationInView:self.view]; ball.position = currentTouch; ball.velocity = CGPointMake(currentTouch.x - lastTouch.x, currentTouch.y - lastTouch.y); lastTouch = currentTouch; }[/c] Finally, in touchesEnded, we just tell the ball we are not dragging it anymore. [c]- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { ball.dragging = NO; }[/c] Now, onto this dragging stuff. We’ll declare a BOOL variable in Ball.h and make it a property. [c]@interface Ball : NSObject { CGPoint position; CGPoint velocity; CGFloat radius; CGColorRef color; CGFloat bounce; CGFloat gravity; BOOL dragging; } @property CGPoint position; @property CGPoint velocity; @property CGFloat radius; @property CGColorRef color; @property CGFloat bounce; @property CGFloat gravity; @property BOOL dragging; - (void)update; - (CGRect)getRect; @end[/c] And synthesize it in Ball.m: [c]#import “Ball.h” @implementation Ball @synthesize position; @synthesize velocity; @synthesize radius; @synthesize color; @synthesize bounce; @synthesize gravity; @synthesize dragging; …[/c] Finally, in the update method, we check to see if we are dragging the ball or not. If so, we just return. We don’t want to do all that gravity and bouncing nonsense. We just want to let the user drag the ball. [c]- (void)update { if(dragging) return; velocity.y += gravity; position.x += velocity.x; position.y += velocity.y; if(position.x + radius > 320.0) {
…[/c]
That if statement up at the top is the only change to this method.
Now, we are about half way there. Oh, no, wait, we are done! Yup. Run the application (I still want to say “Test Movie” but I’m working on it) and you should have a dragable, throwable, bouncable, thoroughly lovable ball on your iPhone, iPod, or at very least, simulator.
Next up… accelerometer!