iOS - 视图转换



视图转换的使用

视图转换是将一个视图添加到另一个视图的有效方法,并带有恰当的过渡动画效果。

按如下方式更新 ViewController.xib −

iOS Tutorial

为 xib 中创建的按钮创建动作。

更新 ViewController.h

在 ViewController 类中声明两个视图实例。在创建动作后,ViewController.h 文件如下所示:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
   UIView *view1;
   UIView *view2;
}

-(IBAction)flipFromLeft:(id)sender;
-(IBAction)flipFromRight:(id)sender;
-(IBAction)flipFromTop:(id)sender;
-(IBAction)flipFromBottom:(id)sender;
-(IBAction)curlUp:(id)sender;
-(IBAction)curlDown:(id)sender;
-(IBAction)dissolve:(id)sender;
-(IBAction)noTransition:(id)sender;

@end

更新 ViewController.m

我们将添加一个自定义方法 setUpView 以初始化视图。我们还会创建另一个方法 doTransitionWithType:,该方法从 view1view2 或反过来创建转换。然后,我们将实现之前创建的动作方法,该方法调用带有转换类型的 doTransitionWithType: 方法。更新后的 ViewController.m 如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   [self setUpView];
   // Do any additional setup after loading the view, typically from a nib.
}

-(void)setUpView {
   view1 = [[UIView alloc]initWithFrame:self.view.frame];
   view1.backgroundColor = [UIColor lightTextColor];
   view2 = [[UIView alloc]initWithFrame:self.view.frame];
   view2.backgroundColor = [UIColor orangeColor];
   [self.view addSubview:view1];
   [self.view sendSubviewToBack:view1];
}

-(void)doTransitionWithType:(UIViewAnimationTransition)animationTransitionType {
   if ([[self.view subviews] containsObject:view2 ]) {
      [UIView transitionFromView:view2
      toView:view1
      duration:2
      options:animationTransitionType
      completion:^(BOOL finished) {
         [view2 removeFromSuperview];
      }];
      [self.view addSubview:view1];
      [self.view sendSubviewToBack:view1];
   } else {
      [UIView transitionFromView:view1
      toView:view2
      duration:2
      options:animationTransitionType
      completion:^(BOOL finished) {
         [view1 removeFromSuperview];
      }];
      [self.view addSubview:view2];
      [self.view sendSubviewToBack:view2];
   }
}

-(IBAction)flipFromLeft:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionFlipFromLeft];
}

-(IBAction)flipFromRight:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionFlipFromRight];
}

-(IBAction)flipFromTop:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionFlipFromTop];
}

-(IBAction)flipFromBottom:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionFlipFromBottom];
}

-(IBAction)curlUp:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionCurlUp];
}

-(IBAction)curlDown:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionCurlDown];
}

-(IBAction)dissolve:(id)sender {
   [self doTransitionWithType:UIViewAnimationOptionTransitionCrossDissolve];
}

-(IBAction)noTransition:(id)sender{
   [self doTransitionWithType:UIViewAnimationOptionTransitionNone];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}
@end

输出

当我们运行应用程序时,我们将得到以下输出 −

iOS Tutorial

您可以选择不同的按钮,查看转换的工作方式。在选择卷曲时,转换将如下所示 −

iOS Tutorial
ios_ui_elements.htm
广告
© . All rights reserved.