#import "WatchViewController.h"
#import "HJHPullDownView.h"
@interface WatchViewController ()
@property (nonatomic ,weak) HJHPullDownView *pullDownView;
@property (nonatomic ,assign) BOOL isPop;
@end
@implementation WatchViewController
- (HJHPullDownView *)pullDownView
{
if (!_pullDownView)
{
_pullDownView = [HJHPullDownView showInView:self.view WithItems:@[@"1",@"2",@"3",@"4",@"5",@"6"] WithOriY:64];
}
return _pullDownView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下拉" style:UIBarButtonItemStylePlain target:self action:@selector(pullDown)];
}
- (void)pullDown
{
if (_isPop == NO)
{
[self pullDownView];
}
else
{
[self.pullDownView hide];
}
_isPop = !_isPop;
}
@end
自定義 下拉菜單
#import <UIKit/UIKit.h>
@interface HJHPullDownView : UIView
+ (instancetype)showInView:(UIView*)superView WithItems:(NSArray *)items WithOriY:(CGFloat)oriY;
- (void)hide;
@end
#import "HJHPullDownView.h"
// 每行幾個模型
#define HJHCols 3
@interface HJHPullDownView ()
// items 模型數(shù)組
@property (nonatomic ,strong) NSArray *items;
@property (nonatomic ,strong) NSMutableArray *btns;
@end
@implementation HJHPullDownView
- (NSMutableArray *)btns
{
if (!_btns)
{
_btns = [NSMutableArray array];
}
return _btns;
}
- (void)hide
{
[UIView animateWithDuration:0.5 animations:^{
self.transform = CGAffineTransformMakeTranslation(0, -self.bounds.size.height);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
+ (instancetype)showInView:(UIView*)superView WithItems:(NSArray *)items WithOriY:(CGFloat)oriY
{
NSUInteger count = items.count;
// 看看items 是不是3的倍數(shù)
if (count % 3 != 0)
{
// 拋出異常
NSException *excp = [NSException exceptionWithName:@"總數(shù)不符" reason:@"傳入的數(shù)組總數(shù)必須是3的倍數(shù)" userInfo:nil];
[excp raise];
}
NSUInteger rows = (count - 1) / HJHCols + 1;
CGFloat itemWH = Screen_Width / HJHCols;
CGFloat menmuH = rows * itemWH;
HJHPullDownView * menu = [[HJHPullDownView alloc]initWithFrame:CGRectMake(0, oriY, Screen_Width, menmuH)];
menu.items = items;
[menu setUpAllBtns];
[menu setUpAllDivideView];
menu.backgroundColor = [UIColor blackColor];
UIView *blackView = [[UIView alloc]initWithFrame:menu.frame];
blackView.backgroundColor = [UIColor blackColor];
[superView addSubview:blackView];
#warning 動畫
menu.transform = CGAffineTransformMakeTranslation(0, -menu.bounds.size.height);
[UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
menu.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[blackView removeFromSuperview];
}];
[superView addSubview:menu];
return menu;
}
- (void)setUpAllBtns
{
for (NSString * str in self.items)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:str forState:UIControlStateNormal];
[self addSubview:btn];
[self.btns addObject:btn];
}
}
- (void)setUpAllDivideView
{
CGFloat itemWH = Screen_Width / HJHCols;
// 豎 總列數(shù) - 1
for (int i = 0; i < HJHCols - 1; i++)
{
UIView *divideV = [[UIView alloc]init];
divideV.backgroundColor = [UIColor whiteColor];
divideV.frame = CGRectMake((i+1) * itemWH, 0, 1, self.bounds.size.height);
[self addSubview:divideV];
}
NSUInteger rows = (self.items.count - 1) / HJHCols +1;
// 橫 總行數(shù) - 1
for (int i = 0; i < rows - 1; i++)
{
UIView *divideV = [[UIView alloc]init];
divideV.backgroundColor = [UIColor whiteColor];
divideV.frame = CGRectMake(0, (i+1) * itemWH,self.bounds.size.width,1 );
[self addSubview:divideV];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
//布局所有的按鈕
NSUInteger count = self.items.count;
NSUInteger col = 0;
NSUInteger row = 0;
CGFloat x = 0;
CGFloat y = 0;
CGFloat itemWH = Screen_Width / HJHCols;
for (NSUInteger i =0; i<count; i++)
{
col = i % HJHCols;
row = i / HJHCols;
UIButton *btn = self.btns[i];
x = col * itemWH;
y = row * itemWH;
btn.frame = CGRectMake(x, y, itemWH, itemWH);
}
}
@end