动画增长/减小尺寸imageView iOS

时间:2013-10-08 12:10:23

标签: ios uiimageview cgaffinetransform cgaffinetransformscale

我正在尝试使用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的比例。

请帮助!!

2 个答案:

答案 0 :(得分:28)

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

[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最初减小尺寸,当动画结束时,它将通过动画恢复到原始尺寸。

答案 1 :(得分:12)

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)
    })
})