zoukankan      html  css  js  c++  java
  • iOS-UIDynamic

    /**
     UIDynamic是苹果在iOS7新推出的,在UIKit中能够做物理仿真动画的技术!
     
     一. 使用步骤:
     
     1. 实例化仿真者,并制定参照系视图
     */
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
    
    /**
     2. 指定动画仿真行为,共有
     
        1> 吸附
        UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.box snapToPoint:location];
     
        2> 推
        [[UIPushBehavior alloc] initWithItems:@[self.box] mode:UIPushBehaviorModeInstantaneous];
     
        推行为可以分为单次推,和持续推两种动作,如果是单次推,需要:_push.active = YES;
         // 推力的角度,方向
         _push.angle = angle;
         // 力量的大小
         _push.magnitude = distance / 10.0;
         
         // 对于单次推动,一定要设置成YES,才能够发力
         _push.active = YES;
     
     
        3> 附着、附加行为
        UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.box offsetFromCenter:offset attachedToAnchor:anchor];
     
        包括:刚性附加行为和弹性附加行为,指定以下两个属性即可实现弹性附加行为:
         // 振幅数值越小,震动越大
         self.attachment.damping = 0.1;
         self.attachment.frequency = 1.0f;
     
        4> 重力行为
        UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[blueView]];
     
        默认是向下运动,从屏幕顶部运动出屏幕大概需要1s的时间
     
        5> 碰撞行为
        UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[blueView]];
        // 启用碰撞边界
        collision.translatesReferenceBoundsIntoBoundary = YES;
     
        如果存在物体需要碰撞,又不能移动的(墙壁,挡板),可以使用边界来实现:
        [collision addBoundaryWithIdentifier:@"lalala" fromPoint:redView.frame.origin toPoint:CGPointMake(toX, toY)];
        
        通过代理方法:
        - (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p
     
        可以做碰撞后的处理,例如小星星!
     
        6> 物体属性行为,可以指定参与仿真物体的物理参数,例如:密度,弹性系数,摩擦系数等,通常不用管
     UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[blueView]];
     
        ======================================================================
        应用场景:
     
        在应用程序开发中,
        吸附行为 => 按钮出现的效果,譬如新浪微博写微博的6个按钮
        重力行为 => 在锁屏时,上拉相机,不到位时松手
        附加行为 => iPhone的短信清单,在快速滑动松手时,会有一个所有短信挤到一起,然后再逐渐张开的效果
     */
  • 相关阅读:
    如何学习自动化测试?
    Jenkins中,执行py文件,python找包的路径(找不到自定义包的问题解决)
    数据库的架构设计
    iOS密码框的实现方式
    UISearchController 的大坑
    <第三方>TGRefreshO按照QQ的刷新方式下拉刷新
    关于项目颜色和字体的宏定义
    <iOS 导航栏>第一节:导航栏透明方法实现代码
    <iOS小技巧>UIview指定设置控件圆角
    关于这次KPL春季决赛的感悟
  • 原文地址:https://www.cnblogs.com/DarbyCJ/p/3745608.html
Copyright © 2011-2022 走看看