OGeek|极客世界-中国程序员成长平台

标题: ios - 复制图层并移动它 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:04
标题: ios - 复制图层并移动它

我想创建一个动画,“创建 View 的副本,将此副本从一个点移动到另一个点,同时降低不透明度直到它完全消失”。

我想我需要通过 CABasicAnimation 来完成,所以我尝试了类似的方法:

    CGPoint point = CGPointMake(200, 0);

    // copy layer
    CALayer *layer = [[CALayer alloc] initWithLayer:self.animatedView.layer];
    [self.animatedView.layer addSublayer:layer];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath"position"];
    animation.fromValue = [layer valueForKey"position"];
    animation.toValue = [NSValue valueWithCGPoint:point];
    animation.duration = 2.0f;

    CABasicAnimation *oanimation = [CABasicAnimation animationWithKeyPath"opacity"];
    oanimation.fromValue = @1;
    oanimation.toValue = @0;
    oanimation.duration = 1.0f;

    // move copy layer
    [layer addAnimation:animation forKey"position"];
    [layer addAnimationanimation forKey"opacity"];

但是什么都没有,我认为这是关于文档的正常情况:

This initializer is used to create shadow copies of layers, for example, for the presentationLayer method. Using this method in any other situation will produce undefined behavior. For example, do not use this method to initialize a new layer with an existing layer’s content.

以前有人做过这种动画吗?

感谢您的帮助。



Best Answer-推荐答案


UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imgView = [[UIImageView alloc] initWithImage:screenShot];
imgView.frame = theView.frame;
[self.view addSubview:imgView];

然后玩 'imgView'

关于ios - 复制图层并移动它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17325120/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4