UIButton 如何用动画从左右两边缩小宽度?

How to reduce the width of UIButton from both left and right with animation?

我试图通过使用以下代码从左侧和右侧减少 UIButton 的宽度。但它只是缩小了 UIButton 的大小。代码是

    -(void)loginclickAction:(id)sender
{
        self.centerZoom = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        self.centerZoom.duration = 1.5f;
        self.centerZoom.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(.9, .9, .9)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(.8,.8, .8)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(.7, .7, .7)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(.6, .6, .6)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(.5, .5,.5)]];
        self.centerZoom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        self.loginButton.transform = CGAffineTransformMakeScale(.5,.5);
         self.loginButton.alpha = 1;
        [self.loginButton.layer addAnimation:self.centerZoom forKey:@"buttonScale"];
        [self.loginButton setUserInteractionEnabled:NO];

}

但是输出正在缩小我不想要的 UIButton。请帮我找到减少动画按钮宽度的方法。

您需要一种方法来仅在整个宽度上缩放按钮。 使用下面的方法,通过动画将按钮从全宽水平缩放到一半-

-(void) reduceButtonWidthFromLeftAndRightWithAniamation:(UIButton *)button
{
    [UIView beginAnimations:@"ScaleButton" context:NULL];
    [UIView setAnimationDuration: 1.5f];
    button.transform = CGAffineTransformMakeScale(0.5, 1.0);
    [UIView commitAnimations];
}

您的转换都影响多个维度:

CGAffineTransformMakeScale(.5, .5);

这是缩放宽度 (x) 和高度 (y),仅缩放宽度:

CGAffineTransformMakeScale(.5, 1);

同样适用于

CATransform3DMakeScale(.9, .9, .9)

它正在改变 x、y 和 z 维度,应该是

CATransform3DMakeScale(.9, 1, 1)