iOS: 필기 인 스 턴 스 를 배우 고 코드 로 보기 생 성 및 전환 을 제어 합 니 다.
4408 단어 학습 노트
//
// 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. 운행
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
STL 학습노트(6) 함수 객체모방 함수는 모두pass-by-value이다 함수 대상은 값에 따라 전달되고 값에 따라 되돌아오기 때문에 함수 대상은 가능한 한 작아야 한다(대상 복사 비용이 크다) 함수 f와 대상 x, x 대상에서 f를 호출하면:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.