在不明确定义CGRect的情况下缩小80%的帧

Bre*_*ler 1 core-animation objective-c ios

我正在尝试为按钮设置动画.按钮将以0的高度和宽度开始,并扩展到其预期大小的100%.然后它将缩小到其尺寸的80%,然后再次回升到100%.

有没有办法做到这一点,而无需为每个位置明确计算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!");
                     }
                      ];
                 }];
Run Code Online (Sandbox Code Playgroud)

编辑:我意识到我可以写一个函数来计算80%的帧,但我想知道是否有更好的方法来做这个不需要我写任何额外的东西.

Jon*_*pan 5

看起来你只是想暂时操纵按钮的外观.如果是这样的话,不要改变它frame,试着改变它transform!这是一个起点.如果您需要更多了解,请告诉我.

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

要将按钮恢复为其原始转换(称为"标识转换"),请将其transform属性设置为CGAffineTransformIdentity.

一旦你弄明白了,就读一下CGAffineTransform(Quartz表兄弟NSAffineTransform和2D大叔)CATransform3D.