IOS通過加速感應器實現手機實現手機屏幕上的足球可以來回的滾動反彈

jopen 10年前發布 | 17K 次閱讀 iOS開發 移動開發 IOS

用過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  </pre><br />


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  </pre><br />


HHLViewController.h

</div> </div>

    #import <UIKit/UIKit.h>

@interface HHLViewController : UIViewController<UIAccelerometerDelegate>  

{  
@private  
    UIImageView *imageView;//球體圖像  
    UIAccelerationValue speedX;//X軸方向的運動速度  
    UIAccelerationValue speedY;//Y軸方向的運動速度  
}  
@end  </pre><br />

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  </pre><br />

 還有一點需要給大家說明的是,這個程序只能在真機上才能測試出想過,所以再模擬機上只能看到足球的圖片顯示再屏幕上,大家如果有真機的話建議,在真機上 測試一下。

效果如下,

20131227185217625.png

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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!