作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
所以,我正在尝试实现此应用程序中的动画 http://itunes.apple.com/us/app/spg-mobile-app/id312306003?mt=8这是在第一个 View 中
我已经为翻转 View 实现了动画,我成功地为一侧设置了动画,当我给出了反向翻转的反向逻辑时,它在 View 之间给出了一些间隙
这是我的代码
- (UIImage *)screenShot: (UIView *) aView
{
// Arbitrarily masks to 40%. Use whatever level you like
UIGraphicsBeginImageContext(hostView.frame.size);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextSetRGBFillColor (UIGraphicsGetCurrentContext(), 0, 0, 0, 0.4f);
CGContextFillRect (UIGraphicsGetCurrentContext(), hostView.frame);
UIGraphicsEndImageContext();
return image;
}
- (CALayer *) createLayerFromView: (UIView *) aView transform: (CATransform3D) transform
{
CALayer *imageLayer = [CALayer layer];
imageLayer.anchorPoint = CGPointMake(1.0f, 1.0f);
imageLayer.frame = (CGRect){.size = hostView.frame.size};
imageLayer.transform = transform;
UIImage *shot = [self screenShot:aView];
imageLayer.contents = (__bridge id) shot.CGImage;
return imageLayer;
}
- (void)animationDidStart:(CAAnimation *)animation
{
UIView *source = (UIView *) _source;
[source removeFromSuperview];
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
UITableView *dest = (UITableView *) destination;
dest.frame = CGRectMake(160, 0, 160, 460);
[hostView addSubview:dest];
[transformationLayer removeFromSuperlayer];
//if (delegate)
//SAFE_PERFORM_WITH_ARG(delegate, @selector(segueDidComplete), nil);
}
-(void)animateWithDuration: (CGFloat) aDuration
{
goesForward=FALSE ;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.delegate = self;
group.duration = aDuration;
CGFloat halfWidth = hostView.frame.size.width; // 2.0f;
float multiplier = goesForward ? -1.0f : 1.0f;
CABasicAnimation *translationX = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.x"];
translationX.toValue = [NSNumber numberWithFloat:multiplier * halfWidth];
CABasicAnimation *translationZ = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.z"];
translationZ.toValue = [NSNumber numberWithFloat:-200.0]; //halfWidth];
CABasicAnimation *rotationY = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.rotation.y"];
rotationY.toValue = [NSNumber numberWithFloat: multiplier * M_PI_2];
group.animations = [NSArray arrayWithObjects: rotationY, translationX, translationZ, nil];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[CATransaction flush];
[transformationLayer addAnimation:group forKey:kAnimationKey];
}
- (void) constructRotationLayer
{
UIView *source = (UIView *) _source;
UIView *dest = (UIView *) destination;
hostView = [source superview];
// if ([hostView isKindOfClass:[UIWindow class]]) {
// NSLog(@"I am window class");
// }
transformationLayer = [CALayer layer];
transformationLayer.frame = hostView.bounds; //CGRectMake(40, 0, 280, 460);
transformationLayer.anchorPoint = CGPointMake(0.5f, 0.5f);
CATransform3D sublayerTransform = CATransform3DIdentity;
sublayerTransform.m34 = 1.0 / -1000;
[transformationLayer setSublayerTransform:sublayerTransform];
[hostView.layer addSublayer:transformationLayer];
CATransform3D transform = CATransform3DMakeTranslation(0, 0, 0);
[transformationLayer addSublayer:[self createLayerFromView:source transform:CATransform3DRotate(transform, M_PI_2, 0, 0, 0)]];
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
if (!goesForward)
{
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
}
[transformationLayer addSublayer:[self createLayerFromView:dest transform:transform]];
}
- (void)perform
{
[self constructRotationLayer];
[self animateWithDuration:0.4f];
}
所以,这里我先调用 perform 方法在这里我上传了我的总代码 Here
这段代码对我有点用,但仍然有一些差距,问题是 going view 和 coming view 的框架应该相同
- (UIImage *)screenShot: (UIView *) aView
{
// Arbitrarily masks to 40%. Use whatever level you like
UIGraphicsBeginImageContext(hostView.frame.size);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextSetRGBFillColor (UIGraphicsGetCurrentContext(), 0, 0, 0, 0.4f);
CGContextFillRect (UIGraphicsGetCurrentContext(), hostView.frame);
UIGraphicsEndImageContext();
return image;
}
- (CALayer *) createLayerFromView: (UIView *) aView transform: (CATransform3D) transform
{
CALayer *imageLayer = [CALayer layer];
imageLayer.anchorPoint = CGPointMake(1.0f, 1.0f);
imageLayer.frame = (CGRect){.size = hostView.frame.size};
imageLayer.transform = transform;
UIImage *shot = [self screenShot:aView];
imageLayer.contents = (__bridge id) shot.CGImage;
return imageLayer;
}
- (void)animationDidStart:(CAAnimation *)animation
{
// UIView *source = (UIView *) super.sourceViewController;
[source removeFromSuperview];
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
// UIView *dest = (UIView *) super.destinationViewController;
if (hostView !=nil) {
NSLog(@"hostView %@",hostView);
if (dest) {
[hostView addSubview:dest];
}
}
[transformationLayer removeFromSuperlayer];
if (delegate)
SAFE_PERFORM_WITH_ARG(delegate, @selector(segueDidComplete), nil);
}
-(void)animateWithDuration: (CGFloat) aDuration
{
CAAnimationGroup *group = [CAAnimationGroup animation];
group.delegate = self;
group.duration = aDuration;
CGFloat halfWidth = hostView.frame.size.width / 2.0f;
float multiplier = goesForward ? -1.0f : 1.0f;
CABasicAnimation *translationX = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.x"];
translationX.toValue = [NSNumber numberWithFloat:multiplier * halfWidth];
CABasicAnimation *translationZ = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.z"];
translationZ.toValue = [NSNumber numberWithFloat:-halfWidth];
CABasicAnimation *rotationY = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.rotation.y"];
rotationY.toValue = [NSNumber numberWithFloat: multiplier * M_PI_2];
group.animations = [NSArray arrayWithObjects: rotationY, translationX, translationZ, nil];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[CATransaction flush];
[transformationLayer addAnimation:group forKey:kAnimationKey];
}
- (void) constructRotationLayer
{
// UIView *source = (UIView *) super.sourceViewController;
// UIView *dest = (UIView *) super.destinationViewController;
hostView = source.superview;
transformationLayer = [CALayer layer];
transformationLayer.frame = hostView.bounds;
transformationLayer.anchorPoint = CGPointMake(0.5f, 0.5f);
CATransform3D sublayerTransform = CATransform3DIdentity;
sublayerTransform.m34 = 1.0 / -1000;
[transformationLayer setSublayerTransform:sublayerTransform];
[hostView.layer addSublayer:transformationLayer];
CATransform3D transform = CATransform3DMakeTranslation(0, 0, 0);
[transformationLayer addSublayer:[self createLayerFromView:source transform:transform]];
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
if (!goesForward)
{
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
}
[transformationLayer addSublayer:[self createLayerFromView:dest transform:transform]];
}
- (void)perform
{
[self constructRotationLayer];
[self animateWithDuration:0.4f];
}
最佳答案
终于解决了我的问题,搞砸了 anchor
- (UIImage *)screenShot: (UIView *) aView
{
// Arbitrarily masks to 40%. Use whatever level you like
UIGraphicsBeginImageContext(hostView.frame.size);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextSetRGBFillColor (UIGraphicsGetCurrentContext(), 0, 0, 0, 0.4f);
CGContextFillRect (UIGraphicsGetCurrentContext(), hostView.frame);
UIGraphicsEndImageContext();
return image;
}
- (CALayer *) createLayerFromView: (UIView *) aView transform: (CATransform3D) transform
{
CALayer *imageLayer = [CALayer layer];
imageLayer.anchorPoint = CGPointMake(1.0f, 1.0f);
imageLayer.frame = (CGRect){.size = hostView.frame.size};
imageLayer.transform = transform;
UIImage *shot = [self screenShot:aView];
imageLayer.contents = (__bridge id) shot.CGImage;
return imageLayer;
}
- (void)animationDidStart:(CAAnimation *)animation
{
// UIView *source = (UIView *) super.sourceViewController;
[source removeFromSuperview];
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
// UIView *dest = (UIView *) super.destinationViewController;
if (hostView !=nil) {
NSLog(@"hostView %@",hostView);
if (dest) {
[hostView addSubview:dest];
}
}
[transformationLayer removeFromSuperlayer];
if (delegate)
SAFE_PERFORM_WITH_ARG(delegate, @selector(segueDidComplete), nil);
}
-(void)animateWithDuration: (CGFloat) aDuration
{
CAAnimationGroup *group = [CAAnimationGroup animation];
group.delegate = self;
group.duration = aDuration;
CGFloat halfWidth = hostView.frame.size.width / 2.0f;
float multiplier = goesForward ? -1.0f : 1.0f;
CABasicAnimation *translationX = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.x"];
translationX.toValue = [NSNumber numberWithFloat:multiplier * halfWidth];
CABasicAnimation *translationZ = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.z"];
translationZ.toValue = [NSNumber numberWithFloat:-halfWidth];
CABasicAnimation *rotationY = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.rotation.y"];
rotationY.toValue = [NSNumber numberWithFloat: multiplier * M_PI_2];
group.animations = [NSArray arrayWithObjects: rotationY, translationX, translationZ, nil];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[CATransaction flush];
[transformationLayer addAnimation:group forKey:kAnimationKey];
}
- (void) constructRotationLayer
{
// UIView *source = (UIView *) super.sourceViewController;
// UIView *dest = (UIView *) super.destinationViewController;
hostView = source.superview;
transformationLayer = [CALayer layer];
transformationLayer.frame = hostView.bounds;
transformationLayer.anchorPoint = CGPointMake(0.5f, 0.5f);
CATransform3D sublayerTransform = CATransform3DIdentity;
sublayerTransform.m34 = 1.0 / -1000;
[transformationLayer setSublayerTransform:sublayerTransform];
[hostView.layer addSublayer:transformationLayer];
CATransform3D transform = CATransform3DMakeTranslation(0, 0, 0);
[transformationLayer addSublayer:[self createLayerFromView:source transform:transform]];
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
if (!goesForward)
{
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
transform = CATransform3DTranslate(transform, hostView.frame.size.width, 0, 0);
}
[transformationLayer addSublayer:[self createLayerFromView:dest transform:transform]];
}
- (void)perform
{
[self constructRotationLayer];
[self animateWithDuration:0.4f];
}
关于iphone - 3D 动画 - CABasicAnimation 和 CATransform3D 提供 View 之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071139/
我是一名优秀的程序员,十分优秀!