一个简单网易新闻页面的实现
2015-03-10 09:32
120 查看
本文参考了其它博文教程
//
// 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
相关文章推荐
- 一个简单的在线客服的实现(漂浮在网站左侧,随页面滚动
- node.js 一个简单的页面输出实现代码
- 用CSS实现一个简单的幻灯片效果页面
- Python web入门:Django学习与实践二(简单页面实现和创建一个模板)
- 利用线性布局和相对布局实现一个简单的页面
- JS实现一个简单的页面两份样式表
- 一个简单的学生登陆系统的jsp实现之登陆主页面main.jsp
- 实现一个简单的 页面划入效果
- 在页面中实现一个超简单的“复制按钮”
- 一个简单的Css实现的Tab页面
- 关于贴友的一个书本页面简单布局(html+css)的实现
- 简单实现在一个页面内上下跳转..
- Android用MVP实现一个简单的类淘宝订单页面
- 实现iOS图片等资源文件的热更新化(五): 一个简单完整的资源热更新页面
- jQuery 实现一个简单的信息反馈或者信息收集的页面
- 一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构、性能的要求都很简单,随着互联网业务的不断丰富,网站
- node.js 一个简单的页面输出实现代码
- 用django实现一个简单的页面
- PHP实现一个简单的计算器(在html页面和php文件之间传值)
- 利用线性布局和相对布局实现一个简单的页面并且使应用支持国际化语言