iPhone uiview 애니메이션 모범 사례
-
08-07-2019 - |
문제
iPhone에서보기 전환을 애니메이션하는 모범 사례로 간주되는 것은 무엇입니까?
예를 들어, ViewTransitions
Apple의 샘플 프로젝트는 다음과 같은 코드를 사용합니다.
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
접근 방식은 더 많은 제어력을 제공하고 더 많은 것을 설정할 수 있습니다. 타이밍 기능. 객체이기 때문에 나중에 저장할 수 있습니다. Refactor는 모든 애니메이션을 가리켜 복제 된 코드 등을 줄일 수 있습니다.
그만큼 UIView
수업 방법은 일반적인 애니메이션의 편의 방법이지만 CATransition
. 예를 들어, 4 개의 가능한 전환 유형이 있습니다 (플립 왼쪽, 오른쪽 뒤집기, 컬, 컬). 페이드를하고 싶다면 CATransition's
전환을 페이드하거나 명시적인 애니메이션을 설정하십시오. UIView
의 알파.
주목하십시오 CATransition
Mac OS X에서는 임의를 지정할 수 있습니다. CoreImage
전환으로 사용하도록 필터링하지만 지금은 iPhone 에서이 작업을 수행 할 수 없습니다. 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 및 Bug 3은 ImageViews입니다. 아래 애니메이션은 1 초 지연 후 1 초 지속 시간의 애니메이션을 설명합니다. Bug3는 중앙에서 Bug2의 중심으로 이동됩니다. 애니메이션이 완료되면 "센터 애니메이션 완료!"로 기록됩니다.
-(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)