CGAffineTransformScale是一个UIButton

时间:2012-05-04 02:31:00

标签: iphone xcode animation uiview uibutton

我试图在主视图中缩小和缩小UIButton,当我首先按下一个动作按钮时,按钮放大,当我再次按它时它会缩小但是当我再次按下它来放大时。没有任何反应..这是我的代码:

放大和缩小的方法属于目标C类别

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

谢谢, 牛顿

1 个答案:

答案 0 :(得分:4)

Newton,当removeSubviewWithZoomOutAnimation结束时view.transform是一个仿射变换,将视图的原始大小缩小到0.01。问题是当你第二次调用addSubviewWithZoomInAnimation时再次缩小0.01,但现在view.transform将缩小到0.0001,这不是你想要的。 / p>

只需在两个动画的开头添加view.transform = CGAffineTransformIdentity;,如下所示:

- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {
    view.transform = CGAffineTransformIdentity; 
    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 {
    view.transform = CGAffineTransformIdentity;
    // 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];
                     }];    
}

我还建议您传递UIViewAnimationOptionBeginFromCurrentState UIViewAnimationOptions,以便在快速放大和缩小时改善动画效果。

希望这有帮助!