在我的 UIViewController
中,我使用 image1 创建 CALayer
。
CALayer *imageLayer = [CALayer layer];
[imageLayer setBounds:CGRectMake(0.0, 0.0, 241.0, 430.0)];
[imageLayer setPosition:CGPointMake(160.0, 60.0)];
[imageLayer setAnchorPoint:CGPointMake(0.5, 0.0)];
[imageLayer setContentsid)[UIImage imageNamed"image1.png".CGImage];
[imageLayer setContentsGravity:kCAGravityResizeAspect];
[self.view.layer addSublayer:imageLayer];
稍后在 UIViewController
我想通过使用 CATransition
推送来更改图像。
CATransition *t = [CATransition animation];
t.beginTime = [self.view.layer convertTime:CACurrentMediaTime() + 6.5 fromLayer:nil];
t.type = kCATransitionPush;
t.subtype = kCATransitionFromRight;
[imageLayer setContentsid)[UIImage imageNamed"image2.png"].CGImage];
[imageLayer addAnimation:t forKey:nil];
在运行代码时,我从一开始就得到了 image2,但转换会在 6.5 秒后自行插入。 图1在哪里?怎么了?
正确答案: 感谢 Jacky Boy 和 Tiago Almeida。 我更正了删除该行的代码
t.beginTime = [self.view.layer convertTime:CACurrentMediaTime() + 6.5 fromLayer:nil];
并添加“dispatch_after”。
现在可以了:
CATransition *t = [CATransition animation];
t.type = kCATransitionPush;
t.subtype = kCATransitionFromRight;
[imageLayer setContentsid)[UIImage imageNamed"image2.png"].CGImage];
[imageLayer addAnimation:t forKey:nil];
[CATransaction begin];
NSTimeInterval delayInSeconds = 6.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[imageLayer setContentsid)[UIImage imageNamed"image2.png"].CGImage];
[imageLayer addAnimation:t forKey:nil];
});
[CATransaction commit];
再次感谢各位!
删除以下行:
t.beginTime = [self.view.layer convertTime:CACurrentMediaTime() + 1.5 fromLayer:nil];
这就是延迟动画的原因,所以图层在设置新的内容,并且只有在图层动画之后。如果要指定动画的时长:
[CATransaction begin];
[CATransaction setAnimationDuration:1.0];
CATransition *t = [CATransition animation];
t.type = kCATransitionPush;
t.subtype = kCATransitionFromRight;
[_layer setContentsid)[UIImage imageNamed"image2.jpg"].CGImage];
[_layer addAnimation:t forKey:nil];
[CATransaction commit];
或者更小的版本:
CATransition *t = [CATransition animation];
t.type = kCATransitionPush;
[t setDuration:2.5];
t.subtype = kCATransitionFromRight;
[_layer setContentsid)[UIImage imageNamed"image2.jpg"].CGImage];
[_layer addAnimation:t forKey:nil];
我更喜欢这个:
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.duration = .25f;
transition.subtype = kCATransitionFromRight;
_layer.actions = @{@"contents": transition};
当你想改变内容时:
[_layer setContentsid)[UIImage imageNamed"image2.jpg"].CGImage];
关于iOS - CATransition 问题 : showing end image at start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164305/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |