您的位置:首页 > 产品设计 > UI/UE

[iOS开发] UIKit Dynamics

2014-01-28 10:28 316 查看
关于 UIKit Dynamics 的中译名,我与许多开发者有过讨论,有动力、动力模型、动态等译法。但我认为译为力学更为贴切,希望文中出现的力学知识能让你认同我的看法。

http://www.cocoachina.com/applenews/devnews/2013/1226/7614.html

简单的重力影响view,使之垂直降落,在触底后有一个轻微的弹起,然后再落下

#import "MainViewController.h"

@interface MainViewController ()
@property(nonatomic,retain)UIDynamicAnimator *animator;
@property(nonatomic,retain)UIGravityBehavior *gravity;
@property(nonatomic,retain)UICollisionBehavior *collision;

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
square.backgroundColor = [UIColor redColor];
[self.view addSubview:square];

//会出现重力现象,上面创建的正方形会掉落下去
//UIDynamicAnimator 是 UIKit 物理引擎。这个类会记录你添加到引擎中的各种行为(比如重力),并且提供全局上下文。当你创建动画实例时,需要传入一个参考视图用于定义坐标系。
//UIGravityBehavior 把重力的行为抽象成模型,并且对一个或多个元素施加作用力,让你可以建立物理交互模型。当你创建一个行为实例的时候,你需要把它关联到一组元素上,一般是一组视图。这样你就能选择受该行为影响的元素,在这个例子中就是指受重力影响的元素
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_gravity = [[UIGravityBehavior alloc] initWithItems:@[square]];
[_animator addBehavior:_gravity];

_collision = [[UICollisionBehavior alloc] initWithItems:@[square]];
_collision.translatesReferenceBoundsIntoBoundary = YES;//让UIDynamicAnimator以视图的bounds为边界
[_animator addBehavior:_collision];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: