IOS實例應用(按鈕下拉列表)


(一)實現功能

    單擊按鈕后可展開一個下拉列表,里面可以選擇,選擇后下拉列表收回;如果重新單擊按鈕后也可以收回;如果點擊按鈕列表區外面的時候也可以收回

 

(二)實現思路

    1.首先定義一個UIButton 作為主按鈕,一個UIView作為展開列表視圖。

    2.再定義幾個UIButton,作為展開的表象,用 addSubview:childrenButton,將子按鈕添加到列表視圖。

    3.將UIView設為隱藏。

    4.再定義一個透明的按鈕,充滿整個屏幕,加入到父視窗,並設為隱藏

    5.為主按鈕設置一個bool值,用於判斷當前列表是否已展開。

    6.在透明按鈕的點擊事件中,將列表視圖設為透明,同時更改當前狀態為未展開

    7.如果當前列表未展開:在主按鈕的點擊事件中,將UIView設為不隱藏(這樣感覺就是按了按鈕后彈出的view),將透明按鈕也設為不隱藏(注意層級關系:列表view應該在透明按鈕的上一層)這樣在view內點擊事件由子button獲取,view外的點擊事件由透明按鈕獲取

      如果當前列表展開:將view設為隱藏,將透明按鈕設為隱藏即可

 

(三)實現代碼

static NSString *temStrOrd;//序號的字符串
static UIView *listView;
static UIButton *bgBtn;
static BOOL isShow=false;
UIButton *btnAll;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 120, 50);
    [btn setTitle:@"點我展開" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor blueColor];
    [btn addTarget:self action:@selector(onBtnClicked) forControlEvents:UIControlEventAllTouchEvents];
    [self.view addSubview:btn];
    
    listView = [[UIView alloc] initWithFrame:CGRectMake(100, 152, 120, 100)];
    [listView setBackgroundColor:[UIColor greenColor]];
    listView.hidden = YES;
    [self.view addSubview:listView];
 
    UIButton *childBtn1=[[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(100, 160, 110, 40)];
    [childBtn1 setTitle:@"子按鈕1" forState:UIControlStateNormal];
    [childBtn1 setBackgroundColor:[UIColor whiteColor]];
    [childBtn1 addTarget:self action:@selector(onChildBtn1Clicked) forControlEvents:UIControlEventAllTouchEvents];
    [listView addSubview:childBtn1];
    
    UIButton *childBtn2=[[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(100, 210, 110, 40)];
    [childBtn2 setTitle:@"子按鈕2" forState:UIControlStateNormal];
    [childBtn2 setBackgroundColor:[UIColor whiteColor]];
    [childBtn2 addTarget:self action:@selector(onChildBtn2Clicked) forControlEvents:UIControlEventAllTouchEvents];
    [listView addSubview:childBtn2];
    
    UIButton *bgBtn =[[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(0, 0, rect.size.width   , rect.size.height)];
    bgBtn.hidden = YES;
    [bgBtn addTarget:self action:@selector(onBGBtnClicked) forControlEvents:UIControlEventAllTouchEvents];
    [self.view addSubview:bgBtn];
}

-(void)onBtnClicked
{
    if (isShow) {
        listView.hidden = YES;
        isShow=false;
        bgBtn.hidden =YES;
    }else{
        listView.hidden=NO;
        isShow = true;
        bgBtn.hidden =NO;
    }
}
-(void)onBGBtnClicked
{
    listView.hidden = YES;
    bgBtn.hidden = YES;
    isShow = false;
    NSLog(@"點擊了背景透明按鈕");
}
-(void)onChildBtn1Clicked
{
    NSLog(@"點擊了子按鈕1");
}
-(void)onChildBtn2Clicked
{
    NSLog(@"點擊了子按鈕2");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

 

(四)注意事項

    1.層疊結構 

 

   如但

 

 

 

 

     

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM