关闭

UIDynamic 物理仿真

119人阅读 评论(0) 收藏 举报
分类:

UIDynamic中三个重要的类解析:

  • 1.UIDynamicItem:执行仿真动画的对象

  • 2.UIDynamicAnimator:仿真者,仿真器,相当于控制器

  • 3.UIDynamicBehavior: 动画行为方式

    • UIGravityBehavior:重力行为

    • UICollisionBehavior:碰撞行为

    • UISnapBehavior:吸附行为

    • UIPushBehavior:推动行为

    • UIAttachmentBehavior:刚性附着行为

      TIPS:

      • 1.首先创建仿真器(需要用一个强指针引用),指定执行动画的item(遵守UIDnamicItem协议的对象才能做物理仿真,UIView和UICollectionViewAttributes遵守该协议.)

      • 2.创建动画行为,可单独一种,也可多种

      • 3.添加动画行为到仿真器上

         //1.添加重力属性
            UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[_redView]];
        
            //2.添加碰撞检测
            UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[_redView]];
        
            //3.指定碰撞边界
            collision.translatesReferenceBoundsIntoBoundary = YES;
            //4.添加指定行为到仿真器
            [_animator addBehavior:gravity];
            [_animator addBehavior:collision];

        各种Behavior中的属性:

        • 1.UISnapBehavior:吸附行为

        @property (nonatomic, assign) CGPoint snapPoint NS_AVAILABLE_IOS(9_0);//吸附点
        @property (nonatomic, assign) CGFloat damping;//剧烈程度
        
        • 2.UIPushBehavior:推动行为

        - (instancetype)initWithItems:(NSArray<id <UIDynamicItem>> *)items mode:(UIPushBehaviorMode)mode//初始化时指定推动的模式,分为单次推和持续推
        @property (nonatomic, readwrite) BOOL active;//单次推的模式的时候需要打开
        @property (readwrite, nonatomic) CGFloat angle;//角度
        @property (readwrite, nonatomic) CGFloat magnitude;//力度
        @property (readwrite, nonatomic) CGVector pushDirection;
        
        • 3.UIAttachmentBehavior:刚性附着行为

        - (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(UIOffset)offset attachedToAnchor:(CGPoint)point //offset:偏移的距离
        
        @property (readwrite, nonatomic) CGPoint anchorPoint;//附着点
        @property (readwrite, nonatomic) CGFloat length;
        @property (readwrite, nonatomic) CGFloat damping; // 1: critical damping  振幅
        @property (readwrite, nonatomic) CGFloat frequency; // in Hertz  频率
        @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
        


        • 4.UICollisionBehavior:碰撞行为

        @property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode; //碰撞模式
        @property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;//是否开启边界碰撞检测
        @property (nullable, nonatomic, weak, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;// 有自己的代理方法.分别有开始撞击和停止撞击时调用的代理方法
        • 5.UIGravityBehavior:重力检测

        @property (readwrite, nonatomic) CGVector gravityDirection;//重力方向
        @property (readwrite, nonatomic) CGFloat angle;//角度
        @property (readwrite, nonatomic) CGFloat magnitude;//力度



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5391次
    • 积分:380
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论