UIDynamicAnimator - 仿真物理学

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容