ios之小应用-重力感应
重力感应代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #import "ViewController.h" #import <CoreMotion/CoreMotion.h> @interface ViewController () //创建管理对象 水平仪 @property ( nonatomic , strong) CMMotionManager *manager; //创建动画对象 @property ( nonatomic , strong) UIDynamicAnimator *dyanimat; //重力 @property ( nonatomic , strong) UIGravityBehavior *gravit; //碰撞 @property ( nonatomic , strong) UICollisionBehavior *collision; @end @implementation ViewController - ( void )viewDidLoad { [ super viewDidLoad]; } #pragma mark - 实例化对象 - (CMMotionManager *)manager { if (_manager == nil ) { _manager = [[CMMotionManager alloc] init]; _manager.deviceMotionUpdateInterval = 0.01; } return _manager; } - (UIDynamicAnimator *)dyanimat { if (_dyanimat == nil ) { _dyanimat = [[UIDynamicAnimator alloc] initWithReferenceView: self .view]; } return _dyanimat; } - (UIGravityBehavior *)gravit { if (_gravit == nil ) { _gravit = [[UIGravityBehavior alloc] init]; } return _gravit; } - (UICollisionBehavior *)collision { if (_collision == nil ) { _collision = [[UICollisionBehavior alloc] init]; _collision.translatesReferenceBoundsIntoBoundary = YES ; } return _collision; } #pragma mark - 给对象添加动画 - ( void )touchesBegan:( NSSet <UITouch *> *)touches withEvent:(UIEvent *)event { //最多可添加50个 if ( self .view.subviews.count >= 50) { NSLog (@ "已到上限" ); return ; } //获取手指的点 UITouch *touch = touches.anyObject; CGPoint point = [touch locationInView: self .view]; //创建及切圆角 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; view.layer.cornerRadius = 10; view.layer.masksToBounds = YES ; //手指的点就是view的中心点 view.center = point; //随机颜色 view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]; [ self .view addSubview:view]; //将对象添加到动画里 [ self .dyanimat addBehavior: self .gravit]; [ self .dyanimat addBehavior: self .collision]; // 为view添加重力效果 [ self .gravit addItem:view]; // 为view添加碰撞效果 [ self .collision addItem:view]; // 开始监听 [ self .manager startDeviceMotionUpdatesToQueue: NSOperationQueue .mainQueue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) { // 设置重力方向 self .gravit.gravityDirection = CGVectorMake(motion.gravity.x, -motion.gravity.y); }]; //打印添加的控件的个数 NSLog (@ "%zd - %@" , self .view.subviews.count, view); } - ( void )didReceiveMemoryWarning { [ super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end |
下面是模拟器截图,正常情况是真机去测试的,因为水平仪模拟器是没办法测的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY