我有一个奇怪的问题。我有一个自定义背景的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];
}];
正如您所看到的,我从一个非常小的按钮开始,并通过转换,随着时间的推移,我使按钮更大。我动态计算我的按钮的缩放因子是什么。
有人可以向我解释为什么按钮的标题不可见吗?
答案 0 :(得分:1)
尝试相反的方法:为初始状态设置转换,为最终状态设置“un-transform”:
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
出来以来,对我来说最好的结果一直是设置动画的最终状态,并使用逆变换作为开始状态。