Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation Grow/Decrease Size imageView iOS

I'm trying to animate a custom button using CGAffineTransformMakeScale as follows:

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;
    }];
}   

The button grows perfectly to the original size, however, i don't know the reason but when i click on the button to decrease it, it decreases from a size like 100% bigger than the original size to the original size, instead of beginning the decrease in the original size and achieve an scale of 0.01 as I indicated in the code.

Please help!!

like image 253
user1708257 Avatar asked Oct 08 '13 12:10

user1708257


2 Answers

You can animate the grow and decrease size of the image view using the following code

[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);    
    }];
}];

This will make the imageview decrease in size initially and when animation is over it will get back to its original size with an animation.

like image 55
Geekoder Avatar answered Nov 04 '22 15:11

Geekoder


SWIFT 3 Version

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)
    })
})
like image 39
Oscar Falmer Avatar answered Nov 04 '22 13:11

Oscar Falmer