简体   繁体   中英

Rotate UIButton and preserve its size

I am trying to rotate buttons, that are shaped as a square (65 x 65). After I rotate them, the buttons change their sizes. Is there any way, to make a rotation and keep the size?

Code:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

self.stealthButton.transform = CGAffineTransformMakeRotation(radians);
self.recButton.transform = CGAffineTransformMakeRotation(radians);
self.toggleButton.transform = CGAffineTransformMakeRotation(radians);
[UIView commitAnimations];

try like this may be it helps you ,

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 1;
    fullRotation.repeatCount = 1;
    [button.layer addAnimation:fullRotation forKey:@"360"];

Add below framework in your project.

#import <QuartzCore/QuartzCore.h>
#import "QuartzCore/CALayer.h"

They don't change their size. They change the frame's size.

Add:

CGFrame oldFrame = self.stealthButton.frame;
//Do that for all the buttons
//Do your rotation
self.stealthButton.frame = oldFrame;

BUT: By doing so you will actually shrink the button's size. From the user's point of view it will appear much smaller.

Make sure that you have the content view set to center.

self.stealthButton.contentMode = UIViewContentModeCenter;
self.recButton.contentMode = UIViewContentModeCenter;
self.toggleButton.contentMode = UIViewContentModeCenter;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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