【问题标题】:Mix CABasicAnimation with CGAffineTransform animation将 CABasicAnimation 与 CGAffineTransform 动画混合
【发布时间】:2015-12-01 14:53:24
【问题描述】:

我需要不断地旋转 UIImageView。为此,我找到了这段代码:

if ([self.image.layer animationForKey:@"SpinAnimation"] == nil) {

        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat: 2 * M_PI];
        animation.duration = 90.0f;
        animation.repeatCount = INFINITY;
        [self.image.layer addAnimation:animation forKey:@"SpinAnimation"];
    }

然后,我需要在图像视图上进行转换转换,为其设置动画。如果我这样做:

CGAffineTransform transform = self.image.transform;
transform = CGAffineTransformTranslate(transform, 0, 350);
[UIView animateWithDuration:1.0f animations:^{

        [self.view layoutIfNeeded];
        self.image.transform = transform;
    } completion:^(BOOL finished) {

        self.isMiddleViewOpened = YES;
    }];

执行动画时,图像会在视图周围浮动,然后到达平移的终点。 谢谢☺️

编辑 我有问题,因为在我的第二次变换中,我只编辑了 y 值,但图像不仅仅在 y 轴上移动。如果您尝试此代码,您会看到问题

【问题讨论】:

  • 什么是转换?它会旋转图像吗?
  • 您的代码有什么问题?你有什么问题吗?
  • 我有一个不同的想法,我将发布一种方法来做到这一点,而不将核心动画与ui动画混合
  • 那太好了:)

标签: ios objective-c animation transform


【解决方案1】:

没有办法混合两种动画,你可以使用 Core 动画来做你所有的动画。

我在启动画面中使用它来旋转四个图像块,最后将它们组合成一个。

动画是这样的:

一个图像块的代码: 左上块,我称之为00块

- (void)addSplashScreenAnimationWithCompletion:(void (^)(BOOL finished))completionBlock
{
    [self addSplashScreenAnimationWithBeginTime:0 andFillMode:kCAFillModeBoth andRemoveOnCompletion:NO completion:completionBlock];
}

- (void)addSplashScreenAnimationWithBeginTime:(CFTimeInterval)beginTime andFillMode:(NSString *)fillMode andRemoveOnCompletion:(BOOL)removedOnCompletion completion:(void (^)(BOOL finished))completionBlock
{
    CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    if (completionBlock)
    {
        CABasicAnimation *representativeAnimation = [CABasicAnimation animationWithKeyPath:@"not.a.real.key"];
        representativeAnimation.duration = 8.500; //your duration
        representativeAnimation.delegate = self;
        [self.layer addAnimation:representativeAnimation forKey:@"SplashScreen"];

    }

    CAKeyframeAnimation *_00RotationAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    _00RotationAnimation.duration = 8.500;
    _00RotationAnimation.values = @[@(0.000), @(18.829), @(18.829)];
    _00RotationAnimation.keyTimes = @[@(0.000), @(0.353), @(1.000)];
    _00RotationAnimation.timingFunctions = @[linearTiming, linearTiming];
    _00RotationAnimation.beginTime = beginTime;
    _00RotationAnimation.fillMode = fillMode;
    _00RotationAnimation.removedOnCompletion = removedOnCompletion;


    CAKeyframeAnimation *_00OpacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    _00OpacityAnimation.duration = 8.500;
    _00OpacityAnimation.values = @[@(0.000), @(0.497), @(0.553), @(0.759), @(0.921), @(1.000), @(0.642), @(0.341), @(0.000), @(0.000)];
    _00OpacityAnimation.keyTimes = @[@(0.000), @(0.059), @(0.118), @(0.176), @(0.235), @(0.353), @(0.471), @(0.647), @(0.765), @(1.000)];
    _00OpacityAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming, linearTiming];
    _00OpacityAnimation.beginTime = beginTime;
    _00OpacityAnimation.fillMode = fillMode;
    _00OpacityAnimation.removedOnCompletion = removedOnCompletion;


    CAKeyframeAnimation *_00ScaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
    _00ScaleXAnimation.duration = 8.500;
    _00ScaleXAnimation.values = @[@(0.600), @(0.600), @(1.187), @(1.187)];
    _00ScaleXAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)];
    _00ScaleXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming];
    _00ScaleXAnimation.beginTime = beginTime;
    _00ScaleXAnimation.fillMode = fillMode;
    _00ScaleXAnimation.removedOnCompletion = removedOnCompletion;

    CAKeyframeAnimation *_00ScaleYAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
    _00ScaleYAnimation.duration = 8.500;
    _00ScaleYAnimation.values = @[@(0.688), @(0.688), @(1.359), @(1.359)];
    _00ScaleYAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)];
    _00ScaleYAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming];
    _00ScaleYAnimation.beginTime = beginTime;
    _00ScaleYAnimation.fillMode = fillMode;
    _00ScaleYAnimation.removedOnCompletion = removedOnCompletion;


    CAKeyframeAnimation *_00TranslationXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    _00TranslationXAnimation.duration = 8.500;
    _00TranslationXAnimation.values = @[@(0.000), @(107.423), @(211.936), @(211.936)];
    _00TranslationXAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)];
    _00TranslationXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming];
    _00TranslationXAnimation.beginTime = beginTime;
    _00TranslationXAnimation.fillMode = fillMode;
    _00TranslationXAnimation.removedOnCompletion = removedOnCompletion;


    CAKeyframeAnimation *_00TranslationYAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    _00TranslationYAnimation.duration = 8.500;
    _00TranslationYAnimation.values = @[@(0.000), @(390.498), @(476.237), @(476.237)];
    _00TranslationYAnimation.keyTimes = @[@(0.000), @(0.353), @(0.765), @(1.000)];
    _00TranslationYAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming];
    _00TranslationYAnimation.beginTime = beginTime;
    _00TranslationYAnimation.fillMode = fillMode;
    _00TranslationYAnimation.removedOnCompletion = removedOnCompletion;


}

上述方法包含一系列动画,如旋转、不透明度变化、沿 x 和 y 轴缩放、沿 x 和 y 轴平移。 您需要继承 UIImageView 或 UIView 并将动画放入其中。

你可以像这样调用上面的方法:

 //Here _splashView can be UIView or UIImageView

[_splashView addSplashScreenAnimationWithCompletion:^(BOOL finished) {

      //Do your stuff after animation is completed
}];

CAKeyFrameAnimation 也可以使用repeatCount,并且应该给HUGE_VALF 以获得无限时间旋转。

【讨论】:

  • 那么,关键是要使用CAKeyframeAnimation吗?但是我的图像必须一直旋转,但是当我的视图打开时在它的 y 轴上平移,并在我的视图关闭时返回到它的位置。我该如何实施?我有点困惑
  • 根据我的要求,我将图像旋转了一定程度,您可以永远旋转图像!因此,当您打开视图时,图像必须通过旋转移动到某个位置?
  • 你也可以对 CAKeyFrameAnimation 使用 repeatCount,你应该给 HUGE_VALF 以获得无限时间旋转。
  • 是的,图像在移动到另一个位置时必须继续旋转。然后,当视图关闭时,图像必须返回到原始位置,同时旋转。最基本的是图像必须始终旋转。
  • 该图像也是可点击的,因此我可以识别图像视图上的点击并显示该图像的全屏版本。我注意到,当我使用变换移动图像时,可点击区域保持原始帧而不是新图层位置。我知道通过变换框架保持不变,但我怎样才能避免这种情况呢?我需要在旋转时移动所有视图,我尝试编辑约束,但没有效果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多