i am trying to scale in and out a UIButton in the main view, when i press a action button at first, the button zooms in and when i press it again it zooms out but when i press it again to zoom in.. nothing happens.. here is my code:
THE METHODS TO ZOOM IN AND ZOOM OUT ARE IN A OBJECTIVE-C CATEGORY ON UIVIEW
- (void)viewDidLoad
[super viewDidLoad];
//this button is being added in the storyboard
[self.viewToZoom removeFromSuperview];
}
- (IBAction)zoomButton:(id)sender {
if (isShown) {
[self.view removeSubviewWithZoomOutAnimation:self.viewToZoom duration:1.0 option:0];
isShown = NO;
} else {
[self.view addSubviewWithZoomInAnimation:self.viewToZoom duration:1.0 option:0];
isShown = YES;
}
}
UIView+Animation.m
- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {
CGAffineTransform trans = CGAffineTransformScale(view.transform, 0.01, 0.01);
view.transform = trans; // do it instantly, no animation
[self addSubview:view];
// now return the view to normal dimension, animating this tranformation
[UIView animateWithDuration:secs delay:0.0 options:option
animations:^{
view.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
}
completion:^(BOOL finished) {
NSLog(@"done");
} ];
}
- (void) removeSubviewWithZoomOutAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {
// now return the view to normal dimension, animating this tranformation
[UIView animateWithDuration:secs delay:0.0 options:option
animations:^{
view.transform = CGAffineTransformScale(view.transform, 0.01, 0.01);
}
completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
Thanks,
Newton
Newton, when
removeSubviewWithZoomOutAnimationends theview.transformis an affine transform that scaled the view’s original size down to 0.01. The problem is that when you calladdSubviewWithZoomInAnimationthe second time you’re again scaling down by 0.01, but now theview.transformwill be scaled down to 0.0001, which is not what you’re looking for.Simply add
view.transform = CGAffineTransformIdentity;at the beginning of both animations like this:I also suggest you to pass the
UIViewAnimationOptionBeginFromCurrentStateUIViewAnimationOptions which improves the animation result when quickly zooming in and out.Hope this helps!