programing

아이폰 UIView 애니메이션 모범 사례

linuxpc 2023. 6. 20. 21:25
반응형

아이폰 UIView 애니메이션 모범 사례

iPhone에서 보기 전환 애니메이션을 만드는 데 가장 좋은 방법은 무엇입니까?

예를 들어,ViewTransitions애플의 샘플 프로젝트는 다음과 같은 코드를 사용합니다.

CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:1];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myview layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

그러나 다음과 같은 코드 스니펫도 인터넷에 떠돌고 있습니다.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
[myview removeFromSuperview];
[UIView commitAnimations];

가장 좋은 방법은 무엇입니까?스니펫도 함께 제공해주시면 감사하겠습니다.

참고: 두 번째 접근 방식이 제대로 작동하지 않았습니다.

UIView 참조의 섹션에서 다음과 같은 정보를 확인합니다.beginAnimations:context:방법:

iPhone OS 4.0 이상에서는 이 방법을 사용하지 않습니다.대신 블록 기반 애니메이션 방법을 사용해야 합니다.

톰의 코멘트를 기반으로 한 블록 기반 애니메이션의 Eg

[UIView transitionWithView:mysuperview 
                  duration:0.75
                   options:UIViewAnimationTransitionFlipFromRight
                animations:^{ 
                    [myview removeFromSuperview]; 
                } 
                completion:nil];

저는 많은 멋진 경량 애니메이션을 위해 후자를 사용해 왔습니다.두 보기를 교차 페이드하거나, 다른 보기 앞에서 페이드하거나, 페이드아웃할 수 있습니다.배너처럼 다른 사람 위에 뷰를 찍을 수도 있고, 뷰를 늘리거나 축소할 수도 있습니다.저는 많은 마일리지를 얻고 있습니다.beginAnimation/commitAnimations.

당신이 할 수 있는 일이라고는 생각하지 마세요.

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];

다음은 샘플입니다.

[UIView beginAnimations:nil context:NULL]; {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    if (movingViewIn) {
// after the animation is over, call afterAnimationProceedWithGame
//  to start the game
        [UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];

//      [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation
//      [UIView setAnimationDelay:0.50];
//      [UIView setAnimationRepeatAutoreverses:YES];

        gameView.alpha = 1.0;
        topGameView.alpha = 1.0;
        viewrect1.origin.y = selfrect.size.height - (viewrect1.size.height);
        viewrect2.origin.y = -20;

        topGameView.alpha = 1.0;
    }
    else {
    // call putBackStatusBar after animation to restore the state after this animation
        [UIView setAnimationDidStopSelector:@selector(putBackStatusBar)];
        gameView.alpha = 0.0;
        topGameView.alpha = 0.0;
    }
    [gameView setFrame:viewrect1];
    [topGameView setFrame:viewrect2];

} [UIView commitAnimations];

보시다시피, 알파, 프레임, 심지어 보기 크기까지 가지고 놀 수 있습니다.놀아요.여러분은 그것의 능력에 놀랄지도 모릅니다.

다른 점은 애니메이션에 필요한 컨트롤의 양인 것 같습니다.

CATransition접근 방식은 더 많은 제어를 제공하므로 타이밍 기능과 같은 더 많은 설정 작업을 제공합니다.객체이기 때문에 나중에 사용할 수 있도록 저장할 수 있으며, 모든 애니메이션이 객체를 가리키도록 리팩터하여 중복 코드를 줄일 수 있습니다.

UIView클래스 메소드는 일반적인 애니메이션을 위한 편리한 방법이지만 보다 제한적입니다.CATransition예를 들어, 가능한 전환 유형은 네 가지(왼쪽 뒤집기, 오른쪽 뒤집기, 위로 감기, 아래로 감기)뿐입니다.페이드 인을 하고 싶다면, 당신은 파이드 인을 해야 할 것입니다.CATransition's페이드 전환 또는 명시적인 애니메이션 설정UIView의 알파.

참고:CATransitionMac OS X에서 임의로 지정할 수 있습니다.CoreImage전환으로 사용하기 위해 필터를 사용하지만, 현재 상태로는 아이폰에서 이것을 수행할 수 없습니다.CoreImage.

우리는 이 간단한 코드를 사용하여 ios 5에서 이미지를 애니메이션화할 수 있습니다.

CGRect imageFrame = imageView.frame;
imageFrame.origin.y = self.view.bounds.size.height;

[UIView animateWithDuration:0.5
    delay:1.0
    options: UIViewAnimationCurveEaseOut
    animations:^{
        imageView.frame = imageFrame;
    } 
    completion:^(BOOL finished){
        NSLog(@"Done!");
    }];

에서UIView문서, ios4+의 이 기능에 대해 읽어보십시오.

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

어쨌든 "차단" 방법이 요즘 선호됩니다.아래의 간단한 블록에 대해 설명하겠습니다.

아래 캡처된 내용을 고려해 보십시오. bug2와 bug3는 imageView입니다.아래 애니메이션은 1초 지연 후 1초 지속되는 애니메이션을 설명합니다.bug3는 bug2의 중심에서 bug2의 중심으로 이동합니다.애니메이션이 완료되면 "Center Animation Done!"으로 기록됩니다.

-(void)centerAnimation:(id)sender
{
NSLog(@"Center animation triggered!");
CGPoint bug2Center = bug2.center;

[UIView animateWithDuration:1
                      delay:1.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     bug3.center = bug2Center;
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Center Animation Done!");
                 }];
}

원활한 애니메이션을 위한 코드입니다.
이 튜토리얼에서 이 코드 조각을 찾았습니다.

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setAutoreverses:YES];
[animation setFromValue:[NSNumber numberWithFloat:1.3f]];
[animation setToValue:[NSNumber numberWithFloat:1.f]];
[animation setDuration:2.f];
[animation setRemovedOnCompletion:NO];

[animation setFillMode:kCAFillModeForwards];
[[self.myView layer] addAnimation:animation forKey:@"scale"];/// add here any Controller that you want t put Smooth animation.

Swift 3에 대해 시도해 보고 확인해 보겠습니다.

UIView.transition(with: mysuperview, duration: 0.75, options:UIViewAnimationOptions.transitionFlipFromRight , animations: {
    myview.removeFromSuperview()
}, completion: nil)

언급URL : https://stackoverflow.com/questions/630265/iphone-uiview-animation-best-practice

반응형