• IOS 物理引擎


    来自IOS7 by tutorials   下面是个人的一点翻译总结    

    1,首先在初始化方法李画一个方块 

    UIView* square = [[UIView alloc] initWithFrame:
    CGRectMake(100100100100)];

    square.backgroundColor = [UIColor grayColor]; [self.view addSubview:square];

    2,声明两个变量。

     

    UIDynamicAnimator* _animator;

    UIGravityBehavior* _gravity; 

    3,初始化

     

    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    _gravity = [[UIGravityBehavior alloc] initWithItems:@[square]]; [_animator addBehavior:_gravity];

    4,闲着这个方块直接掉出屏幕了,我们需要加边框。

    声明变量

     

    UICollisionBehavior* _collision;

    代码

    _collision = [[UICollisionBehavior alloc] initWithItems:@[square]];

    _collision.translatesReferenceBoundsIntoBoundary = YES; [_animator addBehavior:_collision];

    5,多个 方块间的冲突

    再声明一个长条。

     

    UIView* barrier = [[UIView alloc]
    initWithFrame:CGRectMake(0, 300, 130, 20)];

    barrier.backgroundColor = [UIColor redColor]; [self.view addSubview:barrier];

    我们看见方块穿过了长条。

    修改代码

     

    _collision = [[UICollisionBehavior alloc] initWithItems:@[square, barrier]];

    这时长条被砸掉下去了。

    6,我们把长条也弄成方框的边框。

     

    _collision = [[UICollisionBehavior alloc] initWithItems:@[square]];

    // add a boundary that coincides with the top edge

    CGPoint rightEdge = CGPointMake(barrier.frame.origin.x + barrier.frame.size.width,

    barrier.frame.origin.y); [_collision addBoundaryWithIdentifier:@"barrier"

    fromPoint:barrier.frame.origin toPoint:rightEdge];

    可以看见方框被长条弹开并落在了底部。

    7,每个动画都由一个方法来执行,如碰撞,反弹等等...添加如下代码

     

    _collision.action = ^{ NSLog(@"%@, %@",

    NSStringFromCGAffineTransform(square.transform), NSStringFromCGPoint(square.center));

    };

    上面代码打印出方框下落时,中心和属性的变化。square.center是方框的中心,square.transform是方框的属性。

    第一部分你会看到方框的中心square.center,的Y轴数据在变化,如

    2014-07-24 11:41:42.133 DynamicsPlayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 161}

    2014-07-24 11:41:42.149 DynamicsPlayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 164}

     

    2014-07-24 11:41:42.167 DynamicsPlayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 167}

    当方块撞到长条时,方块开始转动,这时打印的结果是

    2014-07-24 11:41:42.649 DynamicsPlayground[766:60b] [0.80305022, 0.59591132, -0.59591132, 0.80305022, 0, 0], {170, 266}

    2014-07-24 11:41:42.666 DynamicsPlayground[766:60b] [0.7690773, 0.63915575, -0.63915575, 0.7690773, 0, 0], {172, 269}

     

    2014-07-24 11:41:42.683 DynamicsPlayground[766:60b] [0.72068948, 0.69325799, -0.69325799, 0.72068948, 0, 0], {174, 272}

    8,对象的动态行为是采用的 UIDynamicItem 协议  ,下面是协议的定义。

     

    @protocol UIDynamicItem <NSObject>

    @property (nonatomic, readwrite) CGPoint center;
    @property (nonatomic, readonly) CGRect bounds;
    @property (nonatomic, readwrite) CGAffineTransform transform;

    @end

    9,碰撞通知。添加代码在.m文件里。

     

    @interface ViewController () <UICollisionBehaviorDelegate> @end

    碰撞后

     

    _collision.collisionDelegate = self;

    碰撞后方法

     

    - (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item

    withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p {

    NSLog(@"Boundary contact occurred - %@", identifier); }

    可以看到碰撞后打印的信息,。

    2014-07-24 12:15:45.124 DynamicsPlayground[820:60b] Boundary contact occurred - barrier

     

    2014-07-24 12:15:45.692 DynamicsPlayground[820:60b] Boundary contact occurred - (null)

     

    10,在打印后面添加代码

        UIView* view = (UIView*)item; view.backgroundColor = [UIColor yellowColor];

        [UIView animateWithDuration:0.3

                         animations:^{

                             view.backgroundColor = [UIColor grayColor];

     

                         }];

    会看到方块在每次碰撞后发生的颜色变化。

    11,配置属性。

    下面代码追加在viewDidLoad方法的底部,

        UIDynamicItemBehavior* itemBehaviour =

        [[UIDynamicItemBehavior alloc] initWithItems:@[square]];

     

        itemBehaviour.elasticity = 0.6; 

    [_animator addBehavior:itemBehaviour];

    发现方块变得更有弹性了。除了弹性,itemBehaviour有更多其他属性可以更改。

     

    12,添加动态行为

     

    ViewController.m里添加一布尔型变量

     

    BOOL _firstContact;

    下面代码添加到协议方法里。

        if (!_firstContact) {

            _firstContact = YES;

            UIView* square = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 100, 100)];

            square.backgroundColor = [UIColor grayColor];

            [self.view addSubview:square];

            [_collision addItem:square];

            [_gravity addItem:square];

            UIAttachmentBehavior* attach = [[UIAttachmentBehavior alloc]

                                            initWithItem:view

                                            attachedToItem:square];

           [_animator addBehavior:attach];

        } 

    两个方块被一根隐藏得线联系起来。

     

    来自-IOS7 by tutorials,第二章第一节,目前网上有英文版下载,无中文版翻译。

    --原创可转载 ,如有不恰当的地方可留言--guanliyang

  • 相关阅读:
    POJ1113:Wall (凸包:求最小的多边形,到所有点的距离大于大于L)
    POJ3304:Segments (几何:求一条直线与已知线段都有交点)
    【几何】简单积累
    POJ2689:Prime Distance(大数区间素数筛)
    POJ2142:The Balance (欧几里得+不等式)
    初探云原生应用管理(二): 为什么你必须尽快转向 Helm v3
    初探云原生应用管理(一): Helm 与 App Hub
    AI种黄桃AI卖黄桃 阿里巴巴推进一站式政务服务
    MongoDB sharding 集合不分片性能更高?
    阿里云发布敏感数据保护产品SDDP,数据贴身防护实现“外防内控”
  • 原文地址:https://www.cnblogs.com/guanliyang/p/3865310.html
Copyright © 2020-2023  润新知