How to rotate UIView by 45 degrees?

I want to turn my UIButtonon M_PI/4using animation. Here is my code:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:.5 animations:^{
        self.closeButton.transform = CGAffineTransformMakeRotation(M_PI/4);
    }];
}

But this will damage my button. There was a frame before the animation (260 0; 44 44), and after the animation it became (250.887 -9.1127; 62.2254 62.2254). I saw this post and several others, but I don’t understand how to achieve the animation rotation of UIButton on M_PI/2.

+4
source share
2 answers

The property is not valid after applying the transform. According to the documentation: frame

: , undefined .

, , .

, , reset it CGAffineTransformIdentity, , . center , .

+3

frame UIView - , . PI/2, sqrt (2), , , .

, , ,

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    CGFloat w = self.closeButton.bounds.size.width  / sqrtf( 2.0f );
    CGFloat h = self.closeButton.bounds.size.height / sqrtf( 2.0f );
    NSLog( @"%@ w=%f", NSStringFromCGRect( self.closeButton.bounds ), w );

    [UIView animateWithDuration:.5 animations:^{
        self.closeButton.transform = CGAffineTransformMakeRotation(M_PI/4);
        self.closeButton.bounds = CGRectMake( 0, 0, w, h );
    }];

    NSLog( @"%@", NSStringFromCGRect( self.closeButton.frame ) );
}
+2

Source: https://habr.com/ru/post/1532513/


All Articles