简体   繁体   English

CGAffineTransform旋转UIButton

[英]CGAffineTransform to rotate UIButton

I have a UIButton. 我有一个UIButton。 On this button I apply a CGAffineTransform like this: 在此按钮上,我像这样应用CGAffineTransform

_mainButton.transform = CGAffineTransformMakeRotation((degrees-heading.trueHeading) * M_PI / 180);

In this way I can rotate all my buttons, but I would like to rotate only the background image. 这样,我可以旋转所有按钮,但是我只想旋转背景图像。

So, myButton.imageView should remain at its fixed location. 因此, myButton.imageView应该保留在其固定位置。 How can make this? 如何做到这一点?

You have to rotate the imageView property of the UIButton like this: 您必须像这样旋转UIButtonimageView属性:

_mainButton.imageView.transform = CGAffineTransformMakeRotation((degrees-heading.trueHeading) * M_PI / 180);

imageView The button's image view. imageView按钮的图像视图。 (read-only) (只读)

@property(nonatomic, readonly, retain) UIImageView *imageView

Discussion 讨论

Although this property is read-only, its own properties are read/write. 尽管此属性是只读的,但它自己的属性是读/写。 Use these properties to configure the appearance and behavior of the button's view. 使用这些属性可以配置按钮视图的外观和行为。 For example: 例如:

UIButton *button                   = [UIButton buttonWithType: UIButtonTypeSystem];
button.imageView.exclusiveTouch    = YES;

The imageView property returns a value even if the button has not been displayed yet. 即使尚未显示按钮,imageView属性也会返回一个值。 The value of the property is nil for system buttons. 对于系统按钮,该属性的值为nil。

It's directly from Apple's documentation 它直接来自Apple的文档

Try this, works well: 试试这个,效果很好:

- (IBAction)toggle:(UIButton *)sender {
[UIView animateWithDuration:0.2 animations:^{
    if (CGAffineTransformEqualToTransform(sender.transform, CGAffineTransformIdentity)) {
        sender.transform = CGAffineTransformMakeRotation(M_PI * 0.999);
    } else {
        sender.transform = CGAffineTransformIdentity;
    }
}];

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM