gpt4 book ai didi

iphone - 同时在不同 View 上使用多个 CAAnimations

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:50 26 4
gpt4 key购买 nike

我正在尝试同时执行几个动画。一个是从一个 uimageview 过渡到另一个,另一个是动画标签的 translation.x。标签位于 uiimageview 之上。

但我得到的是翻译工作正常并且转换立即发生,或者基于隐藏属性的转换也适用于我的标签,它应该只被移动(它也从隐藏变为可见)。我不能使用 caanimationgroup,因为它们适用于不同的 View 。

//滑动标签的CAKeyFrameAnimation

...

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];

NSArray *xValues = @[[NSNumber numberWithFloat:myLabel.bounds.origin.x],
[NSNumber numberWithFloat:myLabel.bounds.origin.x + screenHalf],
[NSNumber numberWithFloat:myLabel.bounds.origin.x + screenHalf * 4]];
[anim setValues:xValues];

NSMutableArray *timeFrames = [NSMutableArray array];

CGFloat timeStep = 1.0 / ([xValues count] - 1);

for (NSInteger i = 0; i < [xValues count]; i++)
{
[timeFrames addObject:[NSNumber numberWithFloat:timeStep * i]];
}

[anim setKeyTimes:timeFrames];

[anim setDuration:duration];
[anim setFillMode:kCAFillModeForwards];
[anim setRemovedOnCompletion:FALSE];

[myLabel.layer addAnimation:anim forKey:nil];
...

//从uiimageview过渡到另一个

...
CATransition *transition = [CATransition animation];
[transition setDuration:duration];
[transition setType:kCATransitionFade];

//These two are uiimageviews i'm switching from and to
initial.hidden = TRUE;
next.hidden = FALSE;

//Initial and next are subviews of container which itself is a subview of viewcontroller's main view
[container.layer addAnimation:transition forKey:@""];

如果我像上面那样调用动画,转换会立即发生并且标签会正确地在屏幕上滑动。如果我将最后一行更改为:

[self.view.layer addAnimation:transition forKey:@""];

然后隐藏动画也适用于myLabel。像上面这样组合动画的修复方法是什么,更详细的原因是什么?

最佳答案

我会将整个代码包装在一个 CATransaction 中,以将不同的 CAAnimations 分组到一个组中。

将其与 CAKeyFrameAnimation 一起使用的伪代码如下所示:

[CATransaction begin];
// set completion block if you want
[CATransaction setCompletionBlock:^{ NSLog(@"I'm done"); }];

// start a keyframe animation
CAKeyframeAnimation *key1 = .....

// start another keyframe animation block
CAKeyframeAnimation *key2 = ......

// Maybe do a basic animation
CABasicAnimation *anim1 = .....

// close out all the animations and have them start
[CATransaction commit];

关于iphone - 同时在不同 View 上使用多个 CAAnimations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15764932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com