2012-11-26 2 views
1

내 UIView에 대해 fold 애니메이션을 세로로 만듭니다. 긴 검색 후에 다음 링크를 찾았습니다. Origami transition using CATransform3D perspective .. 위의 링크에서 저는 방금 정확한 애니메이션을 제공합니다. . 내 UIView의 역방향에 표시함으로써 내가 엉망 곳 .. 내가 잘 모릅니다 .. 어떤 도움을세로 접기 애니메이션

-(IBAction)animate 
{ 


    CATransform3D transform = CATransform3DIdentity; 
    CALayer *topSleeve; 
    CALayer *middleSleeve; 
    CALayer *topShadow; 
    CALayer *middleShadow; 
    UIView *mainView; 
    CGFloat width = self.view.frame.size.width; 
    CGFloat height = self.view.frame.size.height; 
    CALayer *firstJointLayer; 
    CALayer *secondJointLayer; 
    CALayer *perspectiveLayer; 

    mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; 
    mainView.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:mainView]; 

    UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    CGSize size = CGSizeMake(viewImage.size.width/2, viewImage.size.height); 


    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); 
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); 
    UIImage *solidColorImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // The left half of the image 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 
    [viewImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:1.0]; 
    UIImage *leftImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 
    [viewImage drawAtPoint:CGPointMake(-size.width, 0) blendMode:kCGBlendModeNormal alpha:1.0]; 
    [solidColorImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeMultiply alpha:0.1]; 
    UIImage *rightImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    perspectiveLayer = [CALayer layer]; 
    perspectiveLayer.frame = CGRectMake(0, 0, width/2, height); 
    [mainView.layer addSublayer:perspectiveLayer]; 

    firstJointLayer = [CATransformLayer layer]; 
    firstJointLayer.frame = mainView.bounds; 
    [perspectiveLayer addSublayer:firstJointLayer]; 

    topSleeve = [CALayer layer]; 
    topSleeve.frame = CGRectMake(0, 0, width/2, height); 
    topSleeve.anchorPoint = CGPointMake(0, 0.5); 
    topSleeve.backgroundColor = [UIColor colorWithPatternImage:leftImage].CGColor; 
    topSleeve.position = CGPointMake(0, height/2); 
    [firstJointLayer addSublayer:topSleeve]; 
    topSleeve.masksToBounds = YES; 

    secondJointLayer = [CATransformLayer layer]; 
    secondJointLayer.frame = mainView.bounds; 
    secondJointLayer.frame = CGRectMake(0, 0, width, height); 
    secondJointLayer.anchorPoint = CGPointMake(0, 0.5); 
    secondJointLayer.position = CGPointMake(width/2, height/2); 
    [firstJointLayer addSublayer:secondJointLayer]; 

    middleSleeve = [CALayer layer]; 
    middleSleeve.frame = CGRectMake(0, 0, width/2, height); 
    middleSleeve.anchorPoint = CGPointMake(0, 0.5); 
    middleSleeve.backgroundColor = [UIColor colorWithPatternImage:rightImage].CGColor; 
    middleSleeve.position = CGPointMake(0, height/2); 
    [secondJointLayer addSublayer:middleSleeve]; 
    middleSleeve.masksToBounds = YES; 


    firstJointLayer.anchorPoint = CGPointMake(0, 0.5); 
    firstJointLayer.position = CGPointMake(0, height/2); 

    topShadow = [CALayer layer]; 
    [topSleeve addSublayer:topShadow]; 
    topShadow.frame = topSleeve.bounds; 
    topShadow.backgroundColor = [UIColor blackColor].CGColor; 
    topShadow.opacity = 0.5; 

    middleShadow = [CALayer layer]; 
    [middleSleeve addSublayer:middleShadow]; 
    middleShadow.frame = middleSleeve.bounds; 
    middleShadow.backgroundColor = [UIColor blackColor].CGColor; 
    middleShadow.opacity = 0.5; 

    transform.m34 = 1.0/(-700.0); 
    perspectiveLayer.sublayerTransform = transform; 

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; 
    [animation setDuration:2]; 
    [animation setAutoreverses:YES]; 
    [animation setRepeatCount:INFINITY]; 
    [animation setFromValue:[NSNumber numberWithDouble:90*M_PI/180]]; 
    [animation setToValue:[NSNumber numberWithDouble:0]]; 
    [firstJointLayer addAnimation:animation forKey:nil]; 

    animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; 
    [animation setDuration:2]; 
    [animation setAutoreverses:YES]; 
    [animation setRepeatCount:INFINITY]; 
    [animation setFromValue:[NSNumber numberWithDouble:-180*M_PI/180]]; 
    [animation setToValue:[NSNumber numberWithDouble:0]]; 
    [secondJointLayer addAnimation:animation forKey:nil]; 



    animation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"]; 
    [animation setDuration:2]; 
    [animation setAutoreverses:YES]; 
    [animation setRepeatCount:INFINITY]; 
    [animation setFromValue:[NSNumber numberWithDouble:perspectiveLayer.bounds.size.width]]; 
    [animation setToValue:[NSNumber numberWithDouble:0]]; 
    [perspectiveLayer addAnimation:animation forKey:nil]; 

    animation = [CABasicAnimation animationWithKeyPath:@"position.x"]; 
    [animation setDuration:2]; 
    [animation setAutoreverses:YES]; 
    [animation setRepeatCount:INFINITY]; 
    [animation setFromValue:[NSNumber numberWithDouble:perspectiveLayer.position.x]]; 
    [animation setToValue:[NSNumber numberWithDouble:0]]; 
    [perspectiveLayer addAnimation:animation forKey:nil]; 

    animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    [animation setDuration:2]; 
    [animation setAutoreverses:YES]; 
    [animation setRepeatCount:INFINITY]; 
    [animation setFromValue:[NSNumber numberWithDouble:0.5]]; 
    [animation setToValue:[NSNumber numberWithDouble:0]]; 
    [topShadow addAnimation:animation forKey:nil]; 

    animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    [animation setDuration:2]; 
    [animation setAutoreverses:YES]; 
    [animation setRepeatCount:INFINITY]; 
    [animation setFromValue:[NSNumber numberWithDouble:0.5]]; 
    [animation setToValue:[NSNumber numberWithDouble:0]]; 
    [middleShadow addAnimation:animation forKey:nil]; 

} 
+0

이봐 난 같은 애니메이션을 원하지만하지 수평에서 수직 방법으로 당신이 나를 PLS에게 감사를 도울 수 있습니다! – kb920

+0

동일한 코드를 사용하십시오. 높이를 너비로 변경하고 y를 x로 변경하십시오. 분명히 코드가 작동합니다. 당신이 논리를 이해한다면 그것은 당신에게 쉽습니다. –

답변

2

.. 감상 할 수있는 CALayer 그 배경은 이성을 상실립니다 때문이다. 각 이미지를 CALayer 내용으로 설정하는 것이 좋습니다. 이렇게하면 각 이미지가 예상대로 (뒤집히지 않고) 그려집니다. 그런데 큰 이미지가있는 패턴은 메모리 사용량을 증가시킬 수 있으므로 피해야합니다.

topSleeve.contents = (id)leftImage.CGImage; 
middleSleeve.contents = (id)rightImage.CGImage; 
+0

잘 작동합니다 :) :). 감사합니다 –

+0

이봐, 난 똑같은 애니메이션을 원하지만 수직적 인 방법으로 가로로하지 않으면 pls을 도울 수있다 고마워! – kb920