Question

I create a UIView and set it's layer's contents a image. And I added a tap gesture to this view. When I tap the view, I want it to rotate 180 degree, and when I click close button, I want it to rotate to it's original degree. But it won't work when I try to rotate it back.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"menuArray"];
    self.menuView.layer.contents = (__bridge id)image.CGImage;

    self.menuView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.menuView addGestureRecognizer:tap];
}
//rotate it 180 degree
- (void)tap
{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseIn animations:^{
        self.menuView.layer.affineTransform = CGAffineTransformMakeRotation(M_PI);
    } completion:^(BOOL finished) {

    }];
}
//rotate it back. this won't work. I have change the value from M_PI to M_PI_2, M_PI_4. it's not working.
- (IBAction)close:(id)sender
{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseIn animations:^{
        self.menuView.layer.affineTransform = CGAffineTransformMakeRotation(-M_PI);
    } completion:^(BOOL finished) {

    }];
}
Was it helpful?

Solution

To undo an affineTransorm you should set it to the constant affineTransformIdentity , which is tranform of nil so to speak, a matrix of all zeros..

So in your second method where you want to rotate back set self.menuView.layer.affineTransform = CGAffineTransformIdentity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top