20

我正在尝试使用 CGAffineTransformMakeScale 为自定义按钮设置动画,如下所示:

if (stateButton == 0) { //The button is gonna appear

    self.selected = YES;

    self.imageView.transform = CGAffineTransformMakeScale(0.01, 0.01);

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        // animate it to the identity transform (100% scale)
        self.imageView.transform = CGAffineTransformIdentity;
    } completion:nil];

}
else if (stateButton ==1) { //The button is gonna disappear


    self.imageView.transform = CGAffineTransformMakeScale(1, 1);

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        // decrease button
        self.imageView.transform = CGAffineTransformMakeScale(.01, .01);
    } completion:^(BOOL finished){

        self.selected = NO;
    }];
}   

按钮完美地增长到原始大小,但是,我不知道原因,但是当我单击按钮减小它时,它会从比原始大小大 100% 的大小减小到原始大小,而不是开始正如我在代码中指出的那样,原始大小的减小并达到 0.01 的比例。

请帮忙!!

4

2 回答 2

33

您可以使用以下代码为图像视图的大小设置动画

[UIView animateWithDuration:2.0 animations:^{
    self.imageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
} 
completion:^(BOOL finished){
    [UIView animateWithDuration:2.0 animations:^{
        self.imageView.transform = CGAffineTransformMakeScale(1, 1);    
    }];
}];

这将使 imageview 最初的大小减小,当动画结束时,它将通过动画恢复到原始大小。

于 2013-10-08T13:29:51.900 回答
19

SWIFT 3 版本

UIView.animate(withDuration: 2.0, animations: {() -> Void in
    self.imageView?.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
}, completion: {(_ finished: Bool) -> Void in
    UIView.animate(withDuration: 2.0, animations: {() -> Void in
        self.imageView?.transform = CGAffineTransform(scaleX: 1, y: 1)
    })
})
于 2017-02-28T07:52:00.383 回答