Multiview

iOS多视图切换实现
本文详细介绍如何在iOS应用中实现三个视图控制器间的平滑切换,包括Storyboard配置、按钮事件响应及动画效果的实现。
  • 新建3个ViewController的类
  • 新建main.stroyboard,并配置好相应的布局(编辑界面,连接视图与类,ViewController的Storyboard ID,连接3个Button的响应事件)
  • 删除app delegate中didFinishLaunchingWithOptions中的代码
  • 修改SwitchViewController.m代码(viewDidLoad,didReceiveMemoryWarning,switchButton的响应函数(包括动画))
  • 修改BlueViewController.m和YellowViewController.m,添加button响应函数

XYZSwitchViewController.m

//
//  XYZSwitchViewController.m
//  MultiView
//
//  Created by Norcy on 14-7-23.
//  Copyright (c) 2014年 QQLive. All rights reserved.
//

#import "XYZSwitchViewController.h"
#import "XYZBlueViewController.h"
#import "XYZYellowViewController.h"

@interface XYZSwitchViewController ()
@property (strong, nonatomic)XYZBlueViewController *blueViewController;
@property (strong, nonatomic)XYZYellowViewController *yellowViewController;
@end

@implementation XYZSwitchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.blueViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
    
    [self.view insertSubview:self.blueViewController.view atIndex:0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if (!self.yellowViewController.view.superview)
        self.yellowViewController = nil;
    else
        self.blueViewController = nil;
}

- (IBAction)switchViews:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration: 0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    if (!self.yellowViewController.view.superview)
    {
        if (!self.yellowViewController)
            self.yellowViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Yellow"];
        [self.blueViewController.view removeFromSuperview];
        [self.view insertSubview:self.yellowViewController.view atIndex:0];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    }
    else
    {
        if (!self.blueViewController)
            self.blueViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
        [self.yellowViewController.view removeFromSuperview];
        [self.view insertSubview:self.blueViewController.view atIndex:0];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    }
    [UIView commitAnimations];
}
@end
View Code

XYZBlueViewController.m

//
//  XYZBlueViewController.m
//  MultiView
//
//  Created by Norcy on 14-7-23.
//  Copyright (c) 2014年 QQLive. All rights reserved.
//

#import "XYZBlueViewController.h"

@interface XYZBlueViewController ()

@end

@implementation XYZBlueViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

- (IBAction)blueBtnPressed:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Blue" delegate:self.view cancelButtonTitle:@"Got it" otherButtonTitles:nil];
    
    [alert show];
}
@end
View Code

XYZYellowViewController.m同理,所有头文件保持不变。

转载于:https://www.cnblogs.com/chenyg32/p/3863391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值