iPhone开发之重力感应实例代码类
iPhone gravity tutorial – code
I recently started creating my first iPhone app for a new Raw Jam client. Getting to grips with objective c was the first challenge, which fortunately I feel I have now achieved.
Yesterday I started looking around Google to see if I could find a suitable tutorial that would help me understand how to impliment ‘gravity’ like functionality. In essence, I needed to be able to flick an object on the screen, have it bounce around a little and then fall back to earth. I found a great tutorial that (through 5 seperate blog posts) leads you through each of the required stages. I HIGHLY recommend that you check it out (
The code in all its glory is below. What we have is essentially a totally generic gravityObject class that can be dropped into any view, and any number of times. What’s more, you actually initiate the object with a starting position and a PNG image (which for me, made more sense than drawing a circle on the screen – but you can change this part to suit your needs. So, without further ado:
GravityObj.h
- #import
-
- @interface GravityObj : UIView {
- CGPoint position;
- CGSize size;
- CGPoint velocity;
- NSTimer *objTimer;
- NSString *pngName;
- CGFloat bounce;
- CGFloat gravity;
- CGPoint acceleratedGravity;
- CGPoint lastTouch;
- CGPoint currentTouch;
- BOOL dragging;
- }
-
- @property CGPoint position;
- @property CGSize size;
- @property CGPoint velocity;
- @property(nonatomic,retain)NSString *pngName;
- @property(nonatomic,retain)NSTimer *objTimer;
- @property CGFloat bounce;
- @property CGFloat gravity;
- @property CGPoint acceleratedGravity;
- @property CGPoint lastTouch;
- @property CGPoint currentTouch;
- @property BOOL dragging;
-
- - (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s;
- - (void)update;
- - (void)onTimer;
-
- @end
GravityObj.m
- #import "GravityObj.h"
-
-
- @implementation GravityObj
-
- @synthesize position, size;
- @synthesize objTimer;
- @synthesize velocity;
- @synthesize pngName;
- @synthesize bounce;
- @synthesize gravity, acceleratedGravity;
- @synthesize lastTouch, currentTouch;
- @synthesize dragging;
-
- - (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s {
- if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) {
- [self setPngName:imageName];
- [self setPosition:p];
- [self setSize:s];
- [self setBackgroundColor:[UIColor clearColor]];
-
- // Set default gravity and bounce
- [self setBounce:-0.9f];
- [self setGravity:0.5f];
- [self setAcceleratedGravity:CGPointMake(0.0, gravity)];
- [self setDragging:NO];
-
- UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
- prezzie.image = [UIImage imageNamed:imageName];
-
- [self addSubview:prezzie];
- [prezzie release];
-
- [[UIAccelerometer sharedAccelerometer] setDelegate:self];
- }
- return self;
- }
-
- - (void)update {
- [self setNeedsDisplay];
-
- if(dragging) return;
-
- velocity.x += acceleratedGravity.x;
- velocity.y += acceleratedGravity.y;
- position.x += velocity.x;
- position.y += velocity.y;
-
- if(position.x + size.width >= 320.0) {
- position.x = 320.0 – size.width;
- velocity.x *= bounce;
- }
- else if(position.x <= 0.0) {
- velocity.x *= bounce;
- }
-
- if(position.y + size.height >= 416.0) {
- position.y = 416.0 – size.height;
- velocity.y *= bounce;
- }
- else if(position.y <= 0.0) {
- velocity.y *= bounce;
- }
- self.frame = CGRectMake(position.x, position.y, size.width, size.height);
- }
-
- - (void)onTimer {
- [self update];
- }
-
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- /* EVENTS */
-
- - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
- acceleratedGravity.x = acceleration.x * gravity;
- acceleratedGravity.y = -acceleration.y * gravity;
- }
-
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- // First, lets check to make sure the timer has been initiated
- if (objTimer == nil) {
- objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
- }
-
- UITouch *touch = [touches anyObject];
- [self setCurrentTouch:[touch locationInView:self]];
- CGFloat dx = currentTouch.x – position.x;
- CGFloat dy = currentTouch.y – position.y;
- CGFloat dist = sqrt(dx * dx + dy * dy);
- if(dist < size.width) {
- [self setVelocity:CGPointMake(0.0, 0.0)];
- [self setDragging:YES];
- }
- [self setLastTouch:currentTouch];
- }
-
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- UITouch *touch = [touches anyObject];
- [self setCurrentTouch:[touch locationInView:self]];
- [self setDragging:YES];
- [self setVelocity:CGPointMake(currentTouch.x – lastTouch.x, currentTouch.y – lastTouch.y)];
- [self setLastTouch:currentTouch];
- }
-
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- [self setDragging:NO];
- }
-
- - (void)dealloc {
- [objTimer release], objTimer = nil;
- [pngName release], pngName = nil;
- [super dealloc];
- }
-
- @end
Then in the view you need to use a gravity object in, simply do:
-
- gravityObj1 = [[GravityObj alloc] initWithPNG:@"ball.png" position:CGPointMake(12, 370) size:CGSizeMake(47, 40)];
- gravityObj1.velocity = CGPointMake(4.0, 3.0);
- [self.view addSubview: gravityObj1];
-
You just need to declare gravityObj1 in your header file and then synthesize it… and thats it!!
原文地址:http://blog.rawjam.co.uk/?p=7
Tags: 手机 开发 程序 代码 重力 感应 实例
相关文章:分享一个应用iPhone重力感应器程序源代码 (2009-12-30 13:22:43)
【分享】V2EX安装方法说明:project-babel-v0.6安装步骤 (2009-12-30 10:2:34)
【翻译】在iPhone上使用UITableView开发九宫格视图 (2009-12-28 13:9:16)
【分享】iPhone开发之数据保存 (2009-12-26 10:28:23)
【分享】 iPhone开发经典语录集锦 (2009-12-26 10:22:23)
黑莓手机开发入门:如何为BlackBerry黑莓手机开发应用软件 (2009-11-20 11:51:31)
UITable View实例教程:分组显示列表 (2009-6-18 11:31:30)
UITableView实例教程:创建Table View的detail view (2009-6-16 17:32:57)
UITableView使用实例:创建一个简单的Table View (2009-6-16 11:40:59)
acer aspire 4520g初步成功安装mac os pc版 (2009-3-10 13:56:51)
订阅
上一篇
下一篇