2012-04-09 36 views
5

तो, मैं एक एनीमेशन जो इस एप्लिकेशन http://itunes.apple.com/us/app/spg-mobile-app/id312306003?mt=8 जो पहली बार देखने3 डी एनिमेशन - CABasicAnimation और CATransform3D विचारों

मैं विचारों flipping के लिए एनीमेशन को लागू किया है में है लागू करने के लिए कोशिश कर रहा हूँ और मैं करने में सफल रहा हूँ के बीच की खाई को दे रही है एक तरफ एनिमेट और जब मैं रिवर्स flipping के लिए रिवर्स तर्क दिया है, यह देखा गया

यहाँ

के बीच कुछ अंतराल दे रहा है यहाँ मैं विधि पहलेप्रदर्शन बोल रहा हूँ तो मेरी कोड

- (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]; 
} 

है,और यहाँ मैं अपने कुल कोड Here


इस कोड मेरे लिए एक सा काम किया लेकिन अभी भी कुछ अंतराल हो रही अपलोड कर दिया है, बात जा रहा देखने के फ्रेम है और आने वाले दृश्य एक ही होना चाहिए

- (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]; 
} 
+0

नमूना कोड पूरा नहीं हुआ है साथ में गड़बड़। यह निर्माण नहीं करेगा क्योंकि यह RotatingFlipSegue – Abizern

+0

@Charan के लिए फ़ाइलों को याद कर रहा है, मुझे उस दृश्य की भी आवश्यकता है। क्या आप इसे साझा कर सकते हैं? यह बहुत अच्छा होगा अगर आप अपने डेमो कोड को गिट करने और हमारे साथ साझा करने के लिए अपलोड करते हैं। – Siten

उत्तर

4

पिछले मेरी समस्या हल, लंगर बिंदु

- (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]; 
} 
2

मैंने वही किया। मेरा कामकाजी कोड नीचे है। कृपया इसे पसंद करें।

- (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 = (id) shot.CGImage; 

     return imageLayer; 
    } 

    - (void)animationDidStart:(CAAnimation *)animation 
    { 
     UIViewController *source; 
     if (islogin) { 
      source = (UIViewController *)self.navigationController; 
     } 
     else 
      source = (UIViewController *)self.joinTABBARController; 
     [source.view removeFromSuperview]; 
    } 

    - (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished 
    { 
     UIViewController *dest; 
     if (islogin) { 
      dest = (UIViewController *) self.joinTABBARController; 
     } 
     else 
      dest = (UIViewController *)self.navigationController; 
     [hostView addSubview:dest.view]; 
     [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:(TranDirection)aDirection 
    { 
     UIViewController *source; 
     UIViewController *dest; 
     if (islogin) { 
      source = (UIViewController *) self.navigationController; 
      dest = (UIViewController*) self.joinTABBARController; 
     } 
     else 
     { 
      source = (UIViewController *) self.joinTABBARController; 
      dest = (UIViewController *) self.navigationController; 
     } 
     hostView = source.view.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.view 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.view transform:transform]]; 

     if (aDirection == RTOL) 
     { 
      transform = CATransform3DRotate(transform, radians(90), 0, 1, 0); 
      transform = CATransform3DTranslate(transform, CUBESIZE, 0, 0); 
      [transformationLayer addSublayer:[self createLayerFromView:dest.view transform:transform]]; 
     } else { 
      transform = CATransform3DRotate(transform, radians(90), 0, 1, 0); 
      transform = CATransform3DTranslate(transform, CUBESIZE, 0, 0); 
      transform = CATransform3DRotate(transform, radians(90), 0, 1, 0); 
      transform = CATransform3DTranslate(transform, CUBESIZE, 0, 0); 
      transform = CATransform3DRotate(transform, radians(90), 0, 1, 0); 
      transform = CATransform3DTranslate(transform, CUBESIZE, 0, 0); 
      [transformationLayer addSublayer:[self createLayerFromView:dest.view transform:transform]]; 

     } 

    } 

    -(void)moveFrom:(TranDirection)aDirection duration:(float)aDuration 
    { 
     [CATransaction flush]; 
     CABasicAnimation *rotation; 
     CABasicAnimation *translationX; 
     CABasicAnimation *translationZ; 
     CAAnimationGroup *group = [CAAnimationGroup animation]; 
     group.delegate = self; 
     group.duration = aDuration; 

     if (aDirection == RTOL) 
     { 
      translationX   = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.x"]; 
      translationX.toValue = [NSNumber numberWithFloat:-(CUBE_VERTICAL_WIDTH/2)]; 
      rotation    = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.rotation.y"]; 
      rotation.toValue  = [NSNumber numberWithFloat:radians(-90)]; 
     } else { 
      translationX   = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.x"]; 
      translationX.toValue = [NSNumber numberWithFloat:(CUBE_VERTICAL_WIDTH/2)]; 
      rotation    = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.rotation.y"]; 
      rotation.toValue  = [NSNumber numberWithFloat:radians(90)] ; 
     } 

     translationZ    = [CABasicAnimation animationWithKeyPath:@"sublayerTransform.translation.z"]; 
     translationZ.toValue  = [NSNumber numberWithFloat:-(CUBE_VERTICAL_WIDTH/2)]; 
     group.animations   = [NSArray arrayWithObjects: rotation, translationX, translationZ, nil]; 
     group.fillMode   = kCAFillModeForwards; 
     group.removedOnCompletion = NO; 
     [transformationLayer addAnimation:group forKey:kAnimationKey]; 
    } 

    - (void)perform:(TranDirection)tdir 
    { 
     [self constructRotationLayer:tdir]; 
     // [self animateWithDuration:2.0f]; 
     [self moveFrom:tdir duration:.4]; 
    } 
+0

लेकिन मेरी समस्या फ्लिप एनीमेशन को उलटते समय है, मुझे – Charan

+0

विचारों के बीच एक अंतर मिल रहा है। मुझे पंचिंग बैग को पिछड़ा और ज़ेड अक्ष पर आगे बढ़ाना है ताकि इस संबंध में कोई सुझाव हो .. – Ankur