一、一个继承自UIViewController的类
#import <UIKit/UIKit.h>
@class PopupPanelView;
@interface RootViewController : UIViewController {
PopupPanelView *ppv;
UIButton *mainButton;
}
@property(nonatomic,retain)IBOutletPopupPanelView *ppv;
@property(nonatomic,retain)IBOutletUIButton *mainButton;
-(void)doPopupPanelView;
@end
#import "RootViewController.h"
#import "PopupPanelView.h"
@implementation RootViewController
@synthesize ppv,mainButton;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
[superloadView];
mainButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[mainButtonsetFrame:CGRectMake(50,18,100,35)];
[mainButtonaddTarget:selfaction:@selector(doPopupPanelView)forControlEvents:UIControlEventTouchDown];
[mainButtonsetTitle:@"close"forState:UIControlStateNormal];
[self.viewaddSubview:mainButton];
ppv = [[PopupPanelViewalloc]initWithFrame:CGRectMake(50,50,100,300)];
[self.viewaddSubview:ppv];
}
-(void)doPopupPanelView{
if(ppv.isOpen){
[ppvviewClose];
[mainButtonsetTitle:@"open"forState:UIControlStateNormal];
}else{
[ppvviewOpen];
[mainButtonsetTitle:@"close"forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mainButton release];
[ppvrelease];
[super dealloc];
}
@end
二、一个继承自UIView的类。
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface PopupPanelView : UIView {
CGRect rectForOpen;
CGRect rectForClose;
BOOL isOpen;
}
@property BOOL isOpen;
-(void)initForButtons;
-(void)buttonPressed:(UIButton *)button;
-(void)viewOpen;
-(void)viewClose;
@end
#import "PopupPanelView.h"
@implementation PopupPanelView
@synthesize isOpen;
- (id)initWithFrame:(CGRect)frame {
if (self = [superinitWithFrame:frame]) {
// Initialization code
isOpen = YES;
// rectForOpen = self.frame;
rectForOpen = CGRectMake(50,90,200, 120);
rectForClose =CGRectMake(rectForOpen.origin.x ,rectForOpen.origin.y,rectForOpen.size.width,0);
[selfsetBackgroundColor:[UIColorlightGrayColor]];
[self.layersetCornerRadius:10.0]; //设置边角圆滑度
[selfsetClipsToBounds:YES]; //content and subviews are clipped to the bounds of the view
[selfinitForButtons];
}
return self;
}
CGFloat btx = 20;
CGFloat bty = 50;
CGFloat btwidth = 60;
CGFloat byheight = 30;
//初始化该view上的button
-(void)initForButtons{
NSString *buttonName = nil;
UIButton *button = nil;
for(int i=0; i<5; i++){
buttonName = [[NSStringalloc]initWithFormat:@"bt0%d",i];
button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btx, bty, btwidth, byheight);
[button setTitle:buttonNameforState:UIControlStateNormal];
button.tag = i;
[button addTarget:selfaction:@selector(buttonPressed:)forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
bty += 40;
[buttonName release];
}
bty = 50;//还原
}
-(void)buttonPressed:(UIButton *)button{
int tag = button.tag;
NSString *alertMessage = [[NSStringalloc]initWithFormat:@" %d号按钮被按下了...",tag];
UIAlertView *alert = nil;
alert = [[UIAlertViewalloc]initWithTitle:nilmessage:alertMessagedelegate:selfcancelButtonTitle:@"cancel"otherButtonTitles:nil,nil];
[alert show];
[alertMessage release];
}
-(void)viewOpen{
isOpen = YES;
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.4];
[UIViewsetAnimationCurve:UIViewAnimationCurveLinear];
[selfsetFrame:rectForOpen];
[UIViewcommitAnimations];
}
-(void)viewClose{
isOpen = NO;
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.4];
[UIViewsetAnimationCurve:UIViewAnimationCurveLinear];
[selfsetFrame:rectForClose];
[UIViewcommitAnimations];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
//如果要把添加button的这两行代码放到这里,那么drawRect方法只能画出按钮的边框,字体显示不出来
/*
[self addSubview:self.button];
[self addSubview:self.button1];
*/
}
- (void)dealloc {
[super dealloc];
}
@end
两个类 做适当修改