ios - 将 CABasicAnimation 与 CGAffineTransform 动画混合
<p><p>我需要不断地旋转 UIImageView。为此,我找到了这段代码:</p>
<pre><code>if ( == nil) {
CABasicAnimation* animation = ;
animation.fromValue = ;
animation.toValue = ;
animation.duration = 90.0f;
animation.repeatCount = INFINITY;
;
}
</code></pre>
<p>然后,我需要在 ImageView 上进行转换转换,为其设置动画。如果我这样做:</p>
<pre><code>CGAffineTransform transform = self.image.transform;
transform = CGAffineTransformTranslate(transform, 0, 350);
[UIView animateWithDuration:1.0f animations:^{
;
self.image.transform = transform;
} completion:^(BOOL finished) {
self.isMiddleViewOpened = YES;
}];
</code></pre>
<p>执行动画时,图像会在 View 周围 float ,然后到达平移的终点。
谢谢☺️</p>
<p><strong>编辑</strong>
我有问题,因为在我的第二次变换中,我只编辑了 y 值,但图像不仅仅在 y 轴上移动。如果您尝试此代码,您会看到问题</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>没有办法混合两种动画,你可以使用 Core 动画来做你所有的动画。</p>
<p>我在启动画面中使用它来旋转四个图像 block ,最后将它们组合成一个。</p>
<p>动画是这样的:</p>
<p> <a href="/image/Cn8xB.gif" rel="noreferrer noopener nofollow"><img src="/image/Cn8xB.gif" alt="enter image description here"/></a> </p>
<p><strong>一个图像 block 的代码:</strong> <em>左上 block ,我称之为00 block </em></p>
<pre><code>- (void)addSplashScreenAnimationWithCompletion:(void (^)(BOOL finished))completionBlock
{
;
}
- (void)addSplashScreenAnimationWithBeginTime:(CFTimeInterval)beginTime andFillMode:(NSString *)fillMode andRemoveOnCompletion:(BOOL)removedOnCompletion completion:(void (^)(BOOL finished))completionBlock
{
CAMediaTimingFunction *linearTiming = ;
if (completionBlock)
{
CABasicAnimation *representativeAnimation = ;
representativeAnimation.duration = 8.500; //your duration
representativeAnimation.delegate = self;
;
}
CAKeyframeAnimation *_00RotationAnimation = ;
_00RotationAnimation.duration = 8.500;
_00RotationAnimation.values = @[@(0.000), @(18.829), @(18.829)];
_00RotationAnimation.keyTimes = @[@(0.000), @(0.353), @(1.000)];
_00RotationAnimation.timingFunctions = @;
_00RotationAnimation.beginTime = beginTime;
_00RotationAnimation.fillMode = fillMode;
_00RotationAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00OpacityAnimation = ;
_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 = @;
_00OpacityAnimation.beginTime = beginTime;
_00OpacityAnimation.fillMode = fillMode;
_00OpacityAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00ScaleXAnimation = ;
_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 = @;
_00ScaleXAnimation.beginTime = beginTime;
_00ScaleXAnimation.fillMode = fillMode;
_00ScaleXAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00ScaleYAnimation = ;
_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 = @;
_00ScaleYAnimation.beginTime = beginTime;
_00ScaleYAnimation.fillMode = fillMode;
_00ScaleYAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00TranslationXAnimation = ;
_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 = @;
_00TranslationXAnimation.beginTime = beginTime;
_00TranslationXAnimation.fillMode = fillMode;
_00TranslationXAnimation.removedOnCompletion = removedOnCompletion;
CAKeyframeAnimation *_00TranslationYAnimation = ;
_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 = @;
_00TranslationYAnimation.beginTime = beginTime;
_00TranslationYAnimation.fillMode = fillMode;
_00TranslationYAnimation.removedOnCompletion = removedOnCompletion;
}
</code></pre>
<p>上述方法包含一系列动画,如旋转、不透明度变化、沿 x 和 y 轴缩放、沿 x 和 y 轴平移。
您需要继承 <code>UIImageView</code> 或 <code>UIView</code> 并将动画放在那里。</p>
<p>你可以像这样调用上面的方法:</p>
<pre><code> //Here _splashView can be UIView or UIImageView
[_splashView addSplashScreenAnimationWithCompletion:^(BOOL finished) {
//Do your stuff after animation is completed
}];
</code></pre>
<p><code>CAKeyFrameAnimation</code> 也可以使用 <code>repeatCount</code>,并且你应该给 <code>HUGE_VALF</code> 以获得无限时间旋转。</p></p>
<p style="font-size: 20px;">关于ios - 将 CABasicAnimation 与 CGAffineTransform 动画混合,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34023294/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34023294/
</a>
</p>
页:
[1]