仿新闻顶部滚动

#import "informationViewController.h"

#import "informationView/informationViewCell.h"

#define topButtonWidth100

@interface informationViewController (){

    UIScrollView*_topScrollView;


    NSMutableArray*_mArrayTopButton;

    NSMutableArray*_mArrayTitle;

}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementationinformationViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.hidden = YES;

    self.view.backgroundColor = [UIColor whiteColor];

    _mArrayTitle = [NSMutableArray array];

    _mArrayTopButton = [NSMutableArray array];


    [self setTopScrollView];


    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, StatusBarHeight+40, self.view.frame.size.width, self.view.frame.size.height-StatusBarHeight-40)];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    _tableView.tableFooterView = [[UIView alloc]init];

}

-(void)setTopScrollView{


     _mArrayTitle = [NSMutableArray arrayWithObjects:@"期货要闻",@"综合资讯",@"今日导读",@"内盘播报",nil];

    _topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(12, StatusBarHeight, WIDTH-24, 40)];


    _topScrollView.showsHorizontalScrollIndicator = NO;

    _topScrollView.showsVerticalScrollIndicator = NO;

    //_topScrollView.backgroundColor = [UIColor redColor];

    for(NSIntegeri =0; i<_mArrayTitle.count; i++) {

        UIButton*topButton = [[UIButtonalloc]initWithFrame:CGRectMake(i *topButtonWidth,0,topButtonWidth,40)];

        topButton.tag= i-10;

    //    [topButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

       // [topButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

         [topButtonsetTitle:_mArrayTitle[i] forState:UIControlStateNormal];

         [_mArrayTopButtonaddObject:topButton];

        [_topScrollViewaddSubview:topButton];

        topButton.tag=10+ i;

        [topButtonaddTarget:self action:@selector(clickTopButton:) forControlEvents:UIControlEventTouchUpInside];

        //topButton.selected = NO;



        [topButtonsetTitleColor:[UIColor colorWithRed:122.0/255 green:121.0/255 blue:121.0/255 alpha:1] forState:UIControlStateNormal];


        topButton.selected=NO;

        [topButtonsetTitleColor:[UIColor colorWithRed:178.0/255 green:17.0/255 blue:25.0/255 alpha:1] forState:UIControlStateSelected];

    }


     ((UIButton*)_mArrayTopButton[1]).selected=YES;

    [_topScrollView setContentSize:CGSizeMake(_mArrayTitle.count * topButtonWidth, 40)];

    ((UIButton*)_mArrayTopButton[1]).selected=YES;

     [_topScrollView setContentSize:CGSizeMake(_mArrayTitle.count * topButtonWidth, 40)];


    if(_topScrollView.contentSize.width - _topScrollView.frame.size.width <0)

    {

        [_topScrollView setContentOffset:CGPointMake(-(_topScrollView.frame.size.width - _topScrollView.contentSize.width)/2, 0) animated:NO];

    }


    [self.view addSubview:_topScrollView];


}

//clickTopButton:

-(void)clickTopButton:(UIButton*)sender{

    // 设置Button的选中情况

    for(UIButton*buttonin_mArrayTopButton) {

        if(button.selected==YES) {

            button.selected=NO;


            button.layer.borderColor= [UIColorcolorWithRed:178.0/255green:17.0/255blue:25.0/255alpha:1].CGColor;

            button.layer.borderWidth=1.0f;

            button.layer.masksToBounds=YES;

            button.layer.cornerRadius=15.0f;

            button.backgroundColor= [UIColorcolorWithRed:255.0/255green:224.0/255blue:226.0/255alpha:1];

        }



    }

//    button.backgroundColor = [UIColor clearColor];

//    [button setSelected:NO];

//    button.layer.borderWidth = 0.0f;

//    sender.selected = YES;


    NSLog(@"输出sender.tag:%ld",(long)sender.tag);


}

#pragma mark -uiscrollviewdelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{

    // 设置Button的选中情况

    for(UIButton*buttonin_mArrayTopButton) {

        if(button.selected==YES) {

            button.selected=NO;

        }

    }

    ((UIButton*)[_mArrayTopButtonobjectAtIndex:scrollView.contentOffset.x/WIDTH]).selected=YES;


}

#pragma mark创建tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return 5;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{


    informationViewCell *viewcell = [informationViewCell cellWithTableView:tableView];


    viewcell.selectionStyle = UITableViewCellSelectionStyleNone;

    viewcell.contentView.backgroundColor = [UIColor colorWithRed:253.0/255 green:252.0/255 blue:250.0/255 alpha:1];

    returnviewcell;

}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    return 100;

}

@end

你可能感兴趣的:(仿新闻顶部滚动)