网上资料:http://ningandjiao.iteye.com/blog/2009542
#import "ViewController.h"
#import "AZball.h"
@interface ViewController ()
{
AZball *_ball;
UIDynamicAnimator *dynamicAnimator;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/**
UIDynamics -- 力学部分
*/
_ball=[[AZball alloc] initWithFrame:CGRectMake(30, 40, 30, 30)];
[self.view addSubview:_ball];
//创建行为
/*
可以自定义行为,也可以使用默认的已经封装的一些无力行为:
重力行为:UIGravityBehavior
碰撞行为:UICollisionBehavior
施加一个力:UIPushBehavior
*/
// 力学容器,不能提前释放,成员变量
dynamicAnimator=[[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 重力行为
UIGravityBehavior *gravityBehiver=[[UIGravityBehavior alloc] init];
// 添加受力对象
[gravityBehiver addItem:_ball];
// 碰撞行为
UICollisionBehavior *collisionBehavior=[[UICollisionBehavior alloc] initWithItems:@[_ball]];
//墙壁反弹
collisionBehavior .translatesReferenceBoundsIntoBoundary=YES;
// 施加力
UIPushBehavior *pushBehiver=[[UIPushBehavior alloc] initWithItems:@[_ball] mode:UIPushBehaviorModeContinuous];
// 施加力的方向
pushBehiver.pushDirection=CGVectorMake(0.8, 0);
//将行为放置到 力学容器 ,开始力学效应。
[dynamicAnimator addBehavior:gravityBehiver];
[dynamicAnimator addBehavior:collisionBehavior];
[dynamicAnimator addBehavior:pushBehiver];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}