本文参考了其它博文教程
//
// ViewController.m// example
//
// Created by jose on 15-3-8.
// Copyright (c) 2015年 jose. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
//采用全局变量
@property(nonatomic,retain)UIView *lineview;
@property(nonatomic,retain)UIScrollView *scrollview;
@property(nonatomic,retain)UIPageControl *pagecontrol;
@property CGFloat width;
@property CGFloat heigt;
@end
@implementation ViewController
@synthesize lineview=_lineview;
@synthesize scrollview=_scrollview;
@synthesize pagecontrol=_pagecontrol;
@synthesize width=_width;
@synthesize heigt=_heigt;
- (void)viewDidLoad {
[super viewDidLoad];
//设置背景颜色
self.view.backgroundColor=[UIColor whiteColor];
//获取屏幕的宽和高
_width=[UIScreen mainScreen].bounds.size.width;
_heigt=[UIScreen mainScreen].bounds.size.height;
//加载视图
[self sethead];
[self setscrollerview];
[self setimgagescrollerviews];
[self pagecontrollers];
[self addlabel];
[self labelbutton];
//定时器
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(cycleplay) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//设置head
-(void)sethead{
UIView *headview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _width, 60)];
headview.backgroundColor=[UIColor redColor];
[self.view addSubview:headview];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake((_width/2-40), 15, 80, 30)];
label.text=@"网易新闻";
label.textColor=[UIColor blackColor];
label.font=[UIFont systemFontOfSize:18];
[headview addSubview:label];
}
-(void)setscrollerview{
//创建scroller的大小,在视图中显示的大小
UIScrollView *scroller=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, _width, 30)];
//配置scroller的总大小
scroller.contentSize=CGSizeMake(_width, 30);
//垂直提示
scroller.showsVerticalScrollIndicator=NO;
scroller.backgroundColor=[UIColor grayColor];
[self.view addSubview:scroller];
NSMutableArray *scrollerarray=[NSMutableArray arrayWithObjects:@"科技",@"数码",@"军事",@"笑话",@"财经",@"娱乐",nil];
//添加按钮
for (int i=0; i<[scrollerarray count]; i++) {
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50*i,0, 50, 30)];
[button setTitle:scrollerarray[i] forState:UIControlStateNormal];
button.tag=100+i;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:button];
}
_lineview=[[UIView alloc]initWithFrame:CGRectMake(0, 27, 50, 3)];
_lineview.backgroundColor=[UIColor blackColor];
//下划线
[scroller addSubview:_lineview];
}
//下划线
-(void)click:(UIButton *)button{
//通过tag获取对应按钮的属性
_lineview.frame=CGRectMake(button.frame.origin.x, 27, 50, 3);
}
//添加imgagescroll
-(void)setimgagescrollerviews{
_scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 90, _width, 240)];
_scrollview.contentSize=CGSizeMake(_width*5, 240);
//偏移量
_scrollview.contentOffset=CGPointMake(320, 0);
//添加图片视图
for (int i=0; i<5; i++){
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(_width*i, 0, _width, 240)];
if(0==i)
{ //获取图片
imageview.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"3"] ofType:@"jpg"]];
}
if(i>=1 && i<=3)
{
imageview.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",i] ofType:@"jpg"]];
}
if(4==i)
{
imageview.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"1"] ofType:@"jpg"]];
}
[_scrollview addSubview:imageview];
}
_scrollview.showsHorizontalScrollIndicator=NO;
_scrollview.showsVerticalScrollIndicator=NO;
_scrollview.delegate=self;
//设置分页
_scrollview.pagingEnabled=YES;
_scrollview.tag=150;
[self.view addSubview:_scrollview];
}
//添加pagecontrol
-(void)pagecontrollers{
_pagecontrol=[[UIPageControl alloc]initWithFrame:CGRectMake(120, 280, 80, 30)];
//页数
_pagecontrol.numberOfPages=3;
//当前点的颜色
_pagecontrol.currentPageIndicatorTintColor=[UIColor orangeColor];
//没有选中的时候点的颜色
_pagecontrol.pageIndicatorTintColor=[UIColor blackColor];
_pagecontrol.tag=160;
[_pagecontrol addTarget:self action:@selector(handpage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_pagecontrol];
}
//点击事件
-(void)handpage:(UIPageControl *)pagecontrol{
//获取scrollview
UIScrollView *scrollview=(UIScrollView *)[self.view viewWithTag:150];
[scrollview setContentOffset:CGPointMake(_width*(pagecontrol.currentPage+1), 0)animated:YES];
}
//代理
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UIPageControl *pagecontrol=(UIPageControl *)[self.view viewWithTag:160];
pagecontrol.currentPage=(scrollView.contentOffset.x-_width)/_width;
int i=(scrollView.contentOffset.x-_width)/_width+1;
if(0==i)
{
scrollView.contentOffset=CGPointMake(_width*3, 0);
pagecontrol.currentPage=2;
}
else if(4==i)
{
scrollView.contentOffset=CGPointMake(_width, 0);
pagecontrol.currentPage=0;
}
}
//循环播放
-(void)cycleplay{
NSInteger current=_pagecontrol.currentPage;
current++;
if(current>2)
{
current=0;
}
_pagecontrol.currentPage=current;
[self handpage:_pagecontrol];
}
//添加标签属性
-(void)addlabel{
UIView *label=[[UIView alloc]initWithFrame:CGRectMake(0, 330, 320, 60)];
label.backgroundColor=[UIColor colorWithRed:255/225.0 green:182/225.0 blue:193/225.0 alpha:1.0];
[self.view addSubview:label];
UILabel *labeltext=[[UILabel alloc]initWithFrame:CGRectMake(20, 20, 80, 20)];
labeltext.text=@"添加标签";
labeltext.font=[UIFont systemFontOfSize:18];
[label addSubview:labeltext];
}
//底部按钮创建
-(void)labelbutton{
NSArray *array=[NSArray arrayWithObjects:@"科技",@"手机",@"电脑",@"相机",@"新闻",@"娱乐",@"搞笑",@"美文",@"旅游",@"音乐",@"生活",nil];
int n=0;
for(int i=0;i<2;i++)
{
for (int j=0; j<4; j++) {
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(5+j*80, 400+i*40, 70, 30)];
[button setTitle:array[n++] forState:UIControlStateNormal];
button.backgroundColor=[UIColor lightGrayColor];
[self.view addSubview:button];
}
}
}
@end