UIDynamic-物理引擎
2016-09-08 07:37
232 查看
@interface ViewController () @property (weak, nonatomic) IBOutlet UIView *redView; @property (weak, nonatomic) IBOutlet UISegmentedControl *segment; @property (weak, nonatomic) IBOutlet UIProgressView *progressView; @property (nonatomic, strong) UIDynamicAnimator *animator; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.redView.transform = CGAffineTransformMakeRotation(M_PI_4); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // [self testGravity]; // [self testGravityAndCollision]; // [self testGravityAndCollision2]; // [self testGravityAndCollision3]; [self testSnap:touches]; } - (void)testGravity { UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init]; [gravity addItem:self.redView]; [self.animator addBehavior:gravity]; } - (void)testGravityAndCollision { UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init]; gravity.gravityDirection = CGVectorMake(0, 1); // 二维向量,表示方向。另一个确定方向还有 angle 属性 gravity.magnitude = 1; // 重力加速度。加速度越大,碰撞越厉害 [gravity addItem:self.redView]; UICollisionBehavior *collision = [[UICollisionBehavior alloc] init]; collision.translatesReferenceBoundsIntoBoundary = YES; [collision addItem:self.redView]; [collision addItem:self.progressView]; [collision addItem:self.segment]; [self.animator addBehavior:gravity]; [self.animator addBehavior:collision]; } - (void)testGravityAndCollision2 { UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init]; [gravity addItem:self.redView]; [self.animator addBehavior:gravity]; UICollisionBehavior *collision = [[UICollisionBehavior alloc] init]; [collision addBoundaryWithIdentifier:@"line1" fromPoint:CGPointMake(0, 160) toPoint:CGPointMake(320, 400)]; [collision addBoundaryWithIdentifier:@"line2" fromPoint:CGPointMake(320, 0) toPoint:CGPointMake(320, 400)]; [collision addItem:self.redView]; [self.animator addBehavior:collision]; } - (void)testGravityAndCollision3 { UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init]; [gravity addItem:self.redView]; [self.animator addBehavior:gravity]; UICollisionBehavior *collision = [[UICollisionBehavior alloc] init]; [collision addBoundaryWithIdentifier:@"circle" forPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 320, 320)]]; [collision addItem:self.redView]; [self.animator addBehavior:collision]; } - (void) testSnap:(NSSet *)touches { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:touch.view]; UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point]; snap.damping = arc4random_uniform(10) / 10.0; [self.animator removeAllBehaviors]; [self.animator addBehavior:snap]; } - (UIDynamicAnimator *)animator { if (_animator == nil) { _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; } return _animator; } @end
概念:
物理仿真元素:遵循UIDynamicItem协议的对象;
物理仿真行为:继承UIDynamicBehavior,UIDynamic提供了
UIGravityBehavior-重力行为
UICollisionBehavior-碰撞行为
UISnapBehavior-捕捉行为
UIPushBehavior-推动行为
UIAttachmentBehavior-附着行为
UIDynamicItemBehavior-动力元素行为
物理仿真器:让物理仿真元素执行具体的物理仿真行为,它是UIDynamicAnimator类型的对象;
步骤:
创建物理仿真器,设置仿真范围;
创建物理仿真行为,添加物理仿真元素;
将物理仿真行为添加到物理仿真器中,开始仿真;
注意:
如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除;
[self.animator removeAllBehaviors];
[self.animator addBehavior:snap];
相关文章推荐
- iOS 之物理仿真引擎-UIDynamic简介(一)
- iOS 之物理仿真引擎-UIDynamic简介(二)
- UIDynamic物理引擎
- iOS 之物理仿真引擎-UIDynamic简介(三)
- iOS开发——高级篇——UIDynamic 物理引擎
- UIDynamic 物理引擎
- UIDynamic仿物理引擎-浮动碰撞效果-b
- [cocos2dx]cocos2dx3.x+lua中使用物理引擎
- PhysX物理引擎(编程入门)
- 构建一个简单的物理引擎
- [iphone-Game]物理引擎-资源整理贴 (3.14 更新)
- cocos2d学习笔记(十)物理引擎box2d之四
- 使用cocos2d-js-3.0RC1中的物理引擎chipmunk模拟的“别碰钉子”源码分享(含碰撞检测)
- 物理引擎比较:Box2D 对比 Chipmunk
- quick-cocos2d-x物理引擎之chipmunk(一)
- 虚幻4已免费 PhysX物理引擎有开源希望
- 物理引擎
- 基于JBox2D物理引擎开发的“雷电”小游戏(五)——碰撞
- [Havok] - Havok Physics物理引擎的学习入门
- cocos2dx3.2 物理引擎之碰撞