#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 模型数组
@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的倍数
if (count % 3 != 0)
{
// 抛出异常
NSException *excp = [NSException exceptionWithName:@"总数不符" reason:@"传入的数组总数必须是3的倍数" 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;
// 竖 总列数 - 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;
// 横 总行数 - 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
973B27C2-DAD4-4D81-9C90-F0EB84C68718.png
46246F02-B651-4094-BBFB-4C93C3FA1509.png
网友评论