2

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

我想我需要通过 a 来完成,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 addAnimation:oanimation forKey:@"opacity"];

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

此初始化程序用于创建图层的影子副本,例如,用于presentationLayer 方法。在任何其他情况下使用此方法将产生未定义的行为。例如,不要使用此方法用现有图层的内容初始化新图层。

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

谢谢你的帮助。

4

2 回答 2

1
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”:)

于 2014-10-24T15:28:30.013 回答
0

解决方案 1:CALayers

我在 Swift 4.2 中调整了 @Sriram 解决方案,并且更喜欢使用更适应的 CALayer 而不是UIImageView.

// We use the screen's scale to support Retina displays
UIGraphicsBeginImageContextWithOptions(viewToCopy.frame.size, false, UIScreen.main.scale)
if let context: CGContext = UIGraphicsGetCurrentContext() {
    // We convert the layer of the viewToCopy into an UIImage
    viewToCopy.layer.render(in: context)
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    // We create a layer that will hold the image. This layer can be animated further down the road.
    let layer = CALayer()
    layer.contents = screenshot?.cgImage // Don't forget .cgImage, otherwise it won't work
    layer.frame = viewToCopy.frame // For example
    view.layer.addSublayer(layer)
}

我想这个在大多数情况下都可以正常工作,但是我在使用 a 时遇到了一些问题UIVisualEffectView(效果坏了)。所以我不得不想出第二个解决方案。

解决方案2:UIView 复制

首先,您需要一个小扩展才能UIView轻松复制它。我使用了这个答案,但为了清楚起见,我会在这里复制它。

// MARK: - UIView + Copy

extension UIView {
    func copyView<T: UIView>() -> T {
        return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
    }
}

一旦你有了这个,你就可以复制视图,添加它并将它布局到你的视图层次结构中,然后像使用其他任何UIView.

let animationCardView = cardView.copyView()
animationCardView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationCardView)

NSLayoutConstraint.activate([animationCardView.leadingAnchor.constraint(equalTo: cardView.leadingAnchor),
                                     animationCardView.trailingAnchor.constraint(equalTo: cardView.trailingAnchor),
                                     animationCardView.topAnchor.constraint(equalTo: cardView.topAnchor),
                                     animationCardView.bottomAnchor.constraint(equalTo: cardView.bottomAnchor)])
于 2019-02-17T09:43:07.187 回答