iPhone开发之重力感应实例代码类

2009年12月30日 | laifangwen

 

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

GravityObj.m

  1. #import "GravityObj.h"
  2.  
  3.  
  4. @implementation GravityObj
  5.  
  6. @synthesize position, size;
  7. @synthesize objTimer;
  8. @synthesize velocity;
  9. @synthesize pngName;
  10. @synthesize bounce;
  11. @synthesize gravity, acceleratedGravity;
  12. @synthesize lastTouch, currentTouch;
  13. @synthesize dragging;
  14.  
  15. - (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s {
  16.         if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) {
  17.                 [self setPngName:imageName];
  18.                 [self setPosition:p];
  19.                 [self setSize:s];
  20.                 [self setBackgroundColor:[UIColor clearColor]];
  21.                
  22.                 // Set default gravity and bounce
  23.                 [self setBounce:-0.9f];
  24.         [self setGravity:0.5f];
  25.                 [self setAcceleratedGravity:CGPointMake(0.0, gravity)];
  26.                 [self setDragging:NO];
  27.                
  28.                 UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
  29.                 prezzie.image = [UIImage imageNamed:imageName];
  30.                
  31.                 [self addSubview:prezzie];
  32.                 [prezzie release];
  33.                
  34.                 [[UIAccelerometer sharedAccelerometer] setDelegate:self];
  35.         }
  36.         return self;
  37. }
  38.  
  39. - (void)update {
  40.         [self setNeedsDisplay];
  41.        
  42.         if(dragging) return;
  43.        
  44.         velocity.x += acceleratedGravity.x;
  45.     velocity.y += acceleratedGravity.y;
  46.     position.x += velocity.x;
  47.     position.y += velocity.y;
  48.        
  49.     if(position.x + size.width >= 320.0) {
  50.         position.x = 320.0 – size.width;
  51.         velocity.x *= bounce;
  52.     }
  53.     else if(position.x <= 0.0) {
  54.         velocity.x *= bounce;
  55.     }
  56.        
  57.     if(position.y + size.height >= 416.0) {
  58.         position.y = 416.0 – size.height;
  59.         velocity.y *= bounce;
  60.     }
  61.     else if(position.y <= 0.0) {
  62.         velocity.y *= bounce;
  63.     }
  64.         self.frame = CGRectMake(position.x, position.y, size.width, size.height);
  65. }
  66.  
  67. - (void)onTimer {
  68.         [self update];
  69. }
  70.  
  71. - (void)drawRect:(CGRect)rect {
  72.     // Drawing code
  73. }
  74. /* EVENTS */
  75.  
  76. - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
  77.     acceleratedGravity.x = acceleration.x * gravity;
  78.     acceleratedGravity.y = -acceleration.y * gravity;
  79. }
  80.  
  81. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  82.         // First, lets check to make sure the timer has been initiated
  83.         if (objTimer == nil) {
  84.                 objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
  85.         }
  86.        
  87.     UITouch *touch = [touches anyObject];
  88.         [self setCurrentTouch:[touch locationInView:self]];
  89.     CGFloat dx = currentTouch.x – position.x;
  90.     CGFloat dy = currentTouch.y – position.y;
  91.     CGFloat dist = sqrt(dx * dx + dy * dy);
  92.     if(dist < size.width) {
  93.         [self setVelocity:CGPointMake(0.0, 0.0)];
  94.                 [self setDragging:YES];
  95.     }
  96.         [self setLastTouch:currentTouch];
  97. }
  98.  
  99. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  100.     UITouch *touch = [touches anyObject];
  101.     [self setCurrentTouch:[touch locationInView:self]];
  102.         [self setDragging:YES];
  103.         [self setVelocity:CGPointMake(currentTouch.x – lastTouch.x, currentTouch.y – lastTouch.y)];
  104.         [self setLastTouch:currentTouch];
  105. }
  106.  
  107. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  108.         [self setDragging:NO];
  109. }
  110.  
  111. - (void)dealloc {
  112.         [objTimer release], objTimer = nil;
  113.         [pngName release], pngName = nil;
  114.         [super dealloc];
  115. }
  116.  
  117. @end

Then in the view you need to use a gravity object in, simply do:

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)

引用通告地址: http://blog.laifangwen.com/cmd.asp?act=gettburl&id=261

评论: 0 | 引用: 0 | 浏览:
名称(*):
邮箱:
网站链接:
正文(*):
选 项:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。