首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGAffineTransformScale a UIButton

CGAffineTransformScale a UIButton
EN

Stack Overflow用户
提问于 2012-05-04 10:31:00
回答 1查看 3.2K关注 0票数 0

我试图在主视图中放大和缩小UIButton,当我第一次按下动作按钮时,该按钮会放大,当我再次按下它时,它会缩小,但当我再次按下它时,它会放大。什么都没发生..下面是我的代码:

放大和缩小的方法在UIVIEW上的OBJECTIVE-C类别中

代码语言:javascript
复制
- (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];
                 }];    
}

谢谢,牛顿

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-04 17:31:59

牛顿,当removeSubviewWithZoomOutAnimation结束时,view.transform是一个仿射变换,它将视图的原始大小缩小到0.01。问题是,当您第二次调用addSubviewWithZoomInAnimation时,您再次缩小了0.01%,但现在view.transform将缩小到0.0001,这并不是您想要的。

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

代码语言:javascript
复制
- (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,它可以在快速放大和缩小时改善动画效果。

希望这能有所帮助!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10441916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档