CodingNet - Learning - 10

来源:互联网 发布:31周胎儿b超数据 编辑:程序博客网 时间:2024/06/08 01:27
接下来,我们来看看左上角的漏斗按钮的实现效果:
点击这个漏斗按钮,可以弹出一个朦胧感的一层视图,并且收缩感自如,舒适

首先需要看到这个类:XHRealTimeBlur

这个轮子的源码地址:https://github.com/xhzengAIB/XHRealTimeBlur

这个源码精巧,而且不难读懂,可以建议大家直接看懂源码才开始着手Coding Net的学习


我刨除了一些数据和刷新方面的操作,直接UI实现上

#define kfirstRowNum 3#import "PopFliterMenu.h"#import "XHRealTimeBlur.h"#import "pop.h"#import "LBMacroHeader.h"#import "LBCommonHeader.h"#import "Projects.h"@interface PopFliterMenu () <UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) NSMutableArray *items;@property (nonatomic, strong) XHRealTimeBlur *realTimeBlur;@property (nonatomic, strong) UITableView *tableview;@end@implementation PopFliterMenu- (id)initWithFrame:(CGRect)frame items:(NSArray *)items{        self = [super initWithFrame:frame];    if (self) {        self.items = @[@{@"all":@"2"},@{@"created":@"2"},@{@"joined":@"0"},@{@"watched":@"1"},@{@"stared":@"1"}].mutableCopy;        self.showStatus = FALSE;        [self setup];    }    return self;}- (void)setup{        self.backgroundColor = [UIColor clearColor];        _realTimeBlur = [[XHRealTimeBlur alloc] initWithFrame:self.bounds];    _realTimeBlur.blurStyle = XHBlurStyleTranslucentWhite;    _realTimeBlur.showDuration = 0.1;    _realTimeBlur.disMissDuration = 0.2;    typeof(self) __weak weakSelf = self;            /**     * * * * * * * * * * * * * * * * * * * * *     Pop 的这样相附性超强,可以作为一种相随的功能附加.     * * * * * * * * * * * * * * * * * * * * *     */        // 遮罩层出现前将tableview的 Alpha 值 设置从0.0 至 1.0    _realTimeBlur.willShowBlurViewcomplted = ^(void){            POPBasicAnimation *alphaAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];        alphaAnimation.fromValue = @0.0;        alphaAnimation.toValue   = @1.0;        alphaAnimation.duration  = 0.3f;        [weakSelf.tableview pop_addAnimation:alphaAnimation forKey:@"alphaAnimationS"];    };        _realTimeBlur.willDismissBlurViewCompleted = ^(void){                POPBasicAnimation *alphaAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];        alphaAnimation.fromValue = @1.0;        alphaAnimation.toValue   = @0.0;        alphaAnimation.duration  = 0.2f;        [weakSelf.tableview pop_addAnimation:alphaAnimation forKey:@"alphaAnimationE"];    };        _realTimeBlur.didDismissBlurViewCompleted = ^(BOOL finished){        [weakSelf removeFromSuperview];    };        _realTimeBlur.hasTapGestureEnable = YES;        _tableview = ({                UITableView *tableview=[[UITableView alloc] initWithFrame:self.bounds];        tableview.backgroundColor=[UIColor clearColor];        tableview.delegate=self;        tableview.dataSource=self;        [tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];        tableview.tableFooterView=[UIView new];        tableview.separatorStyle=UITableViewCellSeparatorStyleNone;        tableview;    });        [self addSubview:_tableview];    _tableview.contentInset = UIEdgeInsetsMake(15, 0, 0, 0);            int contentHeight = 320;        if ((kScreen_Height-64) > contentHeight)  {                UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + contentHeight, kScreen_Width,                                                                       kScreen_Height - 64 - contentHeight)];        contentView.backgroundColor = [UIColor clearColor];        [self addSubview:contentView];            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedContentView:)];        [contentView addGestureRecognizer:tapGestureRecognizer];    }}#pragma mark -- event & action- (void)showMenuAtView:(UIView *)containerView{        _showStatus = TRUE;    [containerView addSubview:self];    [_realTimeBlur showBlurViewAtView:self];    [_tableview reloadData];}- (void)dismissMenu{        // 预防层次变乱了.//    UIView *presentView=[[[UIApplication sharedApplication].keyWindow rootViewController] view];//    if ([[presentView.subviews firstObject] isMemberOfClass:NSClassFromString(@"RDVTabBar")]) {//        [presentView bringSubviewToFront:[presentView.subviews firstObject]];//    }    _showStatus=FALSE;    [_realTimeBlur disMiss];}#pragma mark - Gesture .- (void)didClickedContentView:(UIGestureRecognizer *)sender{        _closeBlock();}#pragma mark - Configration .- (NSInteger)convertToProjectType{        switch (_selectNum) {        case 0:            return ProjectsTypeAll;            break;        case 1:            return ProjectsTypeCreated;            break;        case 2:            return ProjectsTypeJoined;            break;        case 3:            return ProjectsTypeWatched;            break;        case 4:            return ProjectsTypeStared;            break;        default:            NSLog(@"type error");            return ProjectsTypeAll;            break;    }}//组装cell标题- (NSString*)formatTitleStr:(NSDictionary*)aDic{    NSString *keyStr=[[aDic allKeys] firstObject];    NSMutableString *convertStr=[NSMutableString new];    if ([keyStr isEqualToString:@"all"]) {        [convertStr appendString:@"全部项目"];    }else if ([keyStr isEqualToString:@"created"]) {        [convertStr appendString:@"我创建的"];    }else if ([keyStr isEqualToString:@"joined"]) {        [convertStr appendString:@"我参与的"];    }else if ([keyStr isEqualToString:@"watched"]) {        [convertStr appendString:@"我关注的"];    }else if ([keyStr isEqualToString:@"stared"]) {        [convertStr appendString:@"我收藏的"];    }else    {        NSLog(@"-------------error type:%@",keyStr);    }    if ([[aDic objectForKey:keyStr] length]>0) {        [convertStr appendString:[NSString stringWithFormat:@" (%@)",[aDic objectForKey:keyStr]]];    }    return [convertStr copy];}#pragma mark - UITableviewdelegate & datasource- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 3;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    switch (section) {        case 0:            return kfirstRowNum;            break;        case 1:            return 2+1;            break;        case 2:            return 1+1;            break;        default:            return 0;            break;    }}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];    cell.backgroundColor=[UIColor clearColor];    UILabel *titleLab=[[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 50)];    titleLab.font=[UIFont systemFontOfSize:15];    [cell.contentView addSubview:titleLab];        if (indexPath.section==0)    {        titleLab.textColor=(indexPath.row==_selectNum)?[UIColor colorWithHexString:@"0x3BBD79"]:[UIColor colorWithHexString:@"0x222222"];        titleLab.text=[self formatTitleStr:[_items objectAtIndex:indexPath.row]];    }    else if (indexPath.section==1)    {        if(indexPath.row==0){                        /**             * * * * * * * * * * * * * * * * * * * * *             这里分割线就是一行Cell             * * * * * * * * * * * * * * * * * * * * *             */                        [titleLab removeFromSuperview];            UIView *seperatorLine=[[UIView alloc] initWithFrame:CGRectMake(20, 15, self.bounds.size.width-40, 0.5)];            seperatorLine.backgroundColor=[UIColor colorWithHexString:@"0xcccccc"];            [cell.contentView addSubview:seperatorLine];            cell.selectionStyle=UITableViewCellSelectionStyleNone;        }else{            titleLab.textColor=(indexPath.row+kfirstRowNum==_selectNum)?[UIColor colorWithHexString:@"0x3BBD79"]:[UIColor colorWithHexString:@"0x222222"];            titleLab.text=[self formatTitleStr:[_items objectAtIndex:3+indexPath.row-1]];        }    }    else    {        if(indexPath.row==0){            [titleLab removeFromSuperview];            UIView *seperatorLine=[[UIView alloc] initWithFrame:CGRectMake(20, 15, self.bounds.size.width-40, 0.5)];            seperatorLine.backgroundColor=[UIColor colorWithHexString:@"0xcccccc"];            [cell.contentView addSubview:seperatorLine];            cell.selectionStyle=UITableViewCellSelectionStyleNone;        }else{            [titleLab setX:45];            titleLab.textColor=[UIColor colorWithHexString:@"0x727f8d"];            titleLab.text=@"项目广场";            UIImageView *projectSquareIcon=[[UIImageView alloc] initWithFrame:CGRectMake(20, 25-8, 16, 16)];            projectSquareIcon.image=[UIImage imageNamed:@"fliter_square"];            [cell.contentView addSubview:projectSquareIcon];        }    }    return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return ((indexPath.row==0)&&(indexPath.section==1))||((indexPath.row==0)&&(indexPath.section==2))?30.5:50;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.section==0) {        _selectNum=indexPath.row;        [self dismissMenu];        _clickBlock([self convertToProjectType]);    }else if (indexPath.section==1) {        if(indexPath.row==0){            _closeBlock();            return;        }        _selectNum=indexPath.row+kfirstRowNum-1;        [self dismissMenu];        _clickBlock([self convertToProjectType]);    }else{        if(indexPath.row==0){            _closeBlock();            return;        }        _clickBlock(1000);        _closeBlock();    }    [tableView deselectRowAtIndexPath:indexPath animated:YES];}@end


如何使用:

#import "FliterMenuTwoViewController.h"#import "PopFliterMenu.h"#import "LBCommonHeader.h"#import "LBMacroHeader.h"@interface FliterMenuTwoViewController ()@property (nonatomic, strong) UIButton *button;@property (nonatomic, strong) PopFliterMenu *myFliterMenu;@end@implementation FliterMenuTwoViewController- (void)viewDidLoad{    [super viewDidLoad];    [self.view setBackgroundColor:[UIColor lightGrayColor]];        _button = [UIButton buttonWithType:UIButtonTypeCustom];    [_button setFrame:CGRectMake(0, 0, 45, 45)];    [_button setBackgroundImage:[UIImage imageNamed:@"filtertBtn_normal_Nav"] forState:UIControlStateNormal];    [_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];    [_button setCenter:self.view.center];    [self.view addSubview:_button];        [self setupFliterMenu];}// 配置遮罩菜单 :- (void)setupFliterMenu{    _myFliterMenu = [[PopFliterMenu alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height) items:nil];        __weak typeof(self) weakSelf = self;    _myFliterMenu.clickBlock = ^(NSInteger pageIndex){                if (pageIndex == 1111) {        }else{            [weakSelf fliterBtnClose:TRUE];        }    };        _myFliterMenu.closeBlock = ^{        [weakSelf closeFliter];    };    }- (void)closeFliter{    if ([_myFliterMenu showStatus])    {        [_myFliterMenu dismissMenu];    }}- (void)fliterBtnClose:(BOOL)status{    [_button setImage:status?[UIImage imageNamed:@"filtertBtn_normal_Nav"]:[UIImage imageNamed:@"filterBtn_selected_Nav"] forState:UIControlStateNormal];}- (void)buttonClicked:(id)sender{    if (_myFliterMenu.showStatus)    {        [self fliterBtnClose:TRUE];        [_myFliterMenu dismissMenu];            }    else    {        [self fliterBtnClose:FALSE];        [_myFliterMenu showMenuAtView:self.view];                // [[[UIApplication sharedApplication].keyWindow rootViewController]        //  这个不能随便用呀!!        // 前面已经离开了introduction页面(原来的rootViewController),如果这个时候你还这样        // 就拿的不是当前的view啦.        //        UIView *presentView=[[[UIApplication sharedApplication].keyWindow rootViewController] view];//        [_myFliterMenu showMenuAtView:presentView];    }}@end


非常值得注意的一点是Facebook 的动画库 pop,  结合这个库让控件的相附性极强,可以与这个遮罩动画相随!





0 0