1, 掉落(重力)行为--UIGravityBehavior
2,碰撞行为--UICollisionBehavior
3,抛掷行为--UIPushBehavior
4,吸附行为--UIAttachmentBehavior
5,迅猛移动弹跳摆动行为 --UISnapBehavior
6,动力元素行为--UIDynamicItemBehavior
一 ,掉落(重力)行为--UIGravityBehavior
常用方法:
- (void)addItem:(id)item;
- (void)removeItem:(id)item;
- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude;
添加或者删除对象组
常用属性:
CGVector gravityDirection;重力方向(是一个二维向量)默认值 (0,1),代数几何中的方向向量,x,y所成角度即是重力掉落方向
CGFloat angle;重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)
-360-360
CGFloat magnitude;量级(用来控制加速度,1.0代表加速度是1000 points /second²)
具体使用是这样的:gravityDirection 二维向量确定向量的方向,在确定方向后,根据angle 的 大小决定下落方向,magnitude决定下落的加速度大小。
.h文件
@property (nonatomic, strong) UIView *squareView;
@property (nonatomic, strong) UIDynamicAnimator *animator;
.m文件
/*重力效果*/
self.squareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
self.squareView.backgroundColor = [UIColor yellowColor];
self.squareView.center = self.view.center;
[self.view addSubview:self.squareView];
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.squareView]];
gravity.gravityDirection = CGVectorMake(0, 1);
gravity.angle = 45;
gravity.magnitude = 5.0;
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
[self.animator addBehavior:gravity];
二,碰撞行为--UICollisionBehavior
常用的方法
- (instancetype)initWithItems:(NSArray *)items;
- (void)addItem:(id)item;
- (void)removeItem:(id)item;
@property (nonatomic, readonly, copy) NSArray* items;
- (void)removeAllBoundaries;
参数分析:
UICollisionBehaviorMode collisionMode;
有以下三个参数
UICollisionBehaviorModeItems = 1 << 0,
设置这个参数说明碰撞会在dynamic items之间检测
UICollisionBehaviorModeBoundaries = 1 << 1,
设置这个参数说明碰撞在我们设置的边界上检测
UICollisionBehaviorModeEverything = NSUIntegerMax
设置这个参数说明检测一切的碰撞,不考虑items,边界,或者其他
这三个值可以使用 | 组合使用
BOOL translatesReferenceBoundsIntoBoundary;是否以参照视图的bounds为边界
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets; 与上一个参数一致,设置EdgeInsets
- (void)addBoundaryWithIdentifier:(id)identifier forPath:(UIBezierPath*)bezierPath;
- (void)addBoundaryWithIdentifier:(id)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
- (UIBezierPath*)boundaryWithIdentifier:(id)identifier;
- (void)removeBoundaryWithIdentifier:(id)identifier;
自定义路线,要么使用 UIBezierPath,要么自定义point
@property (nonatomic, assign, readwrite) id collisionDelegate;
设置Contact不同状态下需要自定义的行为,
代码如下:
.h文件
@property (nonatomic, strong) NSMutableArray *squareViews;
@property (nonatomic, strong) UIDynamicAnimator *animator;
.m文件
- (void) viewDidAppear:(BOOL)animated-内
/* 碰撞效果 */
self.squareViews = [[NSMutableArray alloc] initWithCapacity:5];
NSArray *colors = @[[UIColor greenColor],[UIColor yellowColor]];
CGPoint currentCenterPoint = self.view.center;
CGSize eachViewSize = CGSizeMake(50, 50);
for (int counter = 0; counter < 2; counter ++) {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, eachViewSize.width, eachViewSize.height)];
newView.backgroundColor = colors[counter];
newView.center = currentCenterPoint;
currentCenterPoint.y += eachViewSize.height +10;
[self.view addSubview:newView];
[self.squareViews addObject:newView];
}
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
/*create gravity*/
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:self.squareViews];
[self.animator addBehavior:gravity];
/* create collision */
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:self.squareViews];
// collision.translatesReferenceBoundsIntoBoundary = YES;
collision.collisionMode = UICollisionBehaviorModeBoundaries|UICollisionBehaviorModeItems|UICollisionBehaviorModeEverything;
collision.collisionDelegate = self;
[collision
addBoundaryWithIdentifier:kBottomIdentifier
fromPoint:CGPointMake(0.0f, self.view.bounds.size.height - 100.0f)
toPoint:CGPointMake(self.view.bounds.size.width,
self.view.bounds.size.height - 100.0f)];
[self.animator addBehavior:collision];
#pragma mark UICollisionViewDelegate
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id)item withBoundaryIdentifier:(id)identifier atPoint:(CGPoint)p
{
NSString *paramIdentifier = (NSString *)identifier;
if ([paramIdentifier isEqualToString:kBottomIdentifier]) {
[UIView animateWithDuration:0.5 animations:^{
UIView *view = (UIView *)item;
view.backgroundColor = [UIColor redColor];
view.alpha = 0.0;
view.transform = CGAffineTransformMakeScale(2.0f, 2.0f);
} completion:^(BOOL finished) {
UIView *view = (UIView *)item;
[behavior removeItem:item];
[view removeFromSuperview];
}];
}
}
三,抛掷行为--UIPushBehavior
常用方法:
- (instancetype)initWithItems:(NSArray *)items mode:(UIPushBehaviorMode)mode;
- (void)addItem:(id)item;
- (void)removeItem:(id)item;
@property (nonatomic, readonly, copy) NSArray* items;
常用参数:
UIPushBehaviorMode mode;
下面有两个参数:
UIPushBehaviorModeContinuous,
UIPushBehaviorModeInstantaneous
GFloat angle;
CGFloat magnitude;(1.0 = 100 points per s^2)
CGVector pushDirection;向量方向 和 UIGravityBehavior类似
4,吸附行为--UIAttachmentBehavior
attachment是里面相对复杂的一个,不过也是不错的一个行为
属性:
CGPoint anchorPoint; 锚点
CGFloat length;吸附行为中的两个吸附点之间的距离,通常用这个属性来调整吸附的长度
CGFloat damping;描述吸附行为减弱的阻力大小
CGFloat frequency;吸附行为震荡的频率
UIAttachmentBehaviorType attachedBehaviorType;吸附类型,有下面两个值
UIAttachmentBehaviorTypeItems,表示连接两个item的吸附行为
UIAttachmentBehaviorTypeAnchor,表示连接一个item与锚点的吸附行为
附代码
.h文件
@property (nonatomic, strong) UIView *squareView;
@property (nonatomic, strong) UIView *squareViewAnchorView;
@property (nonatomic, strong) UIView *anchorView;
@property (nonatomic, strong) UIAttachmentBehavior *atachmentBehavior;
@property (nonatomic, strong) UIDynamicAnimator *animator;
.m文件
/*吸附动画*/
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self createSmallSquareView];
[self createAnchorView];
[self createGestureRecgnizer];
[self creataAnimatorAndBehavior];
}
/* attachmentBehavoir */
- (void) createSmallSquareView
{
self.squareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
self.squareView.backgroundColor = [UIColor greenColor];
self.squareView.center = self.view.center;
self.squareViewAnchorView = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 20, 20)];
self.squareViewAnchorView.backgroundColor = [UIColor brownColor];
[self.squareView addSubview:self.squareViewAnchorView];
[self.view addSubview:self.squareView];
}
- (void) createAnchorView
{
self.anchorView = [[UIView alloc] initWithFrame:CGRectMake(120, 120, 20, 20)];
self.anchorView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.anchorView];
}
- (void) createGestureRecgnizer
{
UIPanGestureRecognizer *panGestureRecgnizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:panGestureRecgnizer];
}
- (void) creataAnimatorAndBehavior
{
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
/* create collision detection*/
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.squareView]];
collision.translatesReferenceBoundsIntoBoundary = YES;
self.atachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.squareView offsetFromCenter:UIOffsetMake(self.squareViewAnchorView.center.x,self.squareViewAnchorView.center.y) attachedToAnchor:self.anchorView.center];
self.atachmentBehavior.length = 100;
self.atachmentBehavior.frequency = 50;
self.atachmentBehavior.damping = 3.0;
[self.animator addBehavior:collision];
[self.animator addBehavior:self.atachmentBehavior];
}
- (void) handlePan:(UIPanGestureRecognizer *)panGesture
{
CGPoint tapPoint = [panGesture locationInView:self.view];
[self.atachmentBehavior setAnchorPoint:tapPoint];
self.anchorView.center = tapPoint;
}
5,迅猛移动弹跳摆动行为 --UISnapBehavior
其本身的效果,类似于果冻在光滑的桌面滑动的效果,挺不错的,萌萌搭。
这个和上面的没什么不同,区分点在于,在更新snapBehavior 时,要先移除旧的 snapBehavior,每一个animator 只能保有一个snapBehavior ,当加入多个的时候,animator 无法区分你要做哪个,索性就不做了。
属性:
CGFloat damping;就是滑动到point时晃动的剧烈程度,值从0到1
代码实例
.h文件
@property (nonatomic, strong) UISnapBehavior *snapBehavoir;
@property (nonatomic, strong) UIDynamicAnimator *animator;
.m文件
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
/* UISnapBehavior */
[self createSmallSquareView];
[self createGestureRecgnizer];
[self createAnimatorAndBehavoir];
}
/*UISnapBehavoir */
- (void) createSmallSquareView
{
self.squareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
self.squareView.backgroundColor = [UIColor greenColor];
self.squareView.center = self.view.center;
[self.view addSubview:self.squareView];
}
- (void) createAnimatorAndBehavoir
{
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
/* create collision detection */
UICollisionBehavior *collisionBehavier = [[UICollisionBehavior alloc] initWithItems:@[self.squareView]];
collisionBehavier.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collisionBehavier];
self.snapBehavoir = [[UISnapBehavior alloc] initWithItem:self.squareView snapToPoint:self.squareView.center];
self.snapBehavoir.damping = 0.5f;
[self.animator addBehavior:self.snapBehavoir];
}
- (void) createGestureRecgnizer
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.view addGestureRecognizer:tap];
}
- (void) tapAction:(UITapGestureRecognizer *)tapGesture
{
CGPoint tapPoint = [tapGesture locationInView:self.view];
if (self.snapBehavoir != nil) {
[self.animator removeBehavior:self.snapBehavoir];
}
self.snapBehavoir = [[UISnapBehavior alloc] initWithItem:self.squareView
snapToPoint:tapPoint];
self.snapBehavoir.damping = 0.5f; /* Medium oscillation */
[self.animator addBehavior:self.snapBehavoir];
}
六,动力元素行为--UIDynamicItemBehavior
真实物体特效,可以配合弥补前面的一些不足
BOOL allowsRotation; 强制某物不旋转
CGFloat angularResistance;
CGFloat friction;0-1,决定了这个物质与其他物质擦边或者碰撞时的摩擦力。
CGFloat resistance;运动阻力,大小0-CGFLOAT_MAX
CGFloat elasticity;0-1说明其弹性大小
CGFloat density;不会直接使用,被animator用来计算这如何影响你的动画。
代码示例:
.m文件
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *topView = [self newViewWithCenter:CGPointMake(100, 0) backgroundColor:[UIColor greenColor]];
UIView * bottomView = [self newViewWithCenter:CGPointMake(100, 50) backgroundColor:[UIColor yellowColor]];
[self.view addSubview:topView];
[self.view addSubview:bottomView];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
/*create gravity*/
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[topView,bottomView]];
[self.animator addBehavior:gravity];
/*create collision */
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[topView, bottomView]];;
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];
/*create DynamicItem */
UIDynamicItemBehavior *moreElasticItem = [[UIDynamicItemBehavior alloc] initWithItems:@[bottomView]];
moreElasticItem.elasticity = 1.0f;
UIDynamicItemBehavior *lessElasticItem = [[UIDynamicItemBehavior alloc] initWithItems:@[topView]];
lessElasticItem.elasticity = 0.5f;
[self.animator addBehavior:moreElasticItem];
[self.animator addBehavior:lessElasticItem];
}
- (UIView *) newViewWithCenter:(CGPoint)parament
backgroundColor:(UIColor *)paramBackground
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
newView.backgroundColor = paramBackground;
newView.center = parament;
return newView;
}