【问题标题】:Can't undo a scale transform without undoing a rotation transform在不撤消旋转变换的情况下无法撤消缩放变换
【发布时间】:2011-04-28 16:46:56
【问题描述】:

这是对上一个问题的跟进。我下面的代码通过缩放和旋转正方形来为正方形设置动画。它通过进行旋转变换并为其添加比例变换来实现这一点。这很好用。完成后,它会调用throbReset。我曾经让throbReset 只是将self's transform 设置为CGAffineTransformMakeScale,这样会取消缩放,但也会取消旋转。所以我尝试从当前的transform 开始并为其添加不缩放,但现在它什么也没做(可见)。


CGColorRef color = [[colorArray objectAtIndex:colorIndex] CGColor];
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDuration:0.5f];
 [UIView setAnimationDidStopSelector:@selector(throbReset:context:)];
//  [[self layer] setFillMode:kCAFillModeForwards]; // apparently not needed
 CGAffineTransform xForm = CGAffineTransformMakeScale(2.0, 2.0);
 xForm = CGAffineTransformRotate(xForm, M_PI / 4);
 [self setTransform:xForm];
 [[self layer] setBackgroundColor:color];
 [UIView commitAnimations];
}

- (void)throbReset:(NSString *)animationID context:(void*)context {
 NSLog(@"-%@:%s fired", [self class], _cmd);
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:2.0];
 CGAffineTransform xForm = [self transform];
 xForm = CGAffineTransformScale(xForm, 1.0, 1.0);
 [self setTransform:xForm];
 [UIView commitAnimations];
}

【问题讨论】:

    标签: iphone animation uiview ios transform


    【解决方案1】:

    您只是缩放到相同的大小,因为您基本上是在说采用当前变换并在 X 上按 1:1 和在 Y 上按 1:1 进行缩放。您可能想要在您的第二种方法。

    CGAffineTransform xForm = [self transform];
    xForm = CGAffineTransformScale(xForm,0.5, 0.5);
    

    请记住,当您添加旋转以按相反顺序进行时,请先旋转然后缩放。如果您涉及翻译,这一点会更重要,但在这种情况下,这两种方式都可能有效。

    【讨论】:

    • 所以如果我刚刚将我的transform 属性设置为CGAffineTransformMakeScale(1.0, 1.0),它不会回到原来的大小吗?我猜我不太明白CGAffineTransformMakeScaleCGAffineTransformScale 之间的区别。我的意思是我了解参数是什么,但不了解底层功能。
    • 您正在相对于您在 CGAffine 函数调用中指定的变换进行操作,因此 TransformScale 相对于自身缩放输入。在这种情况下,您输入当前的变换,它会按 1:1 进行缩放。我不确定 MakeScale 的区别,也许它假设身份转换为输入转换。
    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2016-10-14
    • 2015-12-06
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多