手写视图
2012-02-01 23:23
162 查看
纯代码写视图的话,要先引入画图框架,#import <QuartzCore/QuartzCore.h>
PopPanelView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
//CGRect结构在屏幕上定义了一个矩形。
@interface PopPanelView : UIView{
CGRect rectForOpen;
CGRect rectForClose;
BOOL isOpen;
}
@property BOOL isOpen;
-(void) initForButtons;
-(void) buttonPressed:(UIButton *) button;
-(void) viewOpen;
-(void) viewClose;
@end
PopPanelView.m
#import "PopPanelView.h"
@implementation PopPanelView
@synthesize isOpen;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
isOpen = YES;
rectForOpen = self.frame;
rectForClose = CGRectMake(rectForOpen.origin.x, rectForOpen.origin.y, rectForOpen.size.width, 0);
[self setBackgroundColor:[UIColor lightGrayColor]];
[self.layer setCornerRadius:10.0];//设置边角圆滑度
[self setClipsToBounds:YES];
[self initForButtons];
}
return self;
}
CGFloat btx = 20;
CGFloat bty = 50;
CGFloat btwidth = 60;
CGFloat btheight = 30;
//初始化该view上的button
-(void)initForButtons{
NSString *buttonName = nil;
UIButton *button = nil;
for (int i = 0; i < 5; i++) {
buttonName = [[NSString alloc] initWithFormat:@"bt0%d",i];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btx, bty, btwidth, btheight);
[button setTitle:buttonName forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
bty += 40;
//[buttonName release];
}
bty = 50;//还原
}
-(void)buttonPressed:(UIButton *)button{
int tag = button.tag;
NSString *alertMessage = [[NSString alloc] initWithFormat:@" %d 号按钮被按下了...",tag];
UIAlertView *alert = nil;
alert = [[UIAlertView alloc] initWithTitle:nil message:alertMessage delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alert show];
//[alertMessage release];
}
-(void)viewOpen{
isOpen = YES;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDuration:UIViewAnimationCurveLinear];
[self setFrame:rectForOpen];
[UIView commitAnimations];
}
-(void)viewClose{
isOpen = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDuration:UIViewAnimationCurveLinear];
[self setFrame:rectForClose];
[UIView commitAnimations];
}
-(void)drawRect:(CGRect)rect{
//如果要把添加button的这两行代码放到这里,那么drawRect方法只能画出按钮的边框,字体显示不出来
/*
[self addSubView:self.button];
[self addSubView:self.button1];
*/
}
-(void) dealloc{
//[super dealloc];
}
@end
/*入口视图*/
RootViewController.h
#import <UIKit/UIKit.h>
@class PopPanelView;//引入纯代码写的视图类
@interface RootViewController : UIViewController{
PopPanelView *ppv;
UIButton *mainButton;
}
//定义两个插座变量
@property(nonatomic,retain) IBOutlet PopPanelView *ppv;
@property(nonatomic,retain) IBOutlet UIButton *mainButton;
//操作关闭序列按钮和打开的操作方法
-(void)doPopPanelView;
@end
RootViewController.m
#import "RootViewController.h"
#import "PopPanelView.h"
@implementation RootViewController
@synthesize ppv,mainButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
//Implement loadView to create a view hierarchy programmatically, withoutusing a nib.
-(void)loadView{//该方法自动加载纯代码写的视图,而不是解析nib文件
[super loadView];
mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setFrame:CGRectMake(50, 18, 100, 35)];
[mainButton addTarget:self action:@selector(doPopPanelView) forControlEvents:UIControlEventTouchDown];//对序列按钮的操作,指向的方法是doPopPanelView
[mainButton setTitle:@"close" forState:UIControlStateNormal];
[self.view addSubview:mainButton];//加载该congtroller视图中手写创建的打开按钮
ppv = [[PopPanelView alloc] initWithFrame:CGRectMake(50, 50, 100, 300)];
[self.view addSubview:ppv];//加载另一个按钮序列的视图
}
-(void)doPopPanelView{
if (ppv.isOpen) {
[ppv viewClose];
[mainButton setTitle:@"open" forState:UIControlStateNormal];
}else{
[ppv viewOpen];
[mainButton setTitle:@"close" forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc{
//[mainButton release];
//[ppv release];
//[super dealloc];
}
@end
//运行的window
PopPanelAppDelegate.h
#import <UIKit/UIKit.h>
@class RootViewController;
@interface PopPanelAppDelegate : UIResponder <UIApplicationDelegate>{
UIWindow *window;
RootViewController *rootViewController;//入口的controller
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@end
PopPanelAppDelegate.m
#import "PopPanelAppDelegate.h"
#import "RootViewController.h"
@implementation PopPanelAppDelegate
@synthesize window = _window;
@synthesize rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
rootViewController = [[RootViewController alloc] init];
[self.window addSubview:rootViewController.view];//window加载入口的controller
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to
the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
@end
PopPanelView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
//CGRect结构在屏幕上定义了一个矩形。
@interface PopPanelView : UIView{
CGRect rectForOpen;
CGRect rectForClose;
BOOL isOpen;
}
@property BOOL isOpen;
-(void) initForButtons;
-(void) buttonPressed:(UIButton *) button;
-(void) viewOpen;
-(void) viewClose;
@end
PopPanelView.m
#import "PopPanelView.h"
@implementation PopPanelView
@synthesize isOpen;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
isOpen = YES;
rectForOpen = self.frame;
rectForClose = CGRectMake(rectForOpen.origin.x, rectForOpen.origin.y, rectForOpen.size.width, 0);
[self setBackgroundColor:[UIColor lightGrayColor]];
[self.layer setCornerRadius:10.0];//设置边角圆滑度
[self setClipsToBounds:YES];
[self initForButtons];
}
return self;
}
CGFloat btx = 20;
CGFloat bty = 50;
CGFloat btwidth = 60;
CGFloat btheight = 30;
//初始化该view上的button
-(void)initForButtons{
NSString *buttonName = nil;
UIButton *button = nil;
for (int i = 0; i < 5; i++) {
buttonName = [[NSString alloc] initWithFormat:@"bt0%d",i];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btx, bty, btwidth, btheight);
[button setTitle:buttonName forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
bty += 40;
//[buttonName release];
}
bty = 50;//还原
}
-(void)buttonPressed:(UIButton *)button{
int tag = button.tag;
NSString *alertMessage = [[NSString alloc] initWithFormat:@" %d 号按钮被按下了...",tag];
UIAlertView *alert = nil;
alert = [[UIAlertView alloc] initWithTitle:nil message:alertMessage delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alert show];
//[alertMessage release];
}
-(void)viewOpen{
isOpen = YES;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDuration:UIViewAnimationCurveLinear];
[self setFrame:rectForOpen];
[UIView commitAnimations];
}
-(void)viewClose{
isOpen = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDuration:UIViewAnimationCurveLinear];
[self setFrame:rectForClose];
[UIView commitAnimations];
}
-(void)drawRect:(CGRect)rect{
//如果要把添加button的这两行代码放到这里,那么drawRect方法只能画出按钮的边框,字体显示不出来
/*
[self addSubView:self.button];
[self addSubView:self.button1];
*/
}
-(void) dealloc{
//[super dealloc];
}
@end
/*入口视图*/
RootViewController.h
#import <UIKit/UIKit.h>
@class PopPanelView;//引入纯代码写的视图类
@interface RootViewController : UIViewController{
PopPanelView *ppv;
UIButton *mainButton;
}
//定义两个插座变量
@property(nonatomic,retain) IBOutlet PopPanelView *ppv;
@property(nonatomic,retain) IBOutlet UIButton *mainButton;
//操作关闭序列按钮和打开的操作方法
-(void)doPopPanelView;
@end
RootViewController.m
#import "RootViewController.h"
#import "PopPanelView.h"
@implementation RootViewController
@synthesize ppv,mainButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
//Implement loadView to create a view hierarchy programmatically, withoutusing a nib.
-(void)loadView{//该方法自动加载纯代码写的视图,而不是解析nib文件
[super loadView];
mainButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mainButton setFrame:CGRectMake(50, 18, 100, 35)];
[mainButton addTarget:self action:@selector(doPopPanelView) forControlEvents:UIControlEventTouchDown];//对序列按钮的操作,指向的方法是doPopPanelView
[mainButton setTitle:@"close" forState:UIControlStateNormal];
[self.view addSubview:mainButton];//加载该congtroller视图中手写创建的打开按钮
ppv = [[PopPanelView alloc] initWithFrame:CGRectMake(50, 50, 100, 300)];
[self.view addSubview:ppv];//加载另一个按钮序列的视图
}
-(void)doPopPanelView{
if (ppv.isOpen) {
[ppv viewClose];
[mainButton setTitle:@"open" forState:UIControlStateNormal];
}else{
[ppv viewOpen];
[mainButton setTitle:@"close" forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc{
//[mainButton release];
//[ppv release];
//[super dealloc];
}
@end
//运行的window
PopPanelAppDelegate.h
#import <UIKit/UIKit.h>
@class RootViewController;
@interface PopPanelAppDelegate : UIResponder <UIApplicationDelegate>{
UIWindow *window;
RootViewController *rootViewController;//入口的controller
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@end
PopPanelAppDelegate.m
#import "PopPanelAppDelegate.h"
#import "RootViewController.h"
@implementation PopPanelAppDelegate
@synthesize window = _window;
@synthesize rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
rootViewController = [[RootViewController alloc] init];
[self.window addSubview:rootViewController.view];//window加载入口的controller
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to
the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
@end
相关文章推荐
- 题目:给最外层的rootview,把这个根视图下的全部button背景设置成红色,手写代码,不许用递归
- 动手写一个快速集成网易新闻,腾讯视频,头条首页的ScrollPageView,显示滚动视图
- android 手写签名、画板(自定义视图)的使用
- 安卓自定义视图 - 手写签名的实现
- 手写ButterKnife视图注入,解决findViewById
- uicollectionview 流视图手写实现
- 动手写一个快速集成网易新闻,腾讯视频,头条首页的ScrollPageView,显示滚动视图
- HOWTO:如何在Redistributables视图中显示所有的水晶报表(Crystal Reports)Merge Module
- 项目经验——Sql server 中建立索引视图
- Spring excel视图导出时进度条设计
- iOS滚动视图UIScrollView使用方法
- ViewAnimator控制视图的隐藏与显示
- TensorFlow学习笔记之源码分析(2)----手写数字识别mnist example
- Hive的数据模型之视图
- VS 2008中的“设计”视图打不开
- 视图解析器
- 动手写led字符驱动模块
- springMVC 自定义视图及解析器
- iOS开发之collectionView实现无限轮播视图
- SpringMVC入门之八:使用Apache Tiles视图定义布局