上文我们讲到了重力属性UIGravityBehavior这个类。很明显当我们为视图加上了重力的属性之后,这个苹果的UIview就如同掉入了无底洞,不断地下坠,不断的加速。而现在呢,我们要在这个手机屏幕上,添加一个地面。使不断下落的苹果最终有一个着陆点。那么我们如何为这个视图添加一个地面呢,如下(当前内容承接上文内容,如有问题,请看上文:UIGravityBehavior):
首先在.h文件中创建一个UICollisionBehavior对象
.h:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIDynamicAnimator * _animator; UIGravityBehavior * _gravity; //new UICollisionBehavior * _ground; } @end
- (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]; }
群号:336146073
iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上,布布扣,bubuko.com
iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上
原文:http://blog.csdn.net/grozy_sun/article/details/24202305