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

iOS开发 浅解UIDynamicAnimator

2015-10-02 16:00 387 查看
1.UIDynamic是从iOS 7
开始导入的一种新技术,属于UIKit框架,可以模拟显示生活中的物理现象,如碰撞、抖动、摆动等

2.使用UIDynamic
大体步骤:

 1、创建一个动力效果器(UIDynamicAnimator)

 2、创建动力效果(Behavior)添加到对应的视图上

 3、将动力效果添加到动力效果器中
开始动力效果

3.UIDynamic提供的动力效果

 1、UIGravityBehavior:重力效果

 2、UICollisionBehavior:碰撞效果

 3、UIDynamicItemBehavior:动力元素效果

 4、UIPushBehavior:推动效果

 5、UISnapBehavior:迅速移动效果

 6、UIAttachmentBehavior:附着效果

- (void)viewDidLoad {
[super viewDidLoad];
// 动力效果器
dynamicAnimatord = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
// self.view 产生动力效果的有效区域
dynamicAnimatord.delegate = self;

view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
view1.center = self.view.center;
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];

view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.center = CGPointMake(self.view.center.x+100, self.view.center.y+100);
view2.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view2];

}
4、重力效果

UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[view1,view2]];
gravity.magnitude = 1000;// 设置加速度 数值越大,碰撞效果越大....
gravity.gravityDirection = CGVectorMake(0, 1);//X:负数向左,正数向右 Y:负数向上,正数向下
[dynamicAnimatord addBehavior:gravity];


5、边界和物体之间的碰撞效果

// 给view1添加碰撞效果
UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[view1]];

collision.translatesReferenceBoundsIntoBoundary = YES;// 把重力效果器的范围 当做边界
collision.collisionDelegate = self;//挂上代理
[collision addBoundaryWithIdentifier:@"line1" fromPoint:CGPointMake(0, 300) toPoint:CGPointMake(400, 600)];// 通过两个点 画了一条直线 当做边界
// 通过贝塞尔曲线画一个圆形
UIBezierPath *cycle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 200, 300, 300)];
[collision addBoundaryWithIdentifier:@"圆形" forPath:cycle];

[dynamicAnimatord addBehavior:collision];


6、两个视图之间
相互碰撞

UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[view1,view2]];
collision.translatesReferenceBoundsIntoBoundary = YES;
// 如果设置两个元素之间,相互碰撞 设置了边界 也起不了作用
collision.collisionMode = UICollisionBehaviorModeEverything;
collision.collisionDelegate = self;
[dynamicAnimatord addBehavior:collision];


7、动力元素效果

// /可以与其它的 动力效果配合使用
UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc]initWithItems:@[view1,view2]];
// /设置元素的跳跃度(0-1)
item.elasticity = 1;
[dynamicAnimatord addBehavior:item];
8、UISnapBehavior

// 迅速移动效果 只能一次 添加到一个元素上 snapToPoint 让他移动到哪一个点
UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:view1 snapToPoint:[sender locationInView:self.view]];
snap.damping = 0.1;//抖动弧度,阻尼值从0.0到1.0。0.0是最低的振荡

[dynamicAnimator addBehavior:snap];


9、UIPushBehavior

UIPushBehavior *push = [[UIPushBehavior alloc]initWithItems:@[view1] mode:UIPushBehaviorModeContinuous];
// x 正右,负左,y 正上,负下
push.pushDirection = CGVectorMake(1, 0);
push.magnitude = 100;//力度
// push.active = NO;//活动状态 NO不响应,YES响应
[dynamicAnimator addBehavior:push];


10、UIAttachmentBehavior

// view1吸附着view2
UIAttachmentBehavior *attach1 = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(50, 50) attachedToItem:view2 offsetFromCenter:UIOffsetMake(50, 50)];
attach1.length = 10;//<span style="font-family: 'Heiti SC Light';">距离</span><span style="font-family: Menlo;"> </span><span style="font-family: 'Heiti SC Light';">与锚点的距离</span>
attach1.damping = 0.5;//抖动弧度,阻尼值从0.0到1.0。0.0是最低的振荡
attach1.frequency = 10;//频率
[dynamicAnimator addBehavior:attach1];


11、两个物体碰撞动力效果的代理

- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p{
NSLog(@"1撞击的位置X:%f Y:%f",p.x,p.y);
}
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2{

}


12、碰撞边界动力效果的代理
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier atPoint:(CGPoint)p{
NSLog(@"撞击的位置X:%f Y:%f",p.x,p.y);
}
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier{}


13、动力效果器的代理方法
//启动
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator*)animator{
NSLog(@"启动");
}
//暂停
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator{
NSLog(@"暂停");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: