programing tip

iPhone UIView 애니메이션 모범 사례

itbloger 2020. 6. 18. 21:31
반응형

iPhone UIView 애니메이션 모범 사례


iPhone에서 뷰 전환에 애니메이션을 적용하는 가장 좋은 방법은 무엇입니까?

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

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 이상에서는이 방법을 사용하지 않는 것이 좋습니다. 대신 블록 기반 애니메이션 방법을 사용해야합니다.

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

[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접근 방식을 통해보다 많은 제어 기능을 제공 할 수 있습니다. 타이밍 기능. 객체이기 때문에 나중에 저장하고 리 팩터하여 모든 애니메이션이 객체를 가리 키도록하여 코드 복제 등을 줄일 수 있습니다.

The UIView class methods are convenience methods for common animations, but are more limited than CATransition. For example, there are only four possible transition types (flip left, flip right, curl up, curl down). If you wanted to do a fade in, you'd have to either dig down to CATransition's fade transition, or set up an explicit animation of your UIView's alpha.

Note that CATransition on Mac OS X will let you specify an arbitrary CoreImage filter to use as a transition, but as it stands now you can't do this on the iPhone, which lacks CoreImage.


We can animate images in ios 5 using this simple code.

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!");
    }];

In the UIView docs, have a read about this function for ios4+

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

Anyway the "Block" method is preffered now-a-days. I will explain the simple block below.

Consider the snipped below. bug2 and bug 3 are imageViews. The below animation describes an animation with 1 second duration after a delay of 1 second. The bug3 is moved from its center to bug2's center. Once the animation is completed it will be logged "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!");
                 }];
}

Hope that's clean!!!


I found a good tutorial in this link. Hope this will be helpful for some one.

uiview-animation-tutorial


Here is Code for Smooth animation, might Be helpful for many developers.
I found this snippet of code from this tutorial.

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.

let's do try and checkout For 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

반응형