IOS通过加速感应器实现手机实现手机屏幕上的足球可以来回的滚动反弹

jopen 11年前

用过360安全卫士的朋友都应该知道,有的时候360安全卫士回出来一个小球,让你摇动手机,来计算你消耗的热量,很有趣,其实这个功能实现起来非 常的简单。新建一个单视图工程,然后需要找到一张的足球图片,截图使它的格式为png格式,并把这张图片拖到工程内。,然后就是通过加速度感应器来实现。

通过足球图片建立图片视图,并把它添加到根视图上。然后设置图片视图的中心坐标,设置屏幕的界限,当图片视图的中心坐标越界的时候执行相应地操作,比如反弹等。

具体的代码如下:

HHLAppDelegate.h

</div> </div>
    #import <UIKit/UIKit.h>                @class HHLViewController;                @interface HHLAppDelegate : UIResponder <UIApplicationDelegate>                @property (strong, nonatomic) UIWindow *window;                @property (strong, nonatomic) HHLViewController *viewController;                @end  


HHLAppDelegate.m

</div> </div>
    #import "HHLAppDelegate.h"                #import "HHLViewController.h"                @implementation HHLAppDelegate                - (void)dealloc        {            [_window release];            [_viewController release];            [super dealloc];        }                - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions        {            self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];            // Override point for customization after application launch.            self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];            self.window.rootViewController = self.viewController;            [self.window makeKeyAndVisible];            return YES;        }                - (void)applicationWillResignActive:(UIApplication *)application        {            // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.            // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.        }                - (void)applicationDidEnterBackground:(UIApplication *)application        {            // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.             // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.        }                - (void)applicationWillEnterForeground:(UIApplication *)application        {            // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.        }                - (void)applicationDidBecomeActive:(UIApplication *)application        {            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.        }                - (void)applicationWillTerminate:(UIApplication *)application        {            // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.        }                @end  


HHLViewController.h

</div> </div>
    #import <UIKit/UIKit.h>                @interface HHLViewController : UIViewController<UIAccelerometerDelegate>                {        @private            UIImageView *imageView;//球体图像            UIAccelerationValue speedX;//X轴方向的运动速度            UIAccelerationValue speedY;//Y轴方向的运动速度        }        @end  

HHLViewController.m

 

    #import "HHLViewController.h"                @interface HHLViewController ()                @end                @implementation HHLViewController                        -(void)dealloc        {            [imageView release];            [super dealloc];                    }                        - (void)viewDidLoad        {            [super viewDidLoad];            self.view.backgroundColor = [UIColor whiteColor];                        //追加球体的UIImageView            UIImage *pImage = [UIImage imageNamed:@"football.png"];            imageView = [[UIImageView alloc]initWithImage:pImage];                                    imageView.center = self.view.center;                        imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;//?                        [self.view addSubview:imageView];                    }                - (void)viewWillAppear:(BOOL)animated        {            [super viewWillAppear:animated];                        //开始获取加速传感器传过来的值            UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];            accelerometer.updateInterval = 1.0/60.0; //小于60HZ            accelerometer.delegate = self;                    }                - (void)viewWillDisappear:(BOOL)animated        {            [super viewWillDisappear:animated];            speedX = speedY = 0.0;            //结束从加速度传感器获取值            UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];            accelerometer.delegate = nil;                   }                //处理从加速度传感器接收来的通知        -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration        {            speedX += acceleration.x;            speedY += acceleration.y;                        CGFloat posX = imageView.center.x - speedX;            CGFloat posY = imageView.center.y - speedY;                    if (posX <0.0) {                posX = 0.0;                speedX *= -0.4;//碰到左边的边框后以 0.4倍的加速度反弹。                            }            else if (posX > self.view.bounds.size.width)           {               posX = self.view.bounds.size.width;               speedX *= -0.4; //碰到右边的边框后以0.4倍的速度反弹。                          }        if (posY <0.0){            posY = 0.0;            speedY = 0.0;//碰到上边的边框不反弹            }                else if(posY >self.view.bounds.size.height){            posY = self.view.bounds.size.height;            speedY *= -1.5;//碰到下边的边框后以1.5倍的速度反弹。        }        imageView.center = CGPointMake(posX,posY);        }        - (void)didReceiveMemoryWarning        {            [super didReceiveMemoryWarning];            // Dispose of any resources that can be recreated.        }                @end  

 还有一点需要给大家说明的是,这个程序只能在真机上才能测试出想过,所以再模拟机上只能看到足球的图片显示再屏幕上,大家如果有真机的话建议,在真机上 测试一下。

效果如下,

20131227185217625.png

来自:http://blog.csdn.net/hanhailong18/article/details/17616813