我想做的是:
- 单击一个按钮进行垂直翻转。
- 再次单击一个按钮,图层将其翻转回来,依此类推...
代码如下:
@interface ViewController (){
CALayer *plane;
}
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
[self addALayer];
}
- (void)addALayer{
plane = [CALayer layer];
plane.backgroundColor = [[UIColor orangeColor] CGColor];
//[plane insertSublayer:normalBackground atIndex:0];
plane.opacity = 1;
plane.frame = CGRectMake(0, 0, 300, 100);
plane.position = CGPointMake(250, 150);
plane.anchorPoint = CGPointMake(0.5, 0.5);
plane.borderColor = [UIColor whiteColor].CGColor;
plane.borderWidth = 3;
plane.cornerRadius = 10;
[self.view.layer addSublayer:plane];
}
- (IBAction)click:(id)sender {
BOOL isClicked = ((UIButton*)sender).selected;
((UIButton*)sender).selected = !((UIButton*)sender).selected;
CATransform3D transfrom = CATransform3DIdentity;
transfrom.m34 = -1.0/ 500;
if ( !isClicked )
transfrom = CATransform3DRotate(transfrom, degToRad(180.0), 1, 0, 0);
else
transfrom = CATransform3DRotate(transfrom, degToRad(-180.0), 1, 0, 0);
plane.transform = transfrom;
}
但是,在第 1 步,我可以看到图层翻转了 180 度,当我再次单击该按钮时,没有任何反应。
我在中间错过了什么吗?请帮忙。