首页 文章

动画调整UIButton的大小

提问于
浏览
2

我有一些麻烦动画调整按钮宽度的大小 . 这是我的代码:

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

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

有什么建议可能是错的吗?

Update

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

enter image description here

Solution

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

在Objective C中,它只是我还是很多笨拙的东西?!

4 回答

  • 0

    我怀疑你的问题与内在内容大小有关 . 如果设置了(我认为这是默认情况下),那么当你更改 Headers 时,按钮将改变大小,没有动画,没有你在代码中做任何事情 . 有几种方法可以解决这个问题 . 我认为最简单的方法是将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];
    }
    
  • 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];
        }];
    

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

  • 2

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

    在Objective C中,它只是我还是很多笨拙的东西?!

  • 0

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

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

相关问题