CGAffineTransformMakeRotation再次无效

时间:2014-03-08 09:58:55

标签: ios objective-c cgaffinetransform

我创建了一个UIView并将它的图层内容设置为图像。我在这个视图中添加了一个轻击手势。当我点击视图时,我希望它旋转180度,当我点击关闭按钮时,我希望它旋转到它的原始程度。但是当我试图将其旋转回来时,它将不起作用。

- (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) {

    }];
}

1 个答案:

答案 0 :(得分:4)

要撤消affineTransorm,你应该将它设置为常量affineTransformIdentity,这是nil的变换,可以这么说,是一个全零的矩阵..

因此,在第二种方法中,您想要向后旋转设置self.menuView.layer.affineTransform = CGAffineTransformIdentity