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){
}];
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With