动画调整UIButton的大小

时间:2012-11-27 00:27:52

标签: iphone objective-c ios xcode uibutton

我在调整按钮宽度的大小时会遇到麻烦。这是我的代码:

- (IBAction)profileButtonPressed:(UIButton *)sender {
    UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    [sender setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];

    UIImage *buttonProfileDefaultDown = [[UIImage imageNamed:@"Profile_Button_Default_Down"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    [sender setBackgroundImage:buttonProfileDefaultDown forState:UIControlStateHighlighted];

    [sender setTitle:@"LOG OUT" forState:UIControlStateNormal];

    sender.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    sender.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    // Animate
    CGRect originalFrame = sender.frame;
    originalFrame.size.width = sender.frame.size.width+60;
    [UIView animateWithDuration:1.5 animations:^{
        sender.frame = originalFrame;
    }];
}

它改变了按钮的宽度(除非我拿走了改变标题的代码片段),但它没有通过动画来实现它。

有什么建议可能出错吗?

更新

所以我设法设置宽度变化的动画,但是将setTitle放在animate部分中,但它没有为文本本身获得正确的宽度(下图)。我怎样才能做到这一点?

enter image description here

解决方案

所以我最终以编程方式放置了uibutton。似乎自动布局导致了问题。

仅仅是我还是在Objective C中有很多笨拙的东西?!

4 个答案:

答案 0 :(得分:2)

我怀疑你的问题与内在内容大小有关。如果设置了它(我认为它是默认的),那么当你更改标题时,按钮将改变大小,没有动画,没有你在代码中做任何事情。有几种方法可以解决这个问题。我认为最简单的方法是将IB中按钮的大小扩展一点,即使一个像素应该足以让IB设置一个明确的宽度(或者你可以自己设置)。然后在代码中,您可以这样做:

-(IBAction)buttonClick:(UIButton *)sender {
    [sender setTitle:@"Log Out Immediately" forState:UIControlStateNormal];
    [self.button removeConstraint:self.lengthConstraint];
    [UIView animateWithDuration:.5 animations:^{
        self.button.frame = CGRectMake(self.button.frame.origin.x, self.button.frame.origin.y, 250, 44);
    }];
}

self.lengthConstraint是IB中设置的长度约束的IBOutlet。

另一种方法是使用NSTimer更改约束方法中的常量。这是WWDC 2012演讲“掌握自动布局的最佳实践”中提到的方法之一:

-(IBAction)buttonClick:(UIButton *)sender {
    [sender setTitle:@"Log Out Immediately" forState:UIControlStateNormal];
    [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(expandButton:) userInfo:nil repeats:YES];
}

-(void)expandButton:(NSTimer *) timer {
    self.lengthConstraint.constant += 2;
    if (self.lengthConstraint.constant > 170) [timer invalidate];
}

答案 1 :(得分:0)

在制作动画之前删除setTitle并在完成块中执行此操作。

    CGRect originalFrame = sender.frame;
    originalFrame.size.width = sender.frame.size.width+60;

    [UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        sender.frame = originalFrame;
    } completion:^{
        [sender setTitle:@"LOG OUT" forState:UIControlStateNormal];
    }];

<强>更新 检查您是否使用自动布局功能。我不确定,但这应该是问题。当我以编程方式创建按钮时,我在评论中提到这对我有用。

答案 2 :(得分:0)

解决方案

所以我最终以编程方式放置了uibutton。似乎自动布局导致了问题。

仅仅是我还是在Objective C中有很多笨拙的东西?!

答案 3 :(得分:0)

一旦开始使用自动布局,您应该停止修改帧,而是修改视图的约束值。

您可以在按钮的宽度约束中添加IBOutlet并修改其constant值。

相关问题