关闭

UIPushBehavior-动画效果:推移行为

标签: UIPushBehaviorUIDynamicAnimator动画推移
3938人阅读 评论(0) 收藏 举报
分类:

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];
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:114879次
    • 积分:2223
    • 等级:
    • 排名:第17642名
    • 原创:15篇
    • 转载:0篇
    • 译文:99篇
    • 评论:3条
    最新评论