導航條的下拉動畫

#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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容