Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UIImageView disappears after rotation M_PI_4

What is basically happening here is that I'm adding a custom view to the UIBarButtonItem, and I need to rotate it by 45deg , the rotation works perfectly if rotated by 90deg or 180 , but when it's less that 90 the object gets deformed , and on 45deg the object disappears. Here are the snippets for the button and animation.

UIImageView * menuImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plus.png"]];
menuButton = [[UIBarButtonItem alloc] initWithCustomView:menuImage];
UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleMenuView:)];
[menuImage addGestureRecognizer:tap1];
[menuImage setUserInteractionEnabled:YES];
[menuImage.layer setShadowColor:[UIColor blackColor].CGColor];
[menuImage.layer setShadowOffset:CGSizeMake(ShadowSizeWidth, ShadowSizeHeight)];
[menuImage.layer setShadowOpacity:ShadowOpacity];
[menuImage.layer setShadowRadius:ShadowRadius];

[self.navigationItem setRightBarButtonItem:menuButton];

The Rotation:

[UIView animateWithDuration:animationRotateButtonDuration delay:0.0f options:UIViewAnimationCurveLinear animations:^{
    CGAffineTransform myTransform = CGAffineTransformMakeRotation(-M_PI_4);

    UIBarButtonItem * currentItem =  menuButton;

    currentItem.customView.transform = myTransform;

}completion:^(BOOL finished){
}];
like image 303
Noob Avatar asked Dec 27 '12 09:12

Noob


2 Answers

Use anchorPoint and import "QuartzCore/QuartzCore.h"

currentItem.customView.layer.anchorPoint = CGPointMake(1.0, 0.0);//it is not fixed point you have to change.

I think it will be helpful to you.

like image 160
Prasad G Avatar answered Oct 02 '22 06:10

Prasad G


If the UIImageView contentMode is set to UIViewContentModeScaleAspectFit or other scaled aspect, the rotation will cause the image to disappear.

Changing the contentMode to UIViewContentModeCenter should fix the problem.

like image 27
aepryus Avatar answered Oct 02 '22 05:10

aepryus