• iOS: 学习笔记实例, 用代码控制视图创建与切换


    1. 创建iOS, Single View Application.
    2. 修改YYViewController.m

    //
    //  YYViewController.m
    //  DynamicViewDemo
    //
    //  Created by yao_yu on 14-5-28.
    //  Copyright (c) 2014年 yao_yu. All rights reserved.
    //
    
    #import "YYViewController.h"
    
    @interface YYViewController ()
    
    @property(nonatomic, strong) UIView *body;
    
    @property (nonatomic, strong) UIView *currentPage;
    
    @end
    
    const CGFloat HEIGHTEDGE = 40;
    
    @implementation YYViewController
    
    -(void)createBodyView
    {
        CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        CGRect parentFrame = self.view.frame;
        CGRect bodyFrame = CGRectMake(parentFrame.origin.x, parentFrame.origin.y + statusBarFrame.size.height + HEIGHTEDGE, parentFrame.size.width, parentFrame.size.height -  statusBarFrame.size.height - HEIGHTEDGE);
        self.body = [[UIView alloc] initWithFrame:bodyFrame];
        [self.body setBackgroundColor:[UIColor blackColor]];
        [self.view addSubview:self.body];
    }
    
    -(void)createCommands
    {
        CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        CGRect parentFrame = self.view.frame;
        CGRect bodyFrame = CGRectMake(parentFrame.origin.x, parentFrame.origin.y + statusBarFrame.size.height, parentFrame.size.width, HEIGHTEDGE);
        UIView *view = [[UIView alloc] initWithFrame:bodyFrame];
        [view setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:view];
        
        //添加命令按钮
        const CGFloat COMMANDWIDTH = 50;
        const CGFloat COMMANDHEIGHT = 30;
        CGFloat midx = bodyFrame.size.width/2;
        CGFloat midy = bodyFrame.size.height/2;
        CGRect rect = CGRectMake(midx - COMMANDWIDTH/2, midy - COMMANDHEIGHT/2, COMMANDWIDTH, COMMANDHEIGHT);
        UIButton *btnPage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnPage1 setBackgroundColor:[UIColor clearColor]];
        [btnPage1 setTitle:@"第1页" forState:UIControlStateNormal];
        rect.origin.x -= COMMANDWIDTH + 20;
        btnPage1.frame = rect;
        [btnPage1 addTarget:self action:@selector(onShowPage1:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnPage1];
        
        btnPage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnPage1 setTitle:@"第2页" forState:UIControlStateNormal];
        rect.origin.x += COMMANDWIDTH + 20;
        btnPage1.frame = rect;
        [btnPage1 addTarget:self action:@selector(onShowPage2:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnPage1];
        
        btnPage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnPage1 setTitle:@"第3页" forState:UIControlStateNormal];
        rect.origin.x += COMMANDWIDTH + 20;
        btnPage1.frame = rect;
        [btnPage1 addTarget:self action:@selector(onShowPage3:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnPage1];
        
    }
    
    -(void) onShowPage1:(id)sender
    {
        [self clearCurrentPage];
        CGRect frame = CGRectMake(0, 0, self.body.frame.size.width, self.body.frame.size.height);
        self.currentPage = [[UIView alloc] initWithFrame: frame];
        [self.currentPage setBackgroundColor:[UIColor blueColor]];
        [self.body addSubview:self.currentPage];
    }
    
    -(void) onShowPage2:(id)sender
    {
        [self clearCurrentPage];
        CGRect frame = CGRectMake(0, 0, self.body.frame.size.width, self.body.frame.size.height);
        self.currentPage = [[UIView alloc] initWithFrame: frame];
        [self.currentPage setBackgroundColor:[UIColor yellowColor]];
        [self.body addSubview:self.currentPage];
    }
    
    
    -(void) onShowPage3:(id)sender
    {
        [self clearCurrentPage];
        CGRect frame = CGRectMake(0, 0, self.body.frame.size.width, self.body.frame.size.height);
        self.currentPage = [[UIView alloc] initWithFrame: frame];
        [self.currentPage setBackgroundColor:[UIColor greenColor]];
        [self.body addSubview:self.currentPage];
    }
    
    -(void) clearCurrentPage
    {
        if (self.currentPage)
            [self.currentPage removeFromSuperview];
        self.currentPage = nil;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.currentPage = nil;
        [self createBodyView];
        [self createCommands];
    	// Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

     3.运行

  • 相关阅读:
    K8S--架构及基本概念
    虚拟化及云原生的一些概念
    JPA事务中的异常最后不也抛出了,为什么没被catch到而导致回滚?
    OpenFaaS实战之六:of-watchdog(为性能而生)
    OpenFaaS实战之五:大话watchdog
    OpenFaaS实战之四:模板操作(template)
    OpenFaaS实战之三:Java函数
    OpenFaaS实战之二:函数入门
    OpenFaaS实战之一:部署
    Python
  • 原文地址:https://www.cnblogs.com/yaoyu126/p/3757488.html
Copyright © 2020-2023  润新知