#import <UIKit/UIKit.h> @interface VCRoot : UIViewController //定義一個工具欄視圖 @property (retain,nonatomic) UIView* mToolBarView ; @end #import "VCRoot.h" #import "ChechBox.h" @interface VCRoot () @end @implementation VCRoot - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //建立工具欄視圖對象 CGFloat width = self.view.frame.size.width/5; _mToolBarView = [[UIView alloc] init] ; _mToolBarView.frame = CGRectMake(0, self.view.frame.size.height-44, 320, 44) ; //設置背景顏色 _mToolBarView.backgroundColor = [UIColor colorWithRed:0.3 green:0.0 blue:0.5 alpha:0.8f] ; [self.view addSubview:_mToolBarView] ; //按鈕到視圖中 UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ; btn.frame = CGRectMake(0, 2, width, 40) ; [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside] ; [btn setTitle:@"按鈕" forState:UIControlStateNormal] ; [_mToolBarView addSubview:btn] ; UILabel* label = [[UILabel alloc] init] ; label.frame = CGRectMake(width, 2, width, 40) ; label.text = @"標籤"; label.textAlignment = NSTextAlignmentCenter ; //標籤的手勢 UITapGestureRecognizer* tapLabel = [[UITapGestureRecognizer alloc] init] ; tapLabel.numberOfTapsRequired = 1 ; [tapLabel addTarget:self action:@selector(pressLabel)] ; //將標籤添加一個手勢 [label addGestureRecognizer:tapLabel] ; label.userInteractionEnabled = YES ; //自定義按鈕 UIButton* btnCustom = [UIButton buttonWithType:UIButtonTypeCustom] ; btnCustom.frame = CGRectMake(width*2, 2, width, 40) ; [btnCustom setImage:[UIImage imageNamed:@"iphone-on.png"] forState:UIControlStateNormal]; [btnCustom setImage:[UIImage imageNamed:@"iphone-off.png"] forState:UIControlStateHighlighted]; [btnCustom addTarget:self action:@selector(pressBtnCus) forControlEvents:UIControlEventTouchUpInside] ; [_mToolBarView addSubview:label] ; [_mToolBarView addSubview:btnCustom] ; UIImageView* iView = [[UIImageView alloc] init] ; iView.frame = CGRectMake(width*3, 2, width, 40) ; iView.image = [UIImage imageNamed:@"pumpkin"] ; //響應交互事件手勢事件 //imageView默認爲NO iView.userInteractionEnabled = YES ; UITapGestureRecognizer* tapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)] ; //兩次點擊 tapTwo.numberOfTapsRequired = 2 ; [iView addGestureRecognizer:tapTwo] ; [_mToolBarView addSubview:iView] ; self.navigationController.toolbar.hidden = YES ; ChechBox* check = [[ChechBox alloc] init] ; check.frame = CGRectMake(width*4+width/3, 2, width, 32) ; check.on = YES ; [check addTarget:self action:@selector(checkAct:) forControlEvents:UIControlEventValueChanged] ; [_mToolBarView addSubview:check] ; } -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { static BOOL isShow = NO ; [UIView beginAnimations:nil context:nil] ; [UIView setAnimationDuration:1] ; if (isShow == NO) { _mToolBarView.frame = CGRectMake(0, self.view.frame.size.height, 320, 44) ;//移動消失 // _mToolBarView.alpha = 0 ;//透明度漸變 } else { _mToolBarView.frame = CGRectMake(0, self.view.frame.size.height-44, 320, 44) ; // _mToolBarView.alpha = 1 ; } isShow = !isShow ; [UIView commitAnimations] ; } -(void) checkAct:(ChechBox*) checkBox { NSLog(@"check = %@",checkBox) ; } -(void) pressBtn { NSLog(@"按鈕按下!"); } -(void) pressBtnCus { NSLog(@"自定義按鈕按下!"); } -(void) pressLabel { NSLog(@"標籤按下!"); } -(void) tapImage:(UITapGestureRecognizer*) tapTwo { NSLog(@"圖片被雙擊!"); } @end
#import <UIKit/UIKit.h> //聲明一個選擇按鈕 //UIControl:能夠響應事件的控件對象 //UIControl:繼承於UIView @interface ChechBox : UIControl { //選中圖片對象 UIImage* _imageSelect ; //取消選中圖片對象 UIImage* _imageNoSelect ; //顯示到視圖上 UIImageView* _imageView ; //選中狀態 BOOL _on ; //事件目標對象,當前控件的事件函數的擁有者對象 //_target:事件函數聲明實如今對象中 //一般使用試圖控器對象做爲此值 id _target ; //事件函數對象(指針)聲明 //用來響應響應的事件處理 //經過外部來賦值 //實如今外部,_target對象中實現 SEL _selector ; } //定義on屬性 @property (assign,nonatomic) BOOL on ; //從新定義setOn函數 -(void) setOn:(BOOL)on ; //設置選中狀態圖片 -(void) setSelectedImage:(UIImage*) image ; //設置取消狀態的圖片 -(void) setNoSelectedImage:(UIImage*) image; //添加事件函數聲明 -(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents ; @end #import "ChechBox.h" @implementation ChechBox - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code //加載圖片對象 _imageSelect = [UIImage imageNamed:@"selected"] ; _imageNoSelect = [UIImage imageNamed:@"noselected"] ; //建立視圖 _imageView = [[UIImageView alloc] initWithImage:_imageNoSelect] ; _imageView.contentMode = UIViewContentModeCenter; //只能經過外部的參數設定位置 //不能設定控件的大小 _imageView.frame = CGRectMake(0, 0, 32, 32); _target = nil ; _selector = nil ; _on = NO ; [self addSubview:_imageView] ; } return self; } -(void) setSelectedImage:(UIImage *)image { _imageSelect = image ; _imageView.image = _imageNoSelect ; } -(void) setNoSelectedImage:(UIImage *)image { _imageNoSelect = image ; _imageView.image = _imageNoSelect ; } //添加設置事件函數 -(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents { // controlEvents == 0001100000 // UIControlEventValueChanged== 1000000000 // == 0000000000 if ((controlEvents &UIControlEventValueChanged) != 0) { _target = target ; _selector = action ; } } -(void) setOn:(BOOL)on { //狀態改變 if (_on != on) { _on = on ; if (_on == YES) { _imageView.image = _imageSelect ; } else if (_on == NO) { _imageView.image = _imageNoSelect ; } //respondsToSelector:target對象可否執行_selector函數 //功能:避免程序因爲沒有實現_selector,致使程序直接崩潰 //若是實現:返回值爲YES if ([_target respondsToSelector:_selector] == YES) { //經過target action模式執行事件函數 [_target performSelector:_selector withObject:self afterDelay:0] ; } } } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { self.on = !self.on ; } @end