IPhone多视图切换

简介: 处理IPhone多个view切换是我们常遇到的问题,接下来有个比较实用的方法: 而且还附有创建空项目,内存告急处理和动画效果的实现! 具体步骤: 1.创建一个空的项目,然后添加一个ViewController,勾选上with xib文件,点击到xib文件...

处理IPhone多个view切换是我们常遇到的问题,接下来有个比较实用的方法:

而且还附有创建空项目,内存告急处理和动画效果的实现!

具体步骤:

1.创建一个空的项目,然后添加一个ViewController,勾选上with xib文件,点击到xib文件看看File's Owner属性是否是当前的ViewController,如果不是,手动填写一下,并将他的view属性拖向当前的view,view如果改成UIController,那么该view就具备事件

2.修改AppDelegate.h

@class DXWViewController;

@property (strong, nonatomic) DXWViewController *viewController;

3.修改AppDelegate.m

首先在dealloc中添加

[_viewController release];

接着是添加

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[DXWViewController alloc] initWithNibName:@"DXWViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];

    return YES;

}

上面是讲从0开始创建一个启动项目,下面是做一个多视图切换的功能

4.截下来是创建三个ViewController,分别是RedViewController(主view),YellowViewController,BlueViewController


主view下面是一个toolbar控件,其他两个view下面是在属性ButtonBar选择ToolBar实现

5.在主view中编码实现

ViewController.h:

#import <UIKit/UIKit.h>
#import "BlueViewController.h"
#import "YellowViewController.h"
@interface DXWViewController : UIViewController
@property(retain,nonatomic) BlueViewController *blueViewController;
@property(retain,nonatomic)YellowViewController *yellowViewController;
- (IBAction)blueClick:(id)sender;
- (IBAction)yellowClick:(id)sender;
- (IBAction)redClick:(id)sender;
- (IBAction)click:(id)sender;

@end

ViewController.m:

#import "DXWViewController.h"

@interface DXWViewController ()

@end

@implementation DXWViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}
//如果内存警告,则调用这个方法,可以让你自己清掉一些不需要用的内存
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSLog(@"内存告急");
    //如果内存警告则释放无用资源
    if (self.blueViewController.view.superview != nil) {
        [self.yellowViewController.view removeFromSuperview];
    }
    else if(self.yellowViewController.view.superview != nil){
        [self.blueViewController.view removeFromSuperview];
    }
}
- (IBAction)click:(id)sender {
   // NSLog(@"hello");
    [UIView beginAnimations:@"anim" context:nil];
    //一个动画的持续时间
    [UIView setAnimationDuration:1.5f];
    //一个动画的运动随时间快慢曲线
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //动画方式
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    //如果当前view的父view存在
    if (self.blueViewController.view.superview != nil) {
        //如果没创建则创建
        if (self.yellowViewController == nil) {
            self.yellowViewController = [[[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil] autorelease];
        }
        [self.blueViewController.view removeFromSuperview];
        [self.view insertSubview:self.yellowViewController.view atIndex:10];
    }
    
    else if (self.yellowViewController.view.superview != nil) {
        [UIView beginAnimations:@"anim" context:nil];
        //一个动画的持续时间
        [UIView setAnimationDuration:2.5f];
        //一个动画的运动随时间快慢曲线
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        //动画方式
        [UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
        //如果没创建则创建
        if (self.blueViewController == nil) {
            self.blueViewController = [[[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil] autorelease];
        }
        [self.yellowViewController.view removeFromSuperview];
        [self.view insertSubview:self.blueViewController.view atIndex:10];
        [UIView commitAnimations];
    }
    //动画效果启动
    [UIView commitAnimations];
}
- (IBAction)blueClick:(id)sender {
    NSLog(@"blue");
    //[self.blueViewController.view removeFromSuperview];
    self.blueViewController = [[[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil] autorelease];
    [self.view insertSubview:self.blueViewController.view atIndex:10];
}

- (IBAction)yellowClick:(id)sender {
    NSLog(@"yellow");
   //加上这句就可以红黄蓝任意点击
   //[self.yellowViewController.view removeFromSuperview];
    self.yellowViewController = [[[BlueViewController alloc] initWithNibName:@"YellowViewController" bundle:nil] autorelease];
    [self.view insertSubview:self.yellowViewController.view atIndex:10];
}
//主视图控制器
- (IBAction)redClick:(id)sender {
    NSLog(@"red");
    [self.yellowViewController.view removeFromSuperview];
    [self.blueViewController.view removeFromSuperview];
}

@end
旋转效果

相关文章
|
iOS开发
iphone绘图的几个基本概念CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)
我一般情况下不会使用interface builder去画界面,而是用纯代码去创建界面,不是装B,而是刚从vi转到xcode不久,不太习惯interface builder而已。当然如果需要我也会使用它。
877 0
|
26天前
|
编解码 测试技术 iOS开发
iPhone 屏幕尺寸和开发适配
【10月更文挑战第23天】iPhone 的屏幕尺寸变化给开发者带来了一定的挑战,但也为创新提供了机遇。通过深入了解不同屏幕尺寸的特点,遵循适配原则和策略,运用合适的技巧和方法,我们能够为用户提供在不同 iPhone 机型上都具有良好体验的应用。在未来,随着技术的不断进步,我们还需要持续学习和适应,以满足用户对优质应用体验的不断追求。
|
26天前
|
编解码 iOS开发 UED
响应式设计在 iPhone 开发适配中的具体应用
【10月更文挑战第23天】响应式设计在 iPhone 开发适配中扮演着至关重要的角色,它能够帮助我们打造出适应不同屏幕尺寸和用户需求的高质量应用。通过合理运用响应式设计的原则和方法,我们可以在提供良好用户体验的同时,提高开发效率和应用的可维护性。
|
3月前
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
|
Shell iOS开发
iOS逆向:tweak开发教程(iPhone/tool)
iOS逆向:tweak开发教程(iPhone/tool)
1188 0
iOS逆向:tweak开发教程(iPhone/tool)