**第一次写博客有点激动呢**


使用 UIDynamic 的方法就不叙述了
这里先讲一下 牛顿摆是经过计算的一个公式才能达到理想状态 由于我不知道公式 所以做出来不能完美的达成 一个小球落下来另一个小球飞起来

接下来直接上代码 代码中的描述还是很详细的

#import "ViewController.h"
#import "ContentView.h"
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];ContentView *view =[[ContentView alloc]initWithFrame:self.view.bounds];view.backgroundColor=[UIColor blueColor];[self.view addSubview:view];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

#import "ContentView.h"
#import "BallsView.h"
@interface ContentView(){UIDynamicAnimator *_animator;//仿真模拟器NSMutableArray *_arrayBalls;CGPoint _centers[100];//可变的小球中心坐标UISnapBehavior *_snapB;UIAttachmentBehavior *attachement1;
}
@endstatic CGFloat radius=20;//小球半径
static CGFloat attachmentLenth=500;//链接长度
@implementation ContentView
-(id)initWithFrame:(CGRect)frame{if (self=[super initWithFrame:frame]) {//初始化仿真模拟器_animator=[[UIDynamicAnimator alloc]initWithReferenceView:self];//初始化所有仿真元素 就是小球[self creatBalls];//添加小球的行为元素[self addBehavior];//利用 drawRect 方法绘制小球和锚点的连线//使用 touchBegan方法判断我们要抓住的小球 用 touchMove的方法使我们的小球随着手指滑动 使用 touchEnd 方法移除 snap 行为释放小球}return  self;
}static int b;
-(void)addBehavior{//这样的方法要经常使用 可以避免程序的崩溃if (!_arrayBalls.count) {return;}//仿真元素的行为描述UIDynamicItemBehavior *itemBehavior=[[UIDynamicItemBehavior alloc]initWithItems:_arrayBalls];itemBehavior.elasticity=1.00001;//弹性itemBehavior.resistance=.01;//阻尼itemBehavior.density=1000;//密度itemBehavior.allowsRotation=NO;//禁止旋转[_animator addBehavior:itemBehavior];//因为我们的小球都需要重力行为直接添加上就可以了UIGravityBehavior *gravityB=[[UIGravityBehavior alloc]initWithItems:_arrayBalls];gravityB.magnitude=10;//重力加速度[_animator addBehavior:gravityB];//添加碰撞行为UICollisionBehavior *collisionB=[[UICollisionBehavior alloc]initWithItems:_arrayBalls];[_animator addBehavior:collisionB];//添加链接行为 链接行为是将两个点相连for (int i=0; i<_arrayBalls.count; i++) {UIAttachmentBehavior *attachmentB=[[UIAttachmentBehavior alloc]initWithItem:_arrayBalls[i] attachedToAnchor:_centers[i]];attachmentB.length=attachmentLenth;attachmentB.damping=0;attachmentB.frequency=0;[_animator addBehavior:attachmentB];}}-(void)creatBalls{_arrayBalls=[[NSMutableArray alloc]initWithCapacity:1];//计算小球中心的坐标for (int i=0; i<6; i++) {CGPoint p=CGPointMake(60+i*2*radius+i*3, 30);_centers[i]=p;}for (int i=0; i<6; i++) {//这里创建一个小球的 viewBallsView *ball=[[BallsView alloc]initWithFrame:CGRectMake(0, 0, 2*radius, 2*radius) withRadius:radius];//在这里确定小球的初始位置ball.center=_centers[i];//这里添加的 kvo 方法作用是当监听到小球的中心变化时 执行 setNeedDisplay 方法 走了这方法后 就会重新绘制contentView[ball addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];[self addSubview:ball];[_arrayBalls addObject:ball];}}- (void)drawRect:(CGRect)rect {//绘图第一点 获取上下文CGContextRef context=UIGraphicsGetCurrentContext();//绘制连接线for (int i=0; i<6; i++) {UIView *view=_arrayBalls[i];CGContextMoveToPoint(context, _centers[i].x, _centers[i].y);CGContextAddLineToPoint(context,view.center.x, view.center.y);}CGContextStrokePath(context);}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{[self setNeedsDisplay];
}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{UITouch *touch=[touches anyObject];for (int i=0; i<6; i++) {UIView *view=_arrayBalls[i];CGPoint point=[touch locationInView:view];if ([view pointInside:point withEvent:nil]) {//NSLog(@"%d",i);b=i;}}
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{if (_snapB==nil) {UITouch *touch=[touches anyObject];CGPoint point=[touch locationInView:self];_snapB=[[UISnapBehavior alloc]initWithItem:_arrayBalls[b] snapToPoint:point];//NSLog(@"%d",b);[_animator addBehavior:_snapB];}else{[_animator removeBehavior:_snapB];_snapB=nil;}}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{[_animator removeBehavior:_snapB];
}
@end
#import <UIKit/UIKit.h>@interface BallsView : UIView
-(id)initWithFrame:(CGRect)frame withRadius:(CGFloat)radius;
@end
#import "BallsView.h"@implementation BallsView-(id)initWithFrame:(CGRect)frame withRadius:(CGFloat)radius{if (self =[super initWithFrame:frame]) {//初始化小球的属性self.layer.cornerRadius=radius; self.layer.borderColor=[UIColor greenColor].CGColor;self.layer.borderWidth=2;self.backgroundColor=[UIColor yellowColor];self.layer.shadowColor=[UIColor blackColor].CGColor;self.layer.shadowRadius=.5;}return self;
}@end

iOS牛顿摆 小球撞击 物理学仿真模拟相关推荐

  1. 制作简易的牛顿摆锤模型

    作者:坚定的守猴撰写时间:2019年1月20日 开发工具和关键技术:DW CSS3动画旋转和延迟 牛顿摆锤,一个有意思物理模型.下面用CSS3的动画来实现这有意义模型. 代码如下: 先看结构 结构就是 ...

  2. 二阶倒立摆matlab建模与仿真,二级倒立摆的建模与MATLAB仿真.pdf

    二级倒立摆的建模与MATLAB仿真 MATLAB 二级倒立摆的建模与 仿真 刘文斌,等 二级倒立摆的建模与MATLAB仿真 刘文斌,千树川 3000) (四川理工学院电子与信息工程系 四川自贡,64 ...

  3. 用Java程序仿真模拟病毒传播过程(已开源)

    无知和弱小不是生存的障碍,傲慢才是. <三体> 最近,在Github上面有一个 VirusBroadcast 开源项目,它利用Java程序仿真模拟了冠状病毒的传播过程「理想状态下」. 正如 ...

  4. 计算机物理仿真模拟培养方案,中学物理仿真模拟实验.doc

    中央电大毕业设计 毕 业 论 文 专 业:计算机基础与应用 年 级:2004年春计算机本科 学 号:041060219 姓 名:倪亚非 指导老师:李征 2005年12月28日 物理仿真模拟实验 --- ...

  5. android实现牛顿摆

    上次看到一个小伙伴实现了牛顿摆,发现挺有意思的,于是乎自己也想着做一个,虽然说是重复造轮子,我看中的是这个过程,顺便巩固一下自定义view,并且我做了一个扩展,可以自定义线和小球的尺寸与颜色,还有初始 ...

  6. chatgpt赋能python:Python做仿真模拟:一种高效、灵活、易用的工具

    Python做仿真模拟:一种高效.灵活.易用的工具 介绍 随着计算机技术的不断进步,仿真模拟已成为许多学科研究中不可缺少的工具之一.在许多领域,例如物理.生物.经济等,都需要使用仿真模拟的技术来预测. ...

  7. wps 模拟分析 规划求解_FFU气流仿真模拟,到底有多简单?

    FFU风机过滤机组是将风机和过滤器组合在一起构成自身能提供动力的末端净化设备,我们可以简单把它理解成,工业用的空气净化器.它主要用于对空气洁净度要求很高的环境,目前在各种洁净工程中得到了广泛应用. 本 ...

  8. 仿真模拟,需要注意这几点!

    周日晚上的直播甚是精彩 先是潘同学分享了 获奖论文和解题技巧 接着是董同学分享了 论文的写作小技巧 po几张截图让大家感受下 此时此刻可能会有不少童鞋 正在为错过直播而懊悔 不用担心 超模君还准备了一 ...

  9. 修改fragment的进入动画_3DsMax—牛顿摆球(动量守恒摆球)动画

    最终效果 本篇为图文教程,已经将牛顿摆球动画视频教程放到3dsmax学习网中,需要看视频教程的请到3DsMax学习网(www.dddmax.cn)教程中观看. 01 打开一个牛顿摆球模型,如图所示.( ...

  10. rsoft透射谱_基于Rsoft软件光纤光栅的仿真模拟

    ​光纤光栅无论在光纤传感领域还是光纤激光器方面被大量研究,那么如何在没有进行实体实验的情况下来进行光纤光栅的模拟呢,本案例主要利用光波导Rsoft仿真软件对光纤光栅进行模拟分析研究.相信这种方法很实用 ...

最新文章

  1. 独家 | TensorFlow 2.0将把Eager Execution变为默认执行模式,你该转向动态计算图了...
  2. 利用kickstart自动安装虚拟机
  3. 生成树的概念,最小生成树Prim算法 Kruskal算法
  4. goland 远程调试 golang
  5. .net Int16 、(int Int32)、 Int64 的区别
  6. ConcurrentHashMap源码学习
  7. 佳能2900打印机与win10不兼容_佳能mg3660 3680 系列喷墨打印机更换搓纸轮解决不进纸教程...
  8. GC之7大垃圾收集器详解(下)
  9. mysql最大连接数查询_MYSQL 查看最大连接数和修改最大连接数
  10. vba 修改下拉列表_Excel隐藏的超实用技能,涉及VBA技巧,建议【收藏】
  11. 《朝花夕拾》金句摘抄(五)
  12. java动态调用c++库
  13. 雷神开机logo更改_九代酷睿i9加持的性能怪兽 雷神911黑武士Ⅱ评测
  14. flowable 动态多实例
  15. 云服务器的发展历程,盘点微软Azure云服务器的发展历程
  16. 佳能g2810打印机扫描怎么用_canon g2810驱动下载
  17. 管理小故事精髓 100例
  18. PHP+node采集58微聊聊天信息
  19. 视频教程-ASP.NET就业实例视频教程(1)基础入门——搭建网站开发环境教学视频-.NET
  20. echarts 柱状图圆柱_使用echarts画柱状图

热门文章

  1. 前端进行身份证验证(详细)
  2. 【SSM框架】MyBatis
  3. 如何听清楚、说明白--《结构思考力》
  4. Android常用布局-01
  5. java调用本地打印机,绘制打印模板,小票模板
  6. java后台实现CKFinder2.3版本+阿里OSS存储
  7. VMware虚拟机复制文件卡死的问题
  8. CCC与Android交互的注意点
  9. error: You must be logged in to the server (Unauthorized)
  10. 关于计算机上使用的光盘,电脑中使用Windows DVD Maker制作光盘的方法