使用cgaffinetransformmakescale和cgaffinetransformmakerotation会改变我的观点

时间:2016-07-29 07:33:43

标签: objective-c cgaffinetransform

我有一个简单的UIView:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
self.someView = view;

然后我这样做

[UIView animateWithDuration:10
                      delay:1
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
    self.someView.center = CGPointMake(CGRectGetWidth(self.view.bounds) - CGRectGetWidth(self.someView.frame) / 2, 150);

                     CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
                     self.someView.transform = CGAffineTransformScale(transform, 2, 0.5);

}
                 completion:^(BOOL finished) {

                 }];

执行此简单动画时,someView会变形。据我所知,它应该旋转并改变它的大小。我无法理解为什么会这样,我该如何解决?

谢谢

1 个答案:

答案 0 :(得分:1)

问题是动画组合缩放和旋转。最终结果与单独设置动画相同,但两者之间的帧不容易定义。所以动画框架会猜测,而不是你期望的那样。

如果希望动画分别显示旋转和缩放,则需要将它们分别应用于视图层次结构。例如:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
self.someView = view;

UIView *innerView = [[UIView alloc] initWithFrame:view.bounds];
innerView.backgroundColor = [UIColor blueColor];
[view addSubview:innerView];
self.someInnerView = innerView;

动画分割变换:

[UIView animateWithDuration:10
                      delay:1
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.someView.center = CGPointMake(CGRectGetWidth(self.view.bounds) - CGRectGetWidth(self.someView.frame) / 2, 150);

                     self.someView.transform = CGAffineTransformMakeRotation(M_PI);
                     self.someInnerView.transform = CGAffineTransformMakeScale(2, 0.5);

                 }
                 completion:^(BOOL finished) {

                 }];