UIPushBehavior-动画效果:推移行为

1。从一点移到另一点

2。相关属性

       mode : UIPushBehaviorModeContinuous  //推移模式

       angle : setAngle  //推移角度

       magnitude : setMagnitude  //速度   每1个magnigude将会引起100/平方秒的加速度   


e.g. 

  

p1 : squareView的中心点  p2 : 单击的点  

促使squareView朝着p2移动,因为加了UICollisionBehavior,所以移动时又不会超过边界


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self createGestureRecognizer];
    [self createSmallSquareView];
    [self createAnimatorAndBehaviors];
}

- (void) createGestureRecognizer{  //侦测视图单击
    UITapGestureRecognizer *tapGestureRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTap:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void) handleTap:(UITapGestureRecognizer *)paramTap{

    CGPoint tapPoint = [paramTap locationInView:self.view];  //p2
    CGPoint squareViewCenterPoint = self.squareView.center;  //p1

    CGFloat deltaX = tapPoint.x - squareViewCenterPoint.x;
    CGFloat deltaY = tapPoint.y - squareViewCenterPoint.y;
    CGFloat angle = atan2(deltaY, deltaX);  
    [self.pushBehavior setAngle:angle];  //推移的角度
   

    //勾股
    CGFloat distanceBetweenPoints =
    sqrt(pow(tapPoint.x - squareViewCenterPoint.x, 2.0) +
         pow(tapPoint.y - squareViewCenterPoint.y, 2.0));
    //double pow(double x, double y);计算以x为底数的y次幂
    //double sqrt (double);开平方
    
    [self.pushBehavior setMagnitude:distanceBetweenPoints / 200.0f]; //推力的大小(移动速度)
    //每1个magnigude将会引起100/平方秒的加速度,这里分母越大,速度越小
    
}

- (void) createSmallSquareView{
    self.squareView =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
    
    self.squareView.backgroundColor = [UIColor greenColor];
    self.squareView.center = self.view.center;
    
    [self.view addSubview:self.squareView];
}


- (void) createAnimatorAndBehaviors{
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    /* Create collision detection */
    UICollisionBehavior *collision = [[UICollisionBehavior alloc]
                                      initWithItems:@[self.squareView]];
    collision.translatesReferenceBoundsIntoBoundary = YES;
    
    self.pushBehavior = [[UIPushBehavior alloc]
                         initWithItems:@[self.squareView]
                         mode:UIPushBehaviorModeContinuous];
    
    [self.animator addBehavior:collision];
    [self.animator addBehavior:self.pushBehavior];
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值