UIView淡入淡出动画、3D旋转动画
淡入淡出动画
UIView *view01 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 100, 50)];
view01.backgroundColor = [UIColor redColor];
[self.view addSubview:view01];
view01.alpha = 1.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:3.0];
[UIView setAnimationRepeatCount:3];
[UIView setAnimationDelegate:self];
view01.alpha = 0.0;
[UIView commitAnimations];
3D旋转Block动画:
UIView *view01 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 100, 50)];
[UIView animateWithDuration:2 animations:^{
view01.backgroundColor = [UIColor cyanColor];
view01.alpha = 1.0;
[self.view addSubview:view01];
[UIView setAnimationDuration:2.0];
[UIView setAnimationRepeatCount:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view01.transform = CGAffineTransformMakeRotation(3.14);
} completion:^(BOOL finish){
if (finish) {
NSLog(@"animationstop");
}
这里设置动画结束之后还有一些代理方法,例如stop动画结束的完成回调方法,start动画开始回调方法等等。自定义转场动画:
从IOS7之后可以定义自定义转场动画:
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
UIViewController * viewfrom = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController * viewto = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *mtransitionContext = [transitionContext containerView];
[mtransitionContext addSubview:viewfrom.view];
[mtransitionContext addSubview:viewto.view];
viewto.view.frame = CGRectMake(100, 0, 320, 568);
[UIView animateWithDuration:5.0 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^(){
viewto.view.frame = CGRectMake(0, 0, 320, 568);
} completion:^(BOOL finished){
[transitionContext completeTransition:YES];
}];
}
剩下的我们只需要实现navigation的代理协议就可以了:
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
id anim = nil;
anim = mslid;
return anim;
}