您的位置:首页 > 移动开发

手写视图

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息