所以,我有一个UIButton(由图像组成)我在点击时应用CGAffineTransform以突出显示它所处的状态:
- (void)animateMasterAddButton
{
CGAffineTransform buttonTransform;
// Button state hasn't been changed at this point, so selected == NO applies to when the button is *about* to be selected
if(self.masterAddButton.selected == NO)
{
CGAffineTransform buttonRotationTransform = CGAffineTransformMakeRotation((135.0 * M_PI) / 180);
buttonTransform = CGAffineTransformConcat(CGAffineTransformIdentity, buttonRotationTransform);
}
else
{
buttonTransform = CGAffineTransformIdentity;
}
[UIView animateWithDuration: 0.3
animations: ^{
self.masterAddButton.transform = buttonTransform;
}];
}
如果我将设备保持在相同的方向,这样可以正常工作。但是,一旦我转动设备,UIButton将消失,但仅当选择了UIButton 时(即,仅当旋转变换生效时)。我已经记录了按钮的框架并在视图控制器的willRotateToInterfaceOrientation:duration:
方法中进行了界限,结果如下:
使用未选择的按钮旋转设备:
框架:{{8,8},{57,57}}
界限:{{0,0},{57,57}}(这些是正确的值)
选择按钮旋转设备:
框架:{{-4,-4},{80,80}}
界限:{{0,0},{0,113.137}}
在第二组结果中,框架是正确的,因为旋转的图像有其角落,因此会占用更多空间。然而,界限显示出一些棘手的问题。我猜测,当边界宽度为零时,按钮就在那里但是太窄而无法渲染。
任何人都可以告诉我为什么会这样,以及我如何解决它?
答案 0 :(得分:0)
钉着它,带着一些侧面思考。基本上,一旦旋转动画完成,变换的按钮图像将被旋转与动画所应用的相同量的版本替换(我编写了一个UIImage类别方法来执行此过程)。然后使用CGAffineTransformIdentity
禁用按钮上的变换。由于旋转设备时按钮上不存在变换,因此保持放置: - )
以下是代码:
- (void)animateMasterAddButton
{
CGFloat addButtonRotationAngle = AddButtonRotationAngle * -1.0; // AddButtonRotationAngle is a const float defined at the top of .m file
CGAffineTransform buttonTransform;
UIImage *rotatedAddButtonImage = [UIImage imageNamed: @"Add button"];
if(self.masterAddButton.selected == NO)
{
addButtonRotationAngle = AddButtonRotationAngle;
rotatedAddButtonImage = [UIImage rotatedImageFromImage: rotatedAddButtonImage withAngle: (addButtonRotationAngle * M_PI) / 180];
// rotatedImageFromImage:withAngle: is a self-written category method on UIImage
}
CGAffineTransform buttonRotationTransform = CGAffineTransformMakeRotation((addButtonRotationAngle * M_PI) / 180);
buttonTransform = CGAffineTransformConcat(CGAffineTransformIdentity, buttonRotationTransform);
[UIView animateWithDuration: 0.3
animations: ^{
self.masterAddButton.transform = buttonTransform;
}
completion: ^(BOOL finished) {
[self.masterAddButton setImage: rotatedAddButtonImage forState: UIControlStateNormal];
self.masterAddButton.transform = CGAffineTransformIdentity;
}];
}