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

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

具体的代码如下:

HHLAppDelegate.h

#import <UIKit/UIKit.h>@class HHLViewController;@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>@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 <UIKit/UIKit.h>@interface HHLViewController : UIViewController<UIAccelerometerDelegate>{
@privateUIImageView *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];//追加球体的UIImageViewUIImage *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; //小于60HZaccelerometer.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

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

效果如下,

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

  1. airserver怎么下载使用?IOS设备无线传送到Mac电脑的屏幕上教程

    AirSever是一种通用的镜像接收器,非常适合镜像iPhone或Android视频文件. 它可以将简单的大屏幕或投影仪变成通用的屏幕镜像接收器. 使用启用了AirSever的设备,您可以用户拥有iP ...

  2. IOS UIView 01-View开始深入 绘制像素到屏幕上

    前言 一个像素是如何绘制到屏幕上去的?有很多种方式将一些东西映射到显示屏上,他们需要调用不同的框架.许多功能和方法的结合体.这里我们大概的看一下屏幕之后发生的事情.当你想要弄清楚什么时候.怎么去查明并 ...

  3. 京东/淘宝的手机销售榜(前4名 -- 手机品牌 --手机型号*3 --手机分辨率 -- 手机操作系统 --安卓版本号)(android / IOS)

    京东手机金榜 (1)Apple iPhone 11(自营店) 手机品牌:Apple 手机型号:Apple iPhone 11 手机分辨率:1792×828 手机操作系统:IOS 对比度:1400:1 ...

  4. 苹果x有android文件夹,iOS有大变化:新增像安卓手机的文件管理夹

    原标题:iOS有大变化:新增像安卓手机的文件管理夹 6月6日凌晨1点,苹果将于美国加州圣何塞McEnery会议中心举办苹果全球开发者大会(WWDC 2017). 作为 WWDC 开幕当天的重头戏,今年 ...

  5. 6s升级12系统无服务器,老iPhone升级iOS 12后,我想砸手机了

    原标题:老iPhone升级iOS 12后,我想砸手机了 或许是为了减少「降频门」事件带来的消极影响,iOS 12 可谓是诚意十足. 许多 iPhone 5s.iPhone 6 等老旧机型用户纷纷表示, ...

  6. ios 代码设置控件宽高比_IOS开发入门之iOS一套代码适配所有iPhone手机布局实现方案...

    本文将带你了解IOS开发入门iOS一套代码适配所有iPhone手机布局实现方案,希望本文对大家学IOS有所帮助. iOS一套代码适配所有iPhone手机布局实现方案 本文主要是讲述一套代码适配所有iP ...

  7. iOS一套代码适配所有iPhone手机布局实现方案

    iOS一套代码适配所有iPhone手机布局实现方案 本帖主要是讲述一套代码适配所有iPhone手机布局解决方案.要点是了解原理和思想. 多个项目采用了这种布局方式,感觉良好,实际情况还是要看看你们UI ...

  8. 最新多屏群控技术---手机控制手机/苹果群控/IOS群控/实时同步操作群控功能讲解以及入门教程

    通过无线/数据线 发送命令来操作主控手机来带动全部被控手机,电脑任意更换不限制 主控手机怎么操作被控手机全部同步进行相同操作 支持一键每台手机输入不一样的文字!多屏统一操作,多屏单独操作! 无需连接U ...

  9. h5 video 手机上无法显示_怎样把手机上的导航显示在汽车屏幕上

    目前安卓或者iOS都支持车载互联功能,如果车机支持相应的功能只需要通过有线或无线方式连接即可:此外,安卓还可以通过MHL线连接到车机的HDMI接口处实现投屏.具体地: 1.如果您的车机支持Androi ...

  10. 微信可以远程控制电脑吗_上车| 手机远程控制手机,还可以控制电脑

    对于一个经常使用电脑学习/工作的人来说,每天都要坐在电脑前敲敲这个,点点那个,但是总有比较懒或者有其他事情要走开的时候,这个时候,你是选择硬撑到完成电脑上正在做的事情?还是选择抱着电脑开着热点随时移动 ...

最新文章

  1. 存储过程while_超详细的Oracle存储过程基础入门介绍
  2. RabbitMQ (三)消息重试
  3. java代码连接数据库
  4. android sd大小,android用户的SD卡相同容量是否完全一样的大小
  5. ubuntu中的Jupyter Notebook更换浏览器需要输入密码
  6. java迭代器 异常_java-迭代器并发修改异常
  7. 统治世界的十大算法(转)
  8. windows修改环境变量
  9. 这台计算机上缺少此项目引用的 NuGet 程序包,DotNetCompilerPlatform
  10. 毕业3年,我换了4份工作:好工作,是这样“熬”出来的
  11. edge chrome Android,微软Edge浏览器正式登陆Android平台
  12. Depth Map Prediction from a Single Image using a Multi-Scale Deep Network
  13. 计算机学院毕业生德育总结,毕业生德育答辩总结_相关文章专题_写写帮文库
  14. BootLoad中上位机与下位机交互
  15. Android 旋转动画简单实现
  16. 大工计算机英语考试,大工15春《专业英语(计算机英语)》在线测试123
  17. OpenCV读取图片
  18. php 上传微信客服头像,微信多客服上传头像
  19. React- Hook 踩坑“useState“ is called in function “addP“ which is neither a React
  20. RxJava+Retrofit+Mvp实现购物车(没有结算页面)

热门文章

  1. 一部火了20年的音乐剧,到底有何魅力?
  2. p2p网络中的节点发现之UDP
  3. Modern Effective C++
  4. @人生随笔:一年一影帝,百年周星驰
  5. PR1:初识PR界面
  6. APP分发是什么意思!为什么需要用到呢
  7. uniapp ios 跳转appstore
  8. VR终极入门指南:设备,内容,手机...都在这里了|深扒
  9. 全国python一级考试_全国青少年软件编程(Python)等级考试试卷(一级)测试卷...
  10. 实测macOS双开微信客户端