2012-08-03 40 views
1

我试图为一个按钮设置动画。该按钮将以0和0的高度和宽度开始并扩展到其预期大小的100%。然后它会缩小到其尺寸的80%,然后再回到100%。在没有明确定义CGRect的情况下缩小一个框架80%

有没有办法做到这一点,而不必明确计算每个职位的CGRect?问题在于,在80%时,框架与100%框架的位置不完全对齐。我必须手动计算帧的x和y位置,这是很耗时间的,因为我正在为很多按钮执行此操作。

这里就是我目前正在做的事:

CGRect smallPlay = CGRectMake(PlayButton.frame.origin.x + 94, PlayButton.frame.origin.y + 23, 0, 0); 
    CGRect almostFullPlay = CGRectMake(40, 170, 160, 30); 

    [UIView animateWithDuration:0.3 
        delay:0 
        options: UIViewAnimationCurveEaseOut 
        animations:^{ 

        PlayButton.frame = smallPlay; 
        PlayButton.frame = CGRectMake(50, 178, 187, 45); 

       } 
       completion:^(BOOL finished){ 
        [UIView animateWithDuration:0.2 
         delay:0 
         options:UIViewAnimationCurveEaseOut 
         animations:^{ 

          PlayButton.frame = almostFullPlay; 
          PlayButton.frame = CGRectMake(50, 178,187, 45); 

        } completion:^(BOOL finished){ 
         NSLog(@"Done!"); 
        } 
         ]; 
       }]; 

编辑:我知道我可以写一个函数在80%来计算的框架,但我想知道如果有一个更好的办法这样做不需要我额外写任何东西。

回答

5

看起来你只是想暂时操纵按钮的外观。如果是这种情况,不要改变其frame,请尝试转换它的transform!这是一个起点。让我知道你是否需要更多的理解。

[UIView 
    animateWithDuration: ... 
    delay:    ... 
    options:    UIViewAnimationCurveEaseOut 
    animations:   ^{ 
     PlayButton.transform = CGAffineTransformMakeScale(0.8, 0.8); 
    }]; 

要恢复按钮到原来的变换(称为“身份转换”),其transform属性设置为CGAffineTransformIdentity

一旦你得到了想通了,对CGAffineTransform读了(石英表哥NSAffineTransform,以及CATransform3D二维大伯父。)

+0

非常感谢!这正是我所期待的。 – 2012-08-03 03:26:53

相关问题