首页 > 其他 > 详细

仿网易侧滑

时间:2015-03-23 09:41:41      阅读:267      评论:0      收藏:0      [点我收藏+]
#import "LJFViewController.h"

#define kSCREENWIDTH                    ([UIScreen mainScreen].bounds.size.width)
#define kSCREENHEIGHT                   ([UIScreen mainScreen].bounds.size.height)

#define kWIDTH(v)         (v).frame.size.width
#define kHEIGHT(v)        (v).frame.size.height

@interface LJFViewController ()<UIScrollViewDelegate, UITableViewDelegate>

@property(nonatomic ,strong)NSArray *titleArray;
@property (nonatomic ,strong)NSMutableArray *buttonArray;
@property (nonatomic ,strong)UIScrollView *scrollView;

@end

@implementation LJFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"网易新闻";
    // Do any additional setup after loading the view.
    self.titleArray = @[@"头条",@"娱乐",@"体育",@"科技",@"汽车",@"手机",@"NBA",@"房产"];
    self.buttonArray = [[NSMutableArray alloc]init];

    //初始化视图
    [self initView];
}

-(void)initView{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 30)];
    bgView.backgroundColor = [UIColor colorWithRed:186.0 / 255.0 green:189.0 / 255.0 blue:186.0 / 255.0 alpha:1];
    [self.view addSubview:bgView];

    for (int i = 0; i < self.titleArray.count; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * (kSCREENWIDTH / self.titleArray.count), 0, kSCREENWIDTH / self.titleArray.count, 30);
        btn.tag = i + 99;
        btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
        if (i == 0) {
            btn.selected = YES;
        }
        [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btn_orderClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btn];
        [self.buttonArray addObject:btn];
    }


    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, kSCREENWIDTH, kSCREENHEIGHT)];
    self.scrollView.backgroundColor = [UIColor grayColor];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(kSCREENWIDTH * self.titleArray.count, kWIDTH(self.view)-40 -64);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.bounces = NO;
    [self.view addSubview:self.scrollView];

    for ( int i = 0; i < self.titleArray.count; i++) {
       // UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(i *kSCREENWIDTH, 0, kSCREENWIDTH, kSCREENHEIGHT) style:UITableViewStylePlain];

        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i *kSCREENWIDTH, 0, kSCREENWIDTH, kSCREENHEIGHT)];
        imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i +1]];

        [self.scrollView addSubview:imageview];
        //UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i *kSCREENWIDTH, 0, kSCREENWIDTH, kSCREENHEIGHT)];

        //tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        //tableview.separatorInset = UIEdgeInsetsZero;
        //[self.scrollView addSubview:tableview];

    }


}

-(void)btn_orderClick:(UIButton *)button{

    for (int i = 0; i < self.buttonArray.count; i++)
    {
        if (i == button.tag -99)
        {
            //设置选中页面的title
            //self.title = [self.titleArray objectAtIndex:i];
            [self.buttonArray[i] setSelected:YES];
        }
        else
        {
            [self.buttonArray[i] setSelected:NO];
        }
    }

    if (button.tag == 99)
    {

        [self.scrollView setContentOffset:CGPointMake(0, 0)];
    }
    else
    {

        [self.scrollView setContentOffset:CGPointMake(kSCREENWIDTH, 0)];


    }


}

仿网易侧滑

原文:http://blog.csdn.net/tubiebutu/article/details/44539389

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!