6

我想实现这样的目标:

在此处输入图像描述

这是我的视图控制器的快照

我试过的代码是这样的:

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    // Take a snapshot
    //
    _screenshotView = [[UIImageView alloc] initWithFrame:CGRectNull];

    _screenshotView.image = [self getScreenSnapshot];

    _screenshotView.frame = CGRectMake(-160, -284, _screenshotView.image.size.width, _screenshotView.image.size.height);
    _screenshotView.userInteractionEnabled = YES;
    _screenshotView.layer.anchorPoint = CGPointMake(0, 0);

    _originalSize = _screenshotView.frame.size;

    [window addSubview:_screenshotView];

    [self minimizeFromRect:CGRectMake(0, 0, _originalSize.width, _originalSize.height)];


- (void)minimizeFromRect:(CGRect)rect
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    CGFloat m = 0.7;
    CGFloat newWidth = _originalSize.width * m;
    CGFloat newHeight = _originalSize.height * m;

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:0.6] forKey:kCATransactionAnimationDuration];
    [self addAnimation:@"position.x" view:_screenshotView startValue:rect.origin.x endValue:window.frame.size.width - 80.0];
    [self addAnimation:@"position.y" view:_screenshotView startValue:rect.origin.y endValue:(window.frame.size.height - newHeight) / 2.0];
    [self addAnimation:@"bounds.size.width" view:_screenshotView startValue:rect.size.width endValue:newWidth];
    [self addAnimation:@"bounds.size.height" view:_screenshotView startValue:rect.size.height endValue:newHeight];

    _screenshotView.layer.position = CGPointMake(window.frame.size.width - 80.0, (window.frame.size.height - newHeight) / 2.0);
    _screenshotView.layer.bounds = CGRectMake(window.frame.size.width - 80.0, (window.frame.size.height - newHeight) / 2.0, newWidth, newHeight);

    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
    rotationAndPerspectiveTransform.m34 = 1.0 / -600;
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, -20.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
    _screenshotView.layer.transform = rotationAndPerspectiveTransform;

    [CATransaction commit];
}

代码工作正常,只是看起来旋转是从不同的角度显示的,因为动画后的快照是这样的:

在此处输入图像描述

我错过了什么?

4

2 回答 2

0

通过用图像层对象替换UIImageView对象来解决。CALayer感谢https://github.com/kristopherjohnson/perspectivetest

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    // Take a snapshot
    //

    UIImage *image = [self getScreenSnapshot];
    CGImageRef pictureImage = image.CGImage;

    CGFloat zDistance = 1200.0f;
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 1.0f / -zDistance;

    _screenshotView = [[UIView alloc] initWithFrame:CGRectNull];
    _screenshotView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    _screenshotView.userInteractionEnabled = YES;

    _imageLayer = [CALayer layer];
    _imageLayer.frame = _screenshotView.frame;;
    _imageLayer.contents = (__bridge id)pictureImage;
    _imageLayer.transform = transform;

    [_screenshotView.layer addSublayer:_imageLayer];

    _originalSize = _screenshotView.frame.size;

    [window addSubview:_screenshotView];

    [self minimizeFromRect:CGRectMake(0, 0, _originalSize.width, _originalSize.height)];


- (void)minimizeFromRect:(CGRect)rect
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    CGFloat m = 0.6;
    CGFloat newWidth = _originalSize.width * m;
    CGFloat newHeight = _originalSize.height * m;

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:0.4] forKey:kCATransactionAnimationDuration];
    [self addAnimation:@"position.x" view:_screenshotView startValue:rect.origin.x endValue:window.frame.size.width - 40.0];
    [self addAnimation:@"position.y" view:_screenshotView startValue:rect.origin.y endValue:(window.frame.size.height - newHeight) / 2.0];
    [self addAnimation:@"bounds.size.width" view:_screenshotView startValue:rect.size.width endValue:newWidth];
    [self addAnimation:@"bounds.size.height" view:_screenshotView startValue:rect.size.height endValue:newHeight];
    [self addAnimationForLayer:@"transform.rotation.y" view:_imageLayer startValue:0.0f endValue:-0.4 * M_PI];


    _screenshotView.layer.position = CGPointMake(window.frame.size.width - 80.0, (window.frame.size.height - newHeight) / 2.0);
    _screenshotView.layer.bounds = CGRectMake(window.frame.size.width - 80.0, (window.frame.size.height - newHeight) / 2.0, newWidth, newHeight);

    [CATransaction commit];
}
于 2013-07-23T10:05:56.977 回答
0

一种解决方法是您可以使用两张图像,一张用于上半部,一张用于下半部。您可以将两个图像拼接在一起,使间隙不可见,您可以获得所需的输出。

将属性设置为上图像和下图像的一个正值和一个负值,反之亦然。您可以获得所需结果的两个图像。然后,您可以设置框架以使它们粘在一起,使它们看起来像一个图像。

财产 ::rotationAndPerspectiveTransform.m34 = 1.0 / 600.0;

财产 ::rotationAndPerspectiveTransform.m34 = -1.0 / 600.0;

你可能需要做一些打击和试验。

于 2013-07-23T11:26:29.570 回答