【问题标题】:How to make a CGAffineTransform permanent?如何使 CGAffineTransform 永久化?
【发布时间】:2011-11-07 23:17:35
【问题描述】:

根据这个答案,有一种方法可以使 CGAffineTransform 永久化:

iphone - making the CGAffineTransform permanent

但没有解释......答案讲述了动画生成的副本,但不清楚如何获取它并分配给原始对象,所以这是代码,如何使其永久化?

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];

[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationRepeatCount:1];

[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

float angleRadians = 180 * ((float)M_PI / 180.0f);
CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians);

self.myView.transform = t;
[self.myView setCenter:CGPointMake(160, 220)];

[UIView commitAnimations];

谢谢

【问题讨论】:

    标签: iphone ios cgaffinetransform


    【解决方案1】:

    您可以尝试下一级到核心动画并应用具有以下属性的变换动画:

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D: t];
    animation.toValue = [NSValue valueWithCATransform3D: t];
    animation.duration = 0.0;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    [yourView.layer addAnimation:animation forKey:@"transform"];
    

    然后你可以做其他核心动画,它应该保留变换。不要忘记导入 QuartzCore 框架。

    【讨论】:

    • 您确定您通过的是 CATransform3D 而不是 CGAffineTransform?
    • 如果你这样定义 t 就可以了: CATransform3D t = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation(angleRadians));虽然关键部分,但在回答问题时将 animation.removedOnCompletion 设置为 NO。
    【解决方案2】:

    在 iOS 4.0 或更高版本中,Apple 建议使用基于块的动画

    参考:What are block-based animation methods in iPhone OS 4.0?

        [UIView animateWithDuration:0.3 
                          delay:0 
                        options:UIViewAnimationOptionCurveEaseOut 
                     animations:^{ self.myView.transform = CGAffineTransformMakeRotation(M_PI) } 
                     completion:NULL];
    

    不确定这是否能完全解决您的问题,但这是朝着正确方向迈出的一步。

    【讨论】:

    • 动画块可以工作,但是当我捏缩放视图时,视图会恢复到原始状态
    • 您的捏合缩放机制是您创建的吗?如果是这样,如果您使用方法 CGAffineTransformMakeScale 而不是 CGAffineTransformScale,则应用新的转换(例如缩放以使其更大)将覆盖旋转
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2018-06-18
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多