UIDynamics -- 力学部分

来源:互联网 发布:python的入门书 知乎 编辑:程序博客网 时间:2024/04/29 23:18

网上资料: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.}


0 0
原创粉丝点击