使用动画修改 UIButton 宽度

发布于 2024-11-29 05:23:17 字数 422 浏览 0 评论 0原文

这是我的代码,

-(IBAction)casser{
     oeuf1.contentMode       =   UIViewContentModeScaleAspectFit;
    [UIView animateWithDuration:1.0 animations:^{ 
         CGRect newRect      =   oeuf1.frame;
         newRect.size.width *=   5;
         oeuf1.frame         =   newRect;
         [oeuf1 release];
    }];
}

“oeuf1”是一个 UIButton 当我触摸按钮时,我想将其宽度乘以五,但是当我这样做时,按钮会向右移动,并且其宽度不会改变。我该如何解决这个问题?

Here is my code ,

-(IBAction)casser{
     oeuf1.contentMode       =   UIViewContentModeScaleAspectFit;
    [UIView animateWithDuration:1.0 animations:^{ 
         CGRect newRect      =   oeuf1.frame;
         newRect.size.width *=   5;
         oeuf1.frame         =   newRect;
         [oeuf1 release];
    }];
}

"oeuf1" is a UIButton
I want to multiply its width by five when I touch the button, but when I do that the button moves to the right and its width doesn't change. How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戒ㄋ 2024-12-06 05:23:17

经过测试的代码 100% 有效

将每个方法与指定的方法连接起来

-(IBAction)expand{//touch down


    NSLog(@"expand");

    [UIView transitionWithView:but duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^(void) {


        CGRect newRect = but.frame;

        CGPoint ce= but.center;
        newRect.size.width *= 5;

        but.frame = newRect;    

        but.center=ce;



    } completion:^(BOOL finished) {



    }];


}

 -(IBAction)shrink{//touch up inside
    NSLog(@"shrink");


    [UIView transitionWithView:but duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^(void) {


        CGRect newRect = but.frame;

        CGPoint ce= but.center;
        newRect.size.width /= 5;

        but.frame = newRect;    

        but.center=ce;



    } completion:^(BOOL finished) {



    }];


}

Tested code 100% works

connect each method with specified

-(IBAction)expand{//touch down


    NSLog(@"expand");

    [UIView transitionWithView:but duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^(void) {


        CGRect newRect = but.frame;

        CGPoint ce= but.center;
        newRect.size.width *= 5;

        but.frame = newRect;    

        but.center=ce;



    } completion:^(BOOL finished) {



    }];


}

 -(IBAction)shrink{//touch up inside
    NSLog(@"shrink");


    [UIView transitionWithView:but duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^(void) {


        CGRect newRect = but.frame;

        CGPoint ce= but.center;
        newRect.size.width /= 5;

        but.frame = newRect;    

        but.center=ce;



    } completion:^(BOOL finished) {



    }];


}
黑色毁心梦 2024-12-06 05:23:17

有效答案:

删除 [oeuf1 release];

The working answer:

Remove [oeuf1 release];

七七 2024-12-06 05:23:17

你为什么不直接使用

[oeuf1 setFrame:CGRectMake(oeuf1.frame.origin.x, oeuf1.frame.origin.y, oeuf1.frame.size.width*2, oeuf1.frame.size.height)];

Why don't you just use

[oeuf1 setFrame:CGRectMake(oeuf1.frame.origin.x, oeuf1.frame.origin.y, oeuf1.frame.size.width*2, oeuf1.frame.size.height)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文