IOS_加速計_衝突検出_小球位置修復_dispatch_once単例


H:/0802/01_accelerometer加速計+小球衝突検出_ViewController.h
//
//  ViewController.h
//     01-  1
//
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAccelerometerDelegate>

//       
@property (weak, nonatomic) IBOutlet UIView *ballView;

@end

H:/0802/01_accelerometer加速計+小球衝突検出_ViewController.m
//  ViewController.m
//     01-  1
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
//     :        Y    UIKit     Y       。!!!!!!

#import "ViewController.h"
@interface ViewController ()
{
    //     
    CGPoint     _ballVelocity;
}
@end
@implementation ViewController
- (void)viewDidLoad

 {   [super viewDidLoad];
    // 1.         0,           
    _ballVelocity = CGPointZero;
    // 1.              (  UIAccelerometer)
	//   :    ,     
    UIAccelerometer *acce = [UIAccelerometer sharedAccelerometer];
    // 2.         ,   ,  :  HZ
    [acce setUpdateInterval:1.0 / 30.0];
    // 3.              <UIAccelerometerDelegate>
    [acce setDelegate:self];
}
#pragma mark -      
//        
- (void)updateBallLocation
{
    //       
    // 1.     ,           
    //            
    CGPoint center = [_ballView center];
    //            ,            
    CGFloat r = _ballView.frame.size.width / 2;
    //         
    // 1.1            	
    if (center.x < r) {
		//             ,  x    ,       
        //       ,  *0.8
        _ballVelocity.x = 0.8 * abs(_ballVelocity.x);
    } else if (center.x > self.view.bounds.size.width - r) {
		//             ,  x    ,       
        //         *0.8
        _ballVelocity.x = -0.8 * abs(_ballVelocity.x);
    }
    // 1.2            
    if (center.y < r) {
		//             ,  y    ,       
        //       ,  *0.8
        _ballVelocity.y = 0.8 * abs(_ballVelocity.y);
    } else if (center.y > self.view.bounds.size.height - r) {
		//             ,  y    ,       
        //       ,  *0.8
        _ballVelocity.y = -0.8 * abs(_ballVelocity.y);
    }
    // 2.         ,         
    //         +               
//    CGPoint center = [_ballView center];
    [_ballView setCenter:CGPointMake(center.x + _ballVelocity.x,
					center.y + _ballVelocity.y)];
}
//        ,      ,    
- (void)accelerometer:(UIAccelerometer *)accelerometer
				didAccelerate:(UIAcceleration *)acceleration
{
	/*acceleration      :x,y,z timestamp    ,
	  x,y,z                ,  g,       */
    //   NSLog           ,             
    NSLog(@"%@", acceleration);
    // 1.             ,         ,  ,           
    //            ,           
    _ballVelocity.x += acceleration.x;
    //     :        Y    UIKit     Y       。!!!!!!
    _ballVelocity.y -= acceleration.y;
	//              
    [self updateBallLocation];
}
@end

H:/0802/02_MotionManager+ボール位置修復+衝突検出_ViewController.h
//
//  ViewController.h
//     02.CoreMotion Push  
//
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//       
@property (weak, nonatomic) IBOutlet UIView *ballView;

@end

H:/0802/02_MotionManager+ボール位置修復+衝突検出_ViewController.m
//  ViewController.m
//     02.CoreMotion Push  
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
//       ,         
//           ,            
//        ,           ,   *0.8   ,
//                  ,  ,      ,       ,
//            ,    
//   ,              !
#import "ViewController.h"
//      3   
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
{
    //      ,       ,       
    CMMotionManager *motionManger;
    //     
    CGPoint _ballVelocity;
}
@end
@implementation ViewController
//        
- (void)updateBallLocation
{
    //       
    // 1.     ,           
    //            
    CGPoint center = [_ballView center];
    //            ,            
    CGFloat r = _ballView.frame.size.width / 2;
	NSLog(@"      ---%@", NSStringFromCGPoint(center));
    NSLog(@"  ---%@", NSStringFromCGPoint(_ballVelocity));
    //       ,         
	//           ,            
	//        ,           ,   *0.8   ,
	//                  ,  ,      ,       ,
	//            ,    
	//   ,              !
    if (center.x < r) {
		//             ,  x    ,       
        //       ,  *0.8
        _ballVelocity.x = 0.8 * abs(_ballVelocity.x);
        //       
        center.x = r;
    } else if (center.x > self.view.bounds.size.width - r) {
		//             ,  x    ,       
        //       ,  *0.8
        _ballVelocity.x = -0.8 * abs(_ballVelocity.x);
        //       
        center.x = self.view.bounds.size.width - r;
    }
    if (center.y < r) {
		//             ,  y    ,       
        //       ,  *0.8
        _ballVelocity.y = 0.8 * abs(_ballVelocity.y);
        center.y = r;
    } else if (center.y > self.view.bounds.size.height - r) {
		//             ,  y    ,       
        //       ,  *0.8
        _ballVelocity.y = -0.8 * abs(_ballVelocity.y);
        center.y = self.view.bounds.size.height - r;
    }
    // 2.         ,         
    //            +               
    //    CGPoint center = [_ballView center];
    [_ballView setCenter:CGPointMake(center.x + _ballVelocity.x,
							center.y + _ballVelocity.y)];
    CGFloat x = center.x + _ballVelocity.x;
    CGFloat y = center.y + _ballVelocity.y;
    NSLog(@"         ---%@", NSStringFromCGPoint(CGPointMake(x, y)));
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //    1. alloc init   Motion   
    motionManger = [[CMMotionManager alloc]init];
    //    2.             
    if ([motionManger isAccelerometerAvailable]) {
        //    3.            
        //                 100 Hz,        60HZ
        [motionManger setAccelerometerUpdateInterval:1 / 30];
        //    4. startAccelerometerUpdatesToQueue
		//	                ,Push  ,  block    Handle
        [motionManger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
				withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            //   :      Log,       ,      Data,     
            //       
            //       ,         ,        
            _ballVelocity.x += accelerometerData.acceleration.x / 10;
			//     Y  UI    Y   
            _ballVelocity.y -= accelerometerData.acceleration.y / 10;            
            CGPoint a = CGPointMake(accelerometerData.acceleration.x / 10,
								-accelerometerData.acceleration.y / 10);
            NSLog(@"   ---%@", NSStringFromCGPoint(a));
            //[_ballView setCenter:CGPointMake(_ballView.center.x + _ballVelocity.x,
								//_ballView.center.y + _ballVelocity.y)];
			//        ,      ,(      )
            [self updateBallLocation];
        }];
    } else {
        NSLog(@"     ");
    }
}
@end

H:/0802/03_dispatch_once生成CMMotionManager単例+小球位置修正+衝突検出_AppDelegate.h
//  AppDelegate.h
//     02.CoreMotion Push  
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.

#import <UIKit/UIKit.h>
//     3   
#import <CoreMotion/CoreMotion.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

//   ,        ,   AppDelegate  ,    controller   
@property (strong, nonatomic, readonly) CMMotionManager *sharedMotionManager;

@end

H:/0802/03_dispatch_once生成CMMotionManager単例+小球位置修正+衝突検出_AppDelegate.m
//  AppDelegate.m
//     02.CoreMotion Push  
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
//  dispatch_once  CMMotionManager      
#import "AppDelegate.h"
@interface AppDelegate()
{
    CMMotionManager *_montionManager;
}
@end
@implementation AppDelegate

#pragma mark -     sharedMotionManager Getter  ,            
//   ,        ,   AppDelegate  ,    controller   
- (CMMotionManager *)sharedMotionManager
{
    //   !!!!!!!!   dispatch_one    
	//     ,    ,    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _montionManager = [[CMMotionManager alloc]init];
    });
    return _montionManager;
}





- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}
							
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end

H:/0802/03_dispatch_once生成CMMotionManager単例+小球位置修正+衝突検出_ViewController.h
//
//  ViewController.h
//     02.CoreMotion Push  
//
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//       
@property (weak, nonatomic) IBOutlet UIView *ballView;

@end

H:/0802/03_dispatch_once生成CMMotionManager単例+小球位置修正+衝突検出_ViewController.m
//  ViewController.m
//     02.CoreMotion Push  
//  Created by apple on 13-8-2.
//  Copyright (c) 2013  itcast. All rights reserved.
/*
	   AppDelegate.h       3   <CoreMotion/CoreMotion.h>
	   AppDelegate.m   dispatch_once        _montionManager
*/
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
{
    //      
    CMMotionManager *motionManger;
    //     
    CGPoint _ballVelocity;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    //    1.   AppDelegate           
    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    motionManger = [appDelegate sharedMotionManager];
    [self startAccelerometer];
}

//    ,       , ViewDidLoad     
- (void)startAccelerometer
{
    //    2.          
    if ([motionManger isAccelerometerAvailable]) {
        //    3.            
        //                 100 Hz
        [motionManger setAccelerometerUpdateInterval:1 / 30];
        //    4.        ,Push      
        [motionManger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
					withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            //   :      Log,       ,      Data,     
            //       
            //       ,         ,        
            _ballVelocity.x += accelerometerData.acceleration.x / 10;
            _ballVelocity.y -= accelerometerData.acceleration.y / 10;
            //        
            CGPoint a = CGPointMake(accelerometerData.acceleration.x / 10,
									-accelerometerData.acceleration.y / 10);
            NSLog(@"   ---%@", NSStringFromCGPoint(a));
            
            //       
            //          
            //            [_ballView setCenter:CGPointMake(_ballView.center.x + _ballVelocity.x,
								//_ballView.center.y + _ballVelocity.y)];
			//        ,      
            [self updateBallLocation];
        }];
    } else {
        NSLog(@"     ");
    }
}


//      ,       
- (void)updateBallLocation
{
    //       
    // 1.     ,           
    //            
    CGPoint center = [_ballView center];
    //            ,            
    CGFloat r = _ballView.frame.size.width / 2;
	NSLog(@"      ---%@", NSStringFromCGPoint(center));
    NSLog(@"  ---%@", NSStringFromCGPoint(_ballVelocity));
	
    //       ,         
	//           ,            
	//        ,           ,   *0.8   ,
	//                  ,  ,      ,       ,
	//            ,    
	//   ,              !
    if (center.x < r) {
        //   x    
        _ballVelocity.x = 0.8 * abs(_ballVelocity.x);
        //       
        center.x = r;
    } else if (center.x > self.view.bounds.size.width - r) {
        _ballVelocity.x = -0.8 * abs(_ballVelocity.x);
        //       
        center.x = self.view.bounds.size.width - r;
    }
    // 1.2            
    if (center.y < r) {
        _ballVelocity.y = 0.8 * abs(_ballVelocity.y);
		//       
        center.y = r;
    } else if (center.y > self.view.bounds.size.height - r) {
        _ballVelocity.y = -0.8 * abs(_ballVelocity.y);
		//       
        center.y = self.view.bounds.size.height - r;
    }
//    
//    NSLog(@"      ---%@", NSStringFromCGPoint(center));
//    NSLog(@"  ---%@", NSStringFromCGPoint(_ballVelocity));
    // 2.         ,         
    //         +               
    [_ballView setCenter:CGPointMake(center.x + _ballVelocity.x, center.y + _ballVelocity.y)];
    CGFloat x = center.x + _ballVelocity.x;
    CGFloat y = center.y + _ballVelocity.y;
    NSLog(@"         ---%@", NSStringFromCGPoint(CGPointMake(x, y)));
}


#pragma mark -     ,     
// 1.      Began  ,              
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //             
    //       ,          
    if ([motionManger isAccelerometerActive]) {
        [motionManger stopAccelerometerUpdates];
    } else {
		//              
        [self startAccelerometer];
    }
}
@end