思路: 懶加載 一個View 寂屏,在View 上創(chuàng)建N個按鈕,點擊 按鈕 切換 選中 文字
遇到問題: view 可以消失侠坎,但是 上面的按鈕不會變
原因:可能是 背景的 frame 變了 但是 控件的沒有變
解決 辦法 : View.clipsToBounds = YES; 超出邊緣裁剪
功能代碼 :
第一步: 懶加載 一個 View 創(chuàng)建 N 個按鈕
pragma mark - 懶加載
-
(UIView *)chooseView{
if (!_chooseView) {
_chooseView = [[UIView alloc] initWithFrame:CGRectMake(JKScreenWidth - 107, 64 + 32, 100, 0)];
_chooseView.clipsToBounds = YES;
[self.view addSubview:_chooseView];
_chooseView.backgroundColor = JKRandomColor;CGFloat y = 0; for (int i = 0 ; i < 4; i ++) { y = i == 0 ? y : y + 120 /4 ; UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, y, 100, 120/4)]; button.tag = 100 + i; [button setTitle:self.titles[i] forState:UIControlStateNormal]; button.titleLabel.textColor = [UIColor colorWithHexString:@"#333333"]; button.titleLabel.font = [UIFont jk_systemFontOfPxSize:24]; [button addTarget:self action:@selector(chooseButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [_chooseView addSubview:button]; }
}
return _chooseView;
}
第二步: 創(chuàng)建需要的文字數(shù)組(懶加載)
-
(NSMutableArray *)titles{
if (!_titles) {
_titles = [[NSMutableArray alloc] initWithArray:@[@"所有",@"管理員A",@"管理員B",@"其他"]];
}
return _titles;
}
第三步 : 彈出 View 動畫
//篩選按鈕點擊
-
(void)screenOutButtonClick:(UIButton *)sender{
if (sender.isSelected) {
sender.selected = NO; [UIView animateWithDuration:0.25 animations:^{ self.chooseView.height = 0; }];
}else{
sender.selected = YES; [UIView animateWithDuration:0.25 animations:^{ self.chooseView.height = 120; }];
}
}
第四步: 選擇文字后 收起
pragma mark - 類型按鈕點擊
-
(void)chooseButtonClick:(UIButton *)sender{
[_screenOutButton setTitle:self.titles[sender.tag - 100] forState:UIControlStateNormal];
[UIView animateWithDuration:0.25 animations:^{
self.chooseView.height = 0;
} completion:^(BOOL finished) {
_screenOutButton.selected = NO;
}];
}