手写视图

纯代码写视图的话,要先引入画图框架,#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值