UIDynamicAnimator - 运动管理
UIDynamicBehavior - 运动行为(基类)
- UIGravityBehavior *gravity; - 重力
- UICollisionBehavior *collision; - 碰撞
- UIAttachmentBehavior *attach; - 吸附
- UISnapBehavior *snap; - 振动
- UIPushBehavior *push; - 推
基本流程
- 1 创建运动管理
- 2 创建运动行为
- 3 运动管理 添加 运动行为
- 4 使用的时候,运动行为添加 运动物体。
- 5 注意remove
分开的比较简单,复合就比较多变。
UIGravityBehavior 重力
- 1 UIDynamicAnimator 运动管理者
// 注:运动参照物 self.testView.
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.testView];
self.animator.delegate = self;
...
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator {
// 开始运动
}
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator {
// 运动结束
}
- 2 UIGravityBehavior 重力行为
self.gravity = [[UIGravityBehavior alloc] init];
self.gravity.gravityDirection = CGVectorMake(0.1, 0.3);// 方向 和 速度大小 的 矢量
self.gravity.angle = M_PI;// 方向 弧度
self.gravity.magnitude = 1.;// 重力加速度(速度大小)1000 point/s2 举例:y=gt2
[self.gravity setAngle:M_PI magnitude:0.8];// 动态修改
[self.animator addBehavior:self.gravity];
- 3 添加 item
- (void)toucheTest {
UIView *vvv = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 30, 30)];
vvv.backgroundColor = [UIColor redColor];
[self.testView addSubview:vvv];
[self.gravity addItem:vvv];
}
- 4 必要的时候 remove
NSArray *items = self.gravity.items;
[self.gravity removeItem:vvv];// 注意必要的时候remove
UICollisionBehavior 碰撞
基本步骤与上面的一样,下面直接 运动行为。
// 一般设置
self.collision = [[UICollisionBehavior alloc] init];
self.collision.collisionDelegate = self;
[self.animator addBehavior:self.collision];
// 边界设置
self.collision.collisionMode = UICollisionBehaviorModeEverything;// 碰撞 内容
/*
typedef NS_OPTIONS(NSUInteger, UICollisionBehaviorMode) {
UICollisionBehaviorModeItems = 1 << 0,
UICollisionBehaviorModeBoundaries = 1 << 1,
UICollisionBehaviorModeEverything = NSUIntegerMax
} NS_ENUM_AVAILABLE_IOS(7_0);
*/
self.collision.translatesReferenceBoundsIntoBoundary = YES;// 使用 参考边界
[self.collision setTranslatesReferenceBoundsIntoBoundaryWithInsets:UIEdgeInsetsMake(10, 20, 30, 40)];// 边界 调整
// 1 bezierPath 边界
UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)) radius:CGRectGetWidth(self.view.bounds)/2 startAngle:0 endAngle:2*M_PI clockwise:YES];
[self.collision addBoundaryWithIdentifier:@"bezierPath1" forPath:bezierPath1];
// 2 point1 - point2 边界
[self.collision addBoundaryWithIdentifier:@"point1_point2" fromPoint:CGPointMake(0, 300) toPoint:CGPointMake(CGRectGetWidth(self.view.bounds), 400)];
// 注意 获取 和 remove
NSArray *boundarys = self.collision.boundaryIdentifiers;
[self.collision removeAllBoundaries];
- delegate
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p {
}
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 {
}
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p {
}
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier {
}
UIAttachmentBehavior 依附,跟随
- 正常使用
// 1 可以跟锚点 2 可以item 与 item
self.attach = [[UIAttachmentBehavior alloc] initWithItem:self.view1 attachedToItem:self.view2];
self.attach.length = 100;// 距离
self.attach.damping = 0.3;// 阻尼系数(阻碍变化)
self.attach.frequency = 0.5;// 振动频率,(变化速度)
// self.attach.anchorPoint = CGPointMake(100, 100);
[self.animator addBehavior:self.attach];
// 2者在其他动画作用下保持相互作用力,比如某一个添加重力效果。
- iOS 9
UIKIT_EXTERN const UIFloatRange UIFloatRangeZero NS_AVAILABLE_IOS(9_0);
UIKIT_EXTERN const UIFloatRange UIFloatRangeInfinite NS_AVAILABLE_IOS(9_0);
UIKIT_EXTERN BOOL UIFloatRangeIsInfinite(UIFloatRange range) NS_AVAILABLE_IOS(9_0);
UIKIT_EXTERN BOOL UIFloatRangeIsEqualToRange(UIFloatRange range, UIFloatRange otherRange) NS_AVAILABLE_IOS(9_0);
+ (instancetype)slidingAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point axisOfTranslation:(CGVector)axis NS_AVAILABLE_IOS(9_0);
+ (instancetype)slidingAttachmentWithItem:(id <UIDynamicItem>)item attachmentAnchor:(CGPoint)point axisOfTranslation:(CGVector)axis NS_AVAILABLE_IOS(9_0);
+ (instancetype)limitAttachmentWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2 NS_AVAILABLE_IOS(9_0);
+ (instancetype)fixedAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point NS_AVAILABLE_IOS(9_0);
+ (instancetype)pinAttachmentWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2 attachmentAnchor:(CGPoint)point NS_AVAILABLE_IOS(9_0);
@property (readwrite, nonatomic) CGFloat frictionTorque NS_AVAILABLE_IOS(9_0); // default is 0.0
@property (readwrite, nonatomic) UIFloatRange attachmentRange NS_AVAILABLE_IOS(9_0); // default is UIFloatRangeInfinite
UISnapBehavior
self.snap = [[UISnapBehavior alloc] initWithItem:self.view1 snapToPoint:CGPointMake(100, 100)];
self.snap.damping = 0.3;// 振动频率
UIPushBehavior
self.push = [[UIPushBehavior alloc] init];
self.push.active = YES;// 是否激活
self.push.angle = M_PI/4;// 方向
self.push.magnitude = 0.5;// 力
// self.push.pushDirection = CGVectorMake(1, 2);// 矢量
UIDynamicBehavior 以上运动行为的 父类
可以整合上面几种行为到一起
- (void)addChildBehavior:(UIDynamicBehavior *)behavior;
其他
直接使用好像确实很少。简单使用如上。
1