IOS uses the accelerator sensor to make football on the cell phone screen roll back and forth

Source: Internet
Author: User

Friends who have used 360 security guard should know that sometimes 360 security guard comes back with a small ball, letting you shake your cell phone to calculate the heat you consumed. This is very interesting, in fact, this function is very simple to implement. Create a single view project, and then find a football image in png format, and drag the image to the project ., The acceleration sensor is used.

Create an image view with a football image and add it to the root view. Then, set the central coordinates of the Image view and the screen boundary. When the central coordinates of the Image view cross the border, perform corresponding operations, such as bounce.

The specific code is as follows:

HHLAppDelegate. h

#import 
 
  @class HHLViewController;@interface HHLAppDelegate : UIResponder 
  
   @property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HHLViewController *viewController;@end
  
 


HHLAppDelegate. m

#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

# Import
 
  
@ Interface HHLViewController: UIViewController
  
   
{@ Private UIImageView * imageView; // sphere image UIAccelerationValue speedX; // speed of movement in the X axis direction UIAccelerationValue speedY; // speed of movement in the Y axis} @ 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]; // append the UIImageView UIImage * pImage = [UIImage imageNamed: @ "football.png"]; imageView = [[UIImageView alloc] initWithImage: pImage]; imageView. center = self. view. Center; imageView. autoresizingMask = bytes | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin ;//? [Self. view addSubview: imageView];}-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; // get the value passed by the acceleration sensor. UIAccelerometer * accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer. updateInterval = 1.0/60.0; // less than 60 HZ accelerometer. delegate = self;}-(void) viewWillDisappear :( BOOL) animated {[super viewWillDisappear: animated]; speedX = speedY = 0.0; // end the call to obtain the value UIAcce from the accelerometer Lerometer * accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer. delegate = nil;} // process the notification received from the accelerometer-(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 ;// It rebounded at a 0.4-fold acceleration after hitting the left border.} Else if (posX> self. view. bounds. size. width) {posX = self. view. bounds. size. width; speedX * =-0.4; // rebound at a rate of 0.4 times after hitting the border on the right.} If (posY <0.0) {posY = 0.0; speedY = 0.0; // do not rebound when the top frame is reached} else if (posY> self. view. bounds. size. height) {posY = self. view. bounds. size. height; speedY * =-1.5; // rebound at a rate of 1.5 times after hitting the bottom border .} ImageView. center = CGPointMake (posX, posY);}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated.} @ end

Another thing you need to note is that this program can only be tested on a real machine, so you can only see pictures of football on the simulator and then on the screen, if you have a real machine, test it on the real machine.

The effect is as follows,


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.