iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上

我们谈到了重力上述财产UIGravityBehavior这个类。

非常明确的看法,当我们添加的属性的严重性后,,苹果UIview像掉进无底洞,地下坠,不断的加速。而如今呢,我们要在这个手机屏幕上,加入一个地面。使不断下落的苹果终于有一个着陆点。那么我们怎样为这个视图加入一个地面呢。例如以下(当前内容承接上文内容,如有问题。请看上文:UIGravityBehavior):

首先在.h文件里创建一个UICollisionBehavior对象

.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UIDynamicAnimator * _animator;
    UIGravityBehavior * _gravity;
    //new
    UICollisionBehavior * _ground;
}
@end

然后在.m文件里为UICollisionBehavior对象进行初始化。并为apple对象设置边框属性。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView * apple = [[UIView alloc] initWithFrame:CGRectMake(40,40, 40, 40)];
    apple.backgroundColor = [UIColor redColor];
    [self.view addSubview:apple];
    
    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    _gravity = [[UIGravityBehavior alloc] initWithItems:@[apple]];
    [_animator addBehavior:_gravity];
    
    /*   UICollisionBehavior   */
    _ground = [[UICollisionBehavior alloc] initWithItems:@[apple]];
    _ground.translatesReferenceBoundsIntoBoundary = YES;
    [_animator addBehavior:_ground];
}

这里,以上的代码创建了一个碰撞的行为。它定义了一个或者多个边界交互的特性,这里没有显示的为各个空间加入边界,而是隐式的调用了一个UICollisionBehavior的属性translatesReferenceBoundsIntoBoundary,将这个属性设置为YES之后。

会使边界引用使用视图提供的UIDynamicAnimator边界。构建和执行,你就会看到效果,apple落到地上弹起来了,又又一次落到地上。



点击关注我。很多其它精彩内容!。!

群号:336146073

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/mengfanrong/p/4736317.html