定时刷新视图,写的一个小小的程序,一年前

//

//  ViewController.m

//  手机网易2

//

//  Created by BreazeMago on 15/1/7.

//  Copyright (c) 2015 BreazeMago. All rights reserved.

//


#import "rootViewController.h"


@interface rootViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,UIGestureRecognizerDelegate>

{

   UIScrollView *scollView;

   UIImageView  *adi1;

   UIView *navbar;

   NSMutableArray  *adiImages1;

   NSMutableArray  *adiSrc1;

   NSMutableArray  *adiImages2;

   NSMutableArray  *adiSrc2;

   UIImageView  *bigImages;

   NSInteger adi1Index;

   NSInteger bigImagesIndex;

   NSMutableArray  *newsButtons;

   NSMutableArray  *detailTables;

}

@property (nonatomic,strong) IBOutletUIView  *navbar;

@property (nonatomic,strong) UIPageControl *pageControl;

@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,strong) UIScrollView  *scrollView1;

@property (nonatomic,strong) UIScrollView  *scrollView2;

@property (nonatomic,strong) IBOutletUIImageView  *adi1;

@property (nonatomic,strong) IBOutletUIImageView  *bigImages;

@property (nonatomic,strong) IBOutletNSMutableArray  *newsButtons;

@property (nonatomic,strong) IBOutletNSMutableArray  *detailTables;


@end


@implementation rootViewController

@synthesize navbar;

@synthesize adi1;

@synthesize bigImages;

@synthesize newsButtons;

@synthesize detailTables;

@synthesize pageControl;

@synthesize timer;

@synthesize scrollView1;

@synthesize scrollView2;


// Implement loadView to create a view hierarchy programmatically, without using a nib.


-(void)addTimer

{

    self.timer = [NSTimerscheduledTimerWithTimeInterval:5target:selfselector:@selector(nextImage)userInfo:nilrepeats:YES];

    [[NSRunLoopcurrentRunLoop] addTimer:self.timerforMode:NSRunLoopCommonModes];

}

-(void)nextImage

{

   static int page =0,page1 = 0;

   if (page == 2) {

        page =0;

    }else

    {

        page++;

    }

    

   if (page1 == 1) {

        page1 =0;

    }else{

        page1 =1;

    }

   CGFloat x = page * self.scrollView1.frame.size.width;

   self.scrollView1.contentOffset =CGPointMake(x, 0);

   CGFloat x1 = page1 * self.scrollView2.frame.size.width;

   self.scrollView2.contentOffset =CGPointMake(x1, 0);

    //NSLog(@"nextImage page1 = %d",page);

    //NSLog(@"nextImage x = %f",x);

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

   CGFloat scrollviewW =  scrollView.frame.size.width;

   CGFloat x = scrollView.contentOffset.x;

   int page = (x + scrollviewW / 2) /  scrollviewW;

   self.pageControl.currentPage = page;

    NSLog(@"nextImage page2 = %d",page);

}

- (void)removeTimer

{

    [self.timerinvalidate];

}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [selfremoveTimer];

}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    [selfaddTimer];

}

-(void)handleSwipFrom:(UISwipeGestureRecognizer*)gestureRecognizer

{

    if(gestureRecognizer.direction ==UISwipeGestureRecognizerDirectionUp){

        

    }elseif(gestureRecognizer.direction ==UISwipeGestureRecognizerDirectionDown){

        

    }

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    //3g.163.com/touch

    adi1Index = 0;

    bigImagesIndex =0;

    scollView = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,20,320,1130)];

   scollView.contentSize =CGSizeMake(0,1130);

    scollView.pagingEnabled =YES;

   UISwipeGestureRecognizer *swipRecognizer = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSwipFrom:)];

    [self.viewaddGestureRecognizer:swipRecognizer];//关键语句,给self.view添加一个手势监测;

    swipRecognizer.delegate =self;

   scollView.delegate =self;

    ////NSURL *Img4Url = [NSURL URLWithString:@"http://3g.163.com/touch/?from=index.toplogo"];

    //UIImage *Img4 = [UIImage imageWithData:[NSData dataWithContentsOfURL:Img4Url]];

    //UIImageView *navImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,80,40)];

    //[navImageView setImage:Img4];

    navbar = [[UIViewalloc] initWithFrame:CGRectMake(0,0,320,40)];

    navbar.backgroundColor =[UIColorredColor];

    

    UIButton *btn1 = [[UIButtonalloc] initWithFrame:CGRectMake(200,0,40,40)];

    [btn1 setTitle:@"下载"forState:UIControlStateNormal];

    [btn1 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    //[btn1 setBackgroundColor:[UIColor redColor]];

    

    UIButton *btn2 = [[UIButtonalloc] initWithFrame:CGRectMake(200+40,0,40,40)];

    [btn2 setTitle:@"彩票"forState:UIControlStateNormal];

    [btn2 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    //[btn2 setBackgroundColor:[UIColor yellowColor]];

    

    UIButton *btn3 = [[UIButtonalloc] initWithFrame:CGRectMake(200+80,0,40,40)];

    [btn3 setTitle:@"导航"forState:UIControlStateNormal];

    [btn3 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    //[btn3 setBackgroundColor:[UIColor blueColor]];

    

    //[navbar addSubview:navImageView];

    [scollView addSubview: navbar];

    [scollViewaddSubview:btn1];

    [scollViewaddSubview:btn2];

    [scollViewaddSubview:btn3];

    

    

    NSURL *Img1Url = [NSURLURLWithString:@"http://img1.126.net/channel6/2015/018439/640100_0105.jpg"];

    UIImage *Img1 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img1Url]];

    NSURL *Img2Url = [NSURLURLWithString:@"http://img1.126.net/channel6/640x100bbb.jpg"];

    UIImage *Img2 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img2Url]];

    NSURL *Img3Url = [NSURLURLWithString:@"http://img1.126.net/channel6/2015/018736/640100_0106.jpg"];

    UIImage *Img3 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img3Url]];

   adiImages1 = [[NSMutableArray alloc] initWithObjects:Img1,Img2,Img3,nil];

    adiSrc1 = [[NSMutableArray alloc] initWithObjects:@"http://g.163.com/a?CID=32879&Values=584805076&Redirect=http://e.cn.miaozhen.com/r.gif?k=2002270&p=6rQFh&vo=369e37f12&vr=2&rt=2&ns=[M_ADIP]&ni=[M_IESID]&v=[M_LOC]&o=http://clk.gentags.net/nck/iv-9741/st-890/cr-2/oi-200925/or-3528/adv-122/pcon-0/mid=deviceid&t_ip=clientIp&os=OSType&rnd=rndm/http%253A%252F%252Fbetter.telunsu.net%252Fcny-m",@"http://caipiao.163.com/m/activity/dream.html?from=newspp3g",@"http://g.163.com/a?CID=32974&Values=112870580&Redirect=http://popme.163.com/link/018736_0107_2752.html",nil];

    scrollView1  =[[UIScrollViewalloc] initWithFrame:CGRectMake(0,40, 320, 40)];

    CGFloat imageW =self.scrollView1.frame.size.width;

    CGFloat imageH =self.scrollView1.frame.size.height;

   CGFloat imageY = 0;

    

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

       UIImageView *imageView = [[UIImageViewalloc] init];

       CGFloat imageX = i * imageW;

        imageView.frame =CGRectMake(imageX, imageY, imageW, imageH);

        //imageView.image = [adiImages1 objectAtIndex:i];

        scrollView1.showsHorizontalScrollIndicator =NO;

        //scrollView1.showsVerticalScrollIndicator = NO;

        [scrollView1addSubview:imageView];

    }

    

   CGFloat contentW = 3 *imageW;

   scrollView1.contentSize =CGSizeMake(contentW, 0);

    scrollView1.pagingEnabled =YES;

    scrollView1.delegate =self;

    [selfaddTimer];

    [scollView addSubview:scrollView1];

    

    newsButtons = [[NSMutableArrayalloc] initWithObjects:

                   @"新闻",@"独家",@"体育",@"娱乐",@"女人",@"财经",

                   @"汽车",@"手机",@"科技",@"游戏",@"军事",@"选股",

                   @"图片",@"房产",@"数码",@"博客",@"邮箱",@"小说",nil];/////

   for(int i=0;i<[newsButtonscount];i++){

       UIButton *btn4;

        btn4= [[UIButtonalloc] initWithFrame:CGRectMake(10+(i%6)*50,80+(i/6)*30,49,30)];

        [btn4 setTitle:[newsButtonsobjectAtIndex: i] forState:UIControlStateNormal];

        [btn4 setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        [scollViewaddSubview: btn4];

    }

    

    NSURL *Img5Url = [NSURLURLWithString:@"http://img5.cache.netease.com/3g/2015/1/7/201501072048056e7fe.jpg"];

    UIImage *Img5 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img5Url]];

    NSURL *Img6Url = [NSURLURLWithString:@"http://img1.126.net/channel6/2015/018517/640370_1229.jpg"];

    UIImage *Img6 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img6Url]];

    //adiSrc2 = [[NSMutableArray  alloc] initWithObjects:@"http://3g.163.com/touch/article.html?from=index&docid=AFCOUN1Owangyanfeng",@"http://g.163.com/a?CID=32621&Values=3219655259&Redirect=http://e.cn.miaozhen.com/r.gif?k=1017724&p=40fGn0&vo=313add540&vr=2&rt=2&ns=[M_ADIP]&ni=[M_IESID]&o=http%3A%2F%2Fevent.dianping.com%2Fmarket%2F201412mengniu%2Fapp%2Findex.html%3Fversion%3D*%26token%3D*",nil];

   adiImages2 = [[NSMutableArrayalloc] initWithObjects:Img5,Img6,nil];

    //UIImageView *bigImagesView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 220, 320, 240)];

    scrollView2  =[[UIScrollViewalloc] initWithFrame:CGRectMake(0,170, 320, 240)];

    CGFloat imageW1 =self.scrollView2.frame.size.width;

    CGFloat imageH1 =self.scrollView2.frame.size.height;

   CGFloat imageY1 = 0;

    

   for(int i =0;i<2;i++){

       UIImageView *imageView1 = [[UIImageViewalloc] init];

       CGFloat imageX1 = i * imageW1;

        imageView1.frame =CGRectMake(imageX1, imageY1, imageW1, imageH1);

        //imageView1.image = [adiImages2 objectAtIndex:i];

        scrollView2.showsHorizontalScrollIndicator =NO;

        //scrollView1.showsVerticalScrollIndicator = NO;

        [scrollView2addSubview:imageView1];

    }

    

   CGFloat contentW1 = 2 *imageW1;

   scrollView2.contentSize =CGSizeMake(contentW1, 0);

    scrollView2.pagingEnabled =YES;

    scrollView2.delegate =self;

    [scollView addSubview:scrollView2];

    

    detailTables = [[NSMutableArrayalloc] initWithCapacity:15];

    UITableView *tabView1 = [[UITableViewalloc] initWithFrame:CGRectMake(0,410, 320, 240)style:UITableViewStylePlain];

    tabView1.delegate =self;

    tabView1.dataSource =self;

    [scollViewaddSubview:tabView1];

    [detailTablesaddObject:tabView1];

   /*

     UIView *adiViewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 700, 320, 40)];

     UILabel *adiLabelOne = [[UILabel alloc] initWithFrame:CGRectMake(0, 700, 280, 40)];

     adiLabelOne.text = @"3D动作冒险手游邀你来战";

     NSString*adiSrcStr = @"http://txhd.163.com/newsapp/";

     adiLabelOne.userInteractionEnabled = YES;

     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickUILabel)];

     [adiLabelOne addGestureRecognizer:tapGesture];

     [adiViewOne addSubview :adiLabelOne];

     [scollView addSubview:adiViewOne];

     

     UITableView *tabView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 740, 320, 280) style:UITableViewStylePlain];

     tabView2.delegate = self;

     tabView2.dataSource = self;

     UIView *headerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 740, 320, 40)];

     

     UILabel *headText1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 740, 80, 40)];

     headText1.text = @"新闻";

     headText1.numberOfLines = 0;

     [headerView1 addSubview:headText1];

     tabView2.tableHeaderView = headerView1;

     [scollView addSubview:tabView2];

     [detailTables addObject: tabView2];

     

     UIView *adiViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 1020, 320, 40)];

     UILabel *adiLabelTwo = [[UILabel alloc] initWithFrame:CGRectMake(0, 1020, 280, 40)];

     adiLabelTwo.text = @"免费送8+118元彩票!";

     adiLabelTwo.userInteractionEnabled = YES;

     [adiLabelTwo addGestureRecognizer:tapGesture];

     [adiViewTwo addSubview : adiLabelTwo];

     [scollView addSubview:adiViewTwo];*/

    [self.viewaddSubview:scollView];

    [superviewDidLoad];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

   return 1;

}

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

   int rows = 0;

   if(tableView == [detailTablesobjectAtIndex : 0])

    {

        rows =4;

    }else{

        rows =8;

    }

   return rows;

}


- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

{

   return 5;

}

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

{

   int height = 0;

   if(tableView == [detailTablesobjectAtIndex : 0])

    {

        height =60;

    }else{

        height =30;

    }

    

   return height;

}


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

{

   static NSString *CellWithIdentifier =@"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellWithIdentifier];

    }

    NSURL *Img7Url = [NSURLURLWithString:@"http://img1.cache.netease.com/3g/2015/1/8/201501082026264a41b.jpg"];

    UIImage *Img7 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img7Url]];

    NSURL *Img8Url = [NSURLURLWithString:@"http://img2.cache.netease.com/3g/2015/1/8/20150108213013e203a.jpg"];

    UIImage *Img8 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img8Url]];

    NSURL *Img9Url = [NSURLURLWithString:@"http://img5.cache.netease.com/3g/2015/1/8/2015010814545338bd7.jpg"];

    UIImage *Img9 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img9Url]];

    NSURL *Img10Url = [NSURLURLWithString:@"http://img3.cache.netease.com/3g/2015/1/8/20150108110827d4c42.png"];

    UIImage *Img10 = [UIImageimageWithData:[NSDatadataWithContentsOfURL:Img10Url]];

    

   NSMutableArray *newsImgs1 = [[NSMutableArrayalloc] initWithObjects:Img7,Img8,Img9,Img10,nil];

    NSMutableArray *newsTitles1 = [[NSMutableArrayalloc] initWithObjects:@"江绵恒卸任中科院上海分院长",@"海口一武警车鸣警笛强闯小学",@"林森浩:若死刑核准望捐献遗体",@"宣判后林父跌落椅子无法动弹",nil];

    NSMutableArray *newsSubTitlesImgs1 = [[NSMutableArrayalloc] initWithObjects:@"因年龄原因卸任,上海科技大学党委书记朱志远接任。",@"疑因车上一名女性家长的孩子发烧,事后该家长道歉。",@"付出年轻的生命来赔罪,希望这最后一件事能做对。",@"黄洋父母情绪失控痛哭不止,林森浩父亲称继续上诉。",nil];

   NSUInteger row = [indexPath row];

   if(tableView == [detailTablesobjectAtIndex : 0])

    {

        cell.textLabel.text = [newsTitles1objectAtIndex:row];

        //cell.imageView.image = [newsImgs1 objectAtIndex:row];

        cell.detailTextLabel.text = [newsSubTitlesImgs1objectAtIndex:row];

    }else{

        cell.textLabel.text =@"大标题";

        //cell.imageView.image = [UIImage imageNamed:@".png"];

        cell.detailTextLabel.text =@"小标题";

    }

    

   return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

   /*

     NSString *msg = [[NSString alloc] initWithFormat:@"ƒ„—°‘Òµƒ «:%@",[self.dataList objectAtIndex:[indexPath row]]];

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"÷ æ" message:msg delegate:self cancelButtonTitle:@"»∑∂®" otherButtonTitles:nil, nil];

     [msg release];

     [alert show];

     */

}


@end



你可能感兴趣的:(iOS开发)