我试图在用户按下按钮时为我的 UIButton 提供新的背景图像,然后设置宽度动画。问题是按钮不缩放图像(我只使用视网膜图像)。
点击前:
点击后:
以及让事情发生的代码:
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
[self.profileButton removeConstraint:self.profileButtonConstraint];
// Animate
CGRect originalFrame = self.profileButton.frame;
originalFrame.size.width = 40;
[UIView animateWithDuration:0.2f
animations:^{
self.profileButton.frame = originalFrame;
}
completion:^(BOOL finished) {
}];
最佳答案
试试这个代码...
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[self.profileButton setBackgroundImage:[UIImage imageNamed:@"Profile_Button_Default"] forState:UIControlStateNormal];
[self.profileButton setBackgroundImage:[UIImage imageNamed:@"Profile_Button_Default"] forState:UIControlStateSelected];
[self.profileButton setFrame:CGRectMake(self.profileButton.frame.origin.x, self.profileButton.frame.origin.y, self.profileButton.frame.size.width + 40, self.profileButton.frame.size.height)];
[UIView commitAnimations];
希望对你有帮助...
关于iphone - UIButton 上的 backgroundImage 缩放错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617286/