UIDynamic 入门基础

#import "ViewController.h"


@interface ViewController ()<UICollisionBehaviorDelegate>

{

    UIAttachmentBehavior *attachmentBehavior;

    UIView *iv;

    UIDynamicAnimator *animator;

    UIPanGestureRecognizer *panGestureRecognizer;

}

@end


@implementation ViewController

-(void)gesture:(UIPanGestureRecognizer *)gesture{

    CGPoint location = [gesture locationInView:self.view];

    CGPoint boxLocation = [gesture locationInView:iv];

    

    switch (gesture.state) {

        case UIGestureRecognizerStateBegan:{

            NSLog(@"you touch started position %@",NSStringFromCGPoint(location));

            NSLog(@"location in image started is %@",NSStringFromCGPoint(boxLocation));

            [animator removeAllBehaviors];

            UIOffset centerOffset = UIOffsetMake(boxLocation.x - CGRectGetMidX(iv.bounds),

                                                 boxLocation.y - CGRectGetMidY(iv.bounds));

            attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:iv

                                                           offsetFromCenter:centerOffset

                                                           attachedToAnchor:location];//初始化吸附到锚点

            

            attachmentBehavior.damping=0;  // 跳跃度

            attachmentBehavior.frequency=0;// 幅度

            

            [animator addBehavior:attachmentBehavior];

            break;

        }

        case UIGestureRecognizerStateEnded: {

            [animator removeBehavior:attachmentBehavior];

            

            break;

        }

        default:

            [attachmentBehavior setAnchorPoint:[gesture locationInView:self.view]];

            break;

    }

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //1.吸附行为(UIAttachmentBehavior)吸附的项有两个属性damping(阻尼)frequency震荡幅度。

    //2.重力行为(UIGravityBehavior)通过对象UIGravityBehavior给动力项指定一个重力矢量,具有重力矢量的动力项,会在重力矢量的方向上一直加速,直到与别的动力项产生了冲突或者遇到了边界。

    //3.碰撞行为 (UICollisionBehavior) 通过对象UICollisionBehavior指定一个边界,并且让各个动力项,在该边界内参与碰撞。

    //4.推动行为(UIPushBehavior)通过对象UIPushBehavior给动力项指定一个持续的或者瞬时的力

    //5.捕捉行为(UISnapBehavior)通过对象UISnapBehavior给动力项指定一个捕捉点。动力项会根据配置的效果,来抓住这一捕捉点。

    //6.辅助行为(UIDynamicItemBehavior)用来在item层级设定一些参数,比如item的摩擦,阻力弹性密度和可允许的旋转等。

    iv=[[UIView alloc]init];

    iv.frame=CGRectMake(100, 100, 100, 100);

    iv.backgroundColor=[UIColor redColor];

    [self.view addSubview:iv];

    animator= [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    //    - (void)addBehavior:(UIDynamicBehavior *)behavior;添加指定的行为动画

    //    - (void)removeBehavior:(UIDynamicBehavior *)behavior;移除指定的行为动画

    //    - (void)removeAllBehaviors;移除所有的行为

    

    //1.吸附行为(UIAttachmentBehavior

    [self AttachmentBehavior];

    //2.重力行为(UIGravityBehavior

//    [self GravityBehavior];

    //3.碰撞行为 (UICollisionBehavior)

//    [self CollisionBehavior];

    //4.推动行为(UIPushBehavior

//    [self PushBehavior];

    //5.捕捉行为(UISnapBehavior

//    [self SnapBehavior];

}

-(void)SnapBehavior{

    UISnapBehavior *snapbehavior =[[UISnapBehavior alloc]initWithItem:iv snapToPoint:self.view.center];

    snapbehavior.damping=1;//damping 振幅大小,默认为0.5f

    [animator addBehavior:snapbehavior];

}

-(void)PushBehavior{

    UIPushBehavior *push=[[UIPushBehavior alloc]initWithItems:@[iv] mode:UIPushBehaviorModeInstantaneous];

//    UIPushBehaviorModeContinuous,一直推动

//    UIPushBehaviorModeInstantaneous瞬间推动

    push.pushDirection = CGVectorMake(45, 45);//推移的方向(00)-》(4545

    push.magnitude=1;//每一个magnitude将会引起100/平方秒的加速度

    [animator addBehavior:push];

    UIDynamicItemBehavior *itemBehavior=[[UIDynamicItemBehavior alloc]initWithItems:@[iv]];

    itemBehavior.resistance=0.8;//添加阻力。

    [animator addBehavior:itemBehavior];

}

-(void)CollisionBehavior{

    UIGravityBehavior *gravityBehavior=[[UIGravityBehavior alloc]initWithItems:@[iv]];

    [animator addBehavior:gravityBehavior];

    UICollisionBehavior *collisionBehavior=[[UICollisionBehavior alloc]initWithItems:@[iv]];

    [collisionBehavior setTranslatesReferenceBoundsIntoBoundary:YES];

    [animator addBehavior:collisionBehavior];

    UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc]initWithItems:@[iv]];

//    @property (readwrite, nonatomic) CGFloat elasticity; // Usually between 0 (inelastic) and 1 (collide elastically) 属性设置碰撞弹性系数 范围(0.0-1.0)决定了碰撞的弹性程度,比如碰撞时物体的弹性

//    

//    @property (readwrite, nonatomic) CGFloat friction; // 0 being no friction between objects slide along each other设置摩擦系数 决定了沿接触面滑动时的摩擦力大小

//    

//    @property (readwrite, nonatomic) CGFloat density; // 1 by default 密度   size相关 计算物体的总质量 质量越大 物体加速或减速就越困难

//    

//    @property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping (阻力):决定线性移动的阻力大小 与摩擦系数不同 摩擦系数只作用于滑动运动

//    

//    @property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular velocity damping 设置角度阻力系数。(0--CGFLOAT_MAX)决定旋转运动时的阻力大小

//    

//    @property (readwrite, nonatomic) BOOL allowsRotation; // force an item to never rotate 设置行为中的dynamic item是否可以旋转  设置这个属性为 NO 物体就完全不会转动,而无论施加多大的转动力

    [itemBehavior setElasticity:1];

//    [itemBehavior setResistance:1];

    [animator addBehavior:itemBehavior];

    [collisionBehavior setCollisionDelegate:self];

}


- (void)collisionBehavior:(UICollisionBehavior *)behavior

      endedContactForItem:(id <UIDynamicItem>)item

   withBoundaryIdentifier:(id <NSCopying>)identifier{

    NSLog(@"=======");

}

-(void)GravityBehavior{

    UIGravityBehavior *gravityBehaviour=[[UIGravityBehavior alloc]initWithItems:@[iv]];

    [animator addBehavior:gravityBehaviour];

}

-(void)AttachmentBehavior{


    panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];

    [iv addGestureRecognizer:panGestureRecognizer];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值