0

我有一个奇怪的问题。我有一个带有自定义背景和一个字符作为标题的 UIButton。我希望按钮通过动画变大。动画有效,但是,我再也看不到按钮的标题了。

这是我的代码:

UIButton *letterButton = [UIButton buttonWithType:UIButtonTypeCustom];
[letterButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
[letterButton setTitle:[gameManager letterTextForIndex:curIndex] forState:UIControlStateNormal];
letterButton.frame = CGRectMake(curX + kLetterButtonSize / 2, curY + kLetterButtonSize / 2, 0.01f, 0.01f);
[letterButton addTarget:self action:@selector(pressedLetterButton:) forControlEvents:UIControlEventTouchUpInside];

UIImage *buttonImage = [[UIImage imageNamed:@"blueButton.png"]
                                resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:@"blueButtonHighlight.png"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];

[letterButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[letterButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];


[self.view addSubview:letterButton];

[UIButton animateWithDuration:0.20f delay:curIndex * 0.17f options:UIViewAnimationOptionTransitionNone animations:^
{
     letterButton.transform = CGAffineTransformMakeScale(kLetterButtonSize / 0.01f, kLetterButtonSize / 0.01f);
}
completion:^(BOOL finished)
{
     [letterButton setTitle:[gameManager letterTextForIndex:curIndex] forState:UIControlStateNormal];
 }];

如您所见,我从一个非常小的按钮开始,然后进行转换,随着时间的推移,我使按钮变大。我动态计算我的按钮的比例因子是多少。

谁能向我解释为什么按钮的标题不可见?

4

1 回答 1

1

换一种方式试试:为初始状态设置转换,为最终状态“取消转换”:

UIButton *letterButton = [UIButton buttonWithType:UIButtonTypeCustom];
[letterButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
[letterButton setTitle:[gameManager letterTextForIndex:curIndex]
              forState:UIControlStateNormal];
letterButton.frame = CGRectMake(curX + kLetterButtonSize / 2, curY + kLetterButtonSize / 2, kLetterButtonSize, kLetterButtonSize);
letterButton.transform = CGAffineTransformMakeScale(0.01f/kLetterButtonSize, 0.01f/kLetterButtonSize);
[letterButton addTarget:self
                 action:@selector(pressedLetterButton:)
       forControlEvents:UIControlEventTouchUpInside];

 UIImage *buttonImage = [[UIImage imageNamed:@"blueButton.png"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
 UIImage *buttonImageHighlight = [[UIImage imageNamed:@"blueButtonHighlight.png"]
                                  resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];

[letterButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[letterButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];


[self.view addSubview:letterButton];

[UIButton animateWithDuration:0.20f delay:curIndex * 0.17f options:UIViewAnimationOptionTransitionNone animations:^
{
    letterButton.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
    [letterButton setTitle:[gameManager letterTextForIndex:curIndex] forState:UIControlStateNormal];
}];

它在我的测试中对我有用。

一般来说,自从CAAnimation问世以来,对我来说最好的结果一直是设置动画的最终状态,并使用逆变换作为开始状态。

于 2013-06-09T20:11:53.477 回答