iOS 顶部几个按钮切换界面代码

//

//  MainRankView.h

//  HengTaiXinGolf

//

//  Created by 欧阳荣 on 15/10/12.

//  Copyright © 2015 HengTaiXinGolf. All rights reserved.

//


#import <UIKit/UIKit.h>




//Block 的回调

typedef void (^sendViewBlock)(id);


@interface MainRankView : UIView



@property (nonatomic,copy)sendViewBlock sendBlock;



//顶部分类Btn

@property (nonatomic) NSMutableArray *typeArr;


@property (nonatomic,strong) UIScrollView *topScr;


@property (nonatomic,strong) UIImageView *imageView;

//表视图滚动视图

@property (nonatomic,strong) UIScrollView *typeScr;



@property (nonatomic,strong) UITableView *rankTabView;












@end



//

//  MainRankView.m

//  HengTaiXinGolf

//

//  Created by 欧阳荣 on 15/10/12.

//  Copyright © 2015 HengTaiXinGolf. All rights reserved.

//


#import "MainRankView.h"


@implementation MainRankView

//{

//    NSMutableArray * _typeArr;

//

//}



-(instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        //视图数组实例化

        _typeArr = [NSMutableArray array];

        

        self.backgroundColor = [UIColor whiteColor];

        [self creatUI];

    }

    return self;

}


-(void)creatUI

{

    //创建抬头排名部分

    self.topScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 35)];

    

    

    

    self.topScr.contentSize = CGSizeMake(IPHONE_WIDTH, 0);

    self.topScr.showsHorizontalScrollIndicator = NO;

    self.topScr.showsVerticalScrollIndicator = NO;

    self.topScr.bounces = NO;

    NSArray *nameArr = @[@"最新",@"每月",@"足迹"];

    for (NSInteger i=0; i<nameArr.count; i++)

    {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(i * IPHONE_WIDTH/3, 0, IPHONE_WIDTH/3, 30);

        btn.tag = 100+i;

        [btn setTitle:nameArr[i] forState:UIControlStateNormal];

        

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor colorWithRed:0/255.0 green:159/255.0 blue:251/255.0 alpha:1.0] forState:UIControlStateSelected];

        [btn setBackgroundImage:[UIImage imageNamed:@"seleted"] forState:UIControlStateSelected];

        btn.titleLabel.font = [UIFont systemFontOfSize:15];

        [btn addTarget:self action:@selector(tipBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.topScr addSubview:btn];

    }

    

    [self addSubview:self.topScr];

    

    

    //排名下面部分显示的Scr

    self.typeScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 35, IPHONE_WIDTH, IPHONE_HEIGHT)];

    self.typeScr.contentSize = CGSizeMake(IPHONE_WIDTH*3, 0);

    self.typeScr.pagingEnabled = YES;

    

    NSInteger i=0;

    UIImageView *ImgeView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 34)];

    ImgeView.center=CGPointMake(IPHONE_WIDTH/6, 17);

    self.imageView=ImgeView;

    ImgeView.image=[UIImage imageNamed:@"dh1.png"];

//    ImgeView.backgroundColor=[UIColor redColor];

    [self.topScr addSubview:ImgeView];

    

    for (i=0; i<3; i++)

    {

        // 其他排名的视图

            self.rankTabView = [[UITableView alloc]initWithFrame:CGRectMake(IPHONE_WIDTH *i , 0, IPHONE_WIDTH, IPHONE_HEIGHT - 30 - 49 - 15)];

            self.rankTabView.tag = 1000 + i;

            //让表视图滑动取消

            //self.rankTabView.tabView.scrollEnabled = NO;

            self.rankTabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];


            [_typeArr addObject:self.rankTabView];

            [self.typeScr addSubview:self.rankTabView];

             

    }

    UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(0, 34, IPHONE_WIDTH, 0.5)];

    lineView.backgroundColor=kColor(170, 201, 184);

    [self addSubview:lineView];

    

    

    [self addSubview:self.typeScr];

}

-(void)tipBtnAction:(id)sender

{

    

    if (self.sendBlock)

    {

        self.sendBlock(sender);

    }

}





















/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end




==========================================在视图控制器中添加如下代码============================================


- (void)creatMainView

{

    _mainView = [[MainRankView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT)];

    _mainView.topScr.delegate = self;

    _mainView.typeScr.delegate = self;

    int R = 0;

    for (UITableView *rankView in _mainView.typeArr) {

        rankView.delegate = self;

        rankView.dataSource = self;

        //刷新代理

        if (R == 0) {

            [rankView registerClass:[RankingCell class] forCellReuseIdentifier:CELL_IDENTIFIER];

            self.headerView1 = [[MJRefreshHeaderView alloc]initWithScrollView:rankView];

            self.footerView1 = [[MJRefreshFooterView alloc]initWithScrollView:rankView];


        }else if (R == 1){

            [rankView registerClass:[monthTableViewCell class] forCellReuseIdentifier:CELL_MONTH];

            self.headerView2 = [[MJRefreshHeaderView alloc]initWithScrollView:rankView];

            self.footerView2 = [[MJRefreshFooterView alloc]initWithScrollView:rankView];

        }else if (R == 2){

            [rankView registerClass:[footprintTableViewCell class] forCellReuseIdentifier:CELL_FOOTPRINT];

            self.headerView3 = [[MJRefreshHeaderView alloc]initWithScrollView:rankView];

            self.footerView3 = [[MJRefreshFooterView alloc]initWithScrollView:rankView];

        }

        R ++;

    }

    

    self.headerView1.delegate = self;

    self.footerView1.delegate = self;

    self.headerView2.delegate = self;

    self.footerView2.delegate = self;

    self.headerView3.delegate = self;

    self.footerView3.delegate = self;

    

    //注意此处的Block的循环引用

    __weak RankTypeViewController *vc = self;

    _mainView.sendBlock = ^(UIButton *sender)

    {

        [vc homeTopBtnAction:sender];

    };

    

    [self.view addSubview:_mainView];

    

    _oldBtn = (UIButton *)[vc.view viewWithTag:100];

    _oldBtn.selected = YES;

    

}


//顶部Scr的点击事件

-(void)homeTopBtnAction:(UIButton *)sender

{

    _oldBtn.selected=NO;

    

    sender.selected=YES;

    _oldBtn=sender;

    

    switch (sender.tag)

    {

        case 100:

        {

            _mainView.typeScr.contentOffset = CGPointMake(IPHONE_WIDTH*0, 0);

            _mainView.imageView.center=CGPointMake(IPHONE_WIDTH/6, 17);

            if (_newRanArr.count== 0)

            {

                

                NSLog(@"获取最新排名数据");

                [self geturl:KgetNewRanking];

            }

        }

            break;

        case 101:

        {

            _mainView.typeScr.contentOffset = CGPointMake(IPHONE_WIDTH*1, 0);

             _mainView.imageView.center=CGPointMake(IPHONE_WIDTH/2, 17);

            if (_monthArr.count== 0)

            {

                NSLog(@"获取每月排名数据");

                

                [self geturl:KgetMonthRanking];

            }

        }

            break;

        case 102:

        {

            _mainView.typeScr.contentOffset = CGPointMake(IPHONE_WIDTH*2, 0);

             _mainView.imageView.center=CGPointMake(IPHONE_WIDTH/3+IPHONE_WIDTH/2, 17);

            if (_allArr.count== 0)

            {

                NSLog(@"获取整体排名数据");

                

                [self geturl:KgetFootprintRanking];

            }

        }

            break;

            

        default:

            break;

    }

}


============================让图片高亮和正常显示的方法===================================

  case 7:

        {

            NSLog(@"7");

            if (btn_7 == NO) {

                

                if (numSeleted_s == 3) {

                    UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您最多只可以选择3人进行斗地主游戏!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                    [alerView show];

                    return;

                }


                

                [_selectedNumArr addObject:_listNameArr[7]];

                for (UIImageView *imgView in _imgViewArr) {

                    if (imgView.tag == 666 + 7) {

                        imgView.highlighted = YES;

                    }

                }

                

                btn_7 = YES;

                

            }else{

                

                [_selectedNumArr removeObject:_listNameArr[7]];

                for (UIImageView *imgView in _imgViewArr) {

                    if (imgView.tag == 666 + 7) {

                        imgView.highlighted = NO;

                    }

                }

                

                

                btn_7 = NO;

                

            }

            

            

        }

            break;

            

        default:

            break;


   //选择按钮

            _selecteBtn = [UIButton buttonWithType:UIButtonTypeCustom];

            [_btnArr addObject:_selecteBtn];

            _selecteBtn.tag = i;

            [_btnArr addObject:_selecteBtn];

            //添加图标

            _selecteImgView = [[UIImageView alloc]initWithFrame:CGRectMake(btn_X, _Nav_H + _top_H + _bg_H + btn_Y + i * _btn_H, btn_W, btn_W)];

            _selecteImgView.image = [UIImage imageNamed:@"xz_b"];

            [_selecteImgView setHighlightedImage:[UIImage imageNamed:@"xz_l"]];

            [_imgViewArr addObject:_selecteImgView];

            _selecteImgView.tag = 666 + i;

            [self.view addSubview:_selecteImgView];

            _selecteBtn.frame = CGRectMake(btn_X,_Nav_H + _top_H + _bg_H + btn_Y + i * _btn_H, name_W, btn_W);

            [_selecteBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:_selecteBtn];

            //用户头像

            _iconImgView = [[UIImageView alloc]initWithFrame:CGRectMake(btn_X + btn_W + img_X_M, _Nav_H + _top_H + _bg_H + img_Y_M + i * _btn_H, img_W, img_W)];

            NSString *imgUrl = self.listImgArr[i];

            if ([imgUrl length] > 0)

            {

                __block UIActivityIndicatorView *activityIndicator;

                NSString *urlStr = [NSString stringWithFormat:@"%@thumb/%@",KimgUrl,imgUrl];

                [_iconImgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"mctx_wode"] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                    if (!activityIndicator) {

                        activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

                        activityIndicator.center = _iconImgView.center;

                        dispatch_async(dispatch_get_main_queue(), ^{

                            [_iconImgView addSubview:activityIndicator];

                        });

                        [activityIndicator startAnimating];

                        

                    }

                    

                } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                    [activityIndicator stopAnimating];

                }];

            }

            

            [self.view addSubview:_iconImgView];

            //用户姓名显示

            _memNickLab = [[UILabel alloc]initWithFrame:CGRectMake(btn_X + btn_W + img_X_M + img_W + name_X_M, _Nav_H + _top_H + _bg_H + name_Y_M + i * _btn_H, name_W, name_H)];

            _memNickLab.text = self.listNameArr[i];

            [self.view addSubview:_memNickLab];

           





猜你喜欢

转载自blog.csdn.net/qq_27247497/article/details/51166457