UIScrollView(滚动视图)和(UIPageController)页面控制器+ 定时器的使用

//
//  Teacher_ViewController.m
//  友家家教
//
//  Created by 邹贤 on 15/7/28.
//  Copyright (c) 2015年 友家科技公司. All rights reserved.
//

#import "Teacher_ViewController.h"

#import "ArtViewController.h"
#import "PrimaryViewController.h"
#import "MiddleViewController.h"
#import "HighSchoolViewController.h"
#import "OtherViewController.h"
#import "SequenceViewController.h"





@interface Teacher_ViewController ()

{
    CGFloat w;
    CGFloat h;
    NSArray *imageList;
    UIButton *left;
    NSArray *allLoction;
    NSArray *eachLoction;
    UITableView *locTableView;
    UITableView *eachLocTableView;
    UIPageControl *teacherPage;
    UIScrollView *scrollShowTeacher;
   
    NSTimer *timer;
   
    BOOL last;
   
    NSInteger index;
}

@end

@implementation Teacher_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
   
   
    self.view.backgroundColor = [UIColor colorWithRed:221/225.0 green:223/225.0 blue:190/225.0 alpha:1];
   
    self.title = @"找老师";
   
   

    [self createAll];

  
}



-(void)createAll{


    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 75, 250, 20)];
   
    label.text = @"您也许会感兴趣的教师有 :";
   
    label.font = [UIFont systemFontOfSize:15];
   
    label.textColor = [UIColor blackColor];
   
    [self.view addSubview:label];
   
   
   
    w = CGRectGetWidth(self.view.frame);
   
    h = CGRectGetHeight(self.view.frame);

   
#pragma mark --------------显示教师资料的滚动视图+page
   
    imageList = @[@"demashow",@"mangsengshow",@"jumoshow",@"vnshow"];
   
    scrollShowTeacher = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, w, h/3)];
   
    scrollShowTeacher.delegate = self;
   
    scrollShowTeacher.showsHorizontalScrollIndicator = NO;
   
    scrollShowTeacher.contentSize = CGSizeMake(w*imageList.count, 0);
   
    scrollShowTeacher.pagingEnabled = YES;
   
    scrollShowTeacher.indicatorStyle = UIScrollViewIndicatorStyleBlack;
   
    [self.view addSubview:scrollShowTeacher];
   
    for (int i =0; i<imageList.count; i++) {
       
        UIImageView *teacherImageView = [[UIImageView alloc]initWithFrame:CGRectMake(w*i, 0, w, h/3)];
       
        teacherImageView.image = [UIImage imageNamed:imageList[i]];
       
        [scrollShowTeacher addSubview:teacherImageView];
    }
   
    teacherPage = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 100+h/3-30+10, w, 15)];
   
    teacherPage.numberOfPages = imageList.count;
   
    teacherPage.currentPage = 0;
   
    teacherPage.currentPageIndicatorTintColor = [UIColor greenColor];
   
    teacherPage.pageIndicatorTintColor = [UIColor colorWithRed:215/225.0 green:214/225.0 blue:205/225.0 alpha:1];
   
    teacherPage.tag = 100;
   
    [self.view addSubview:teacherPage];
   
   
    [self addTimer];
   

   
   
   
#pragma mark -----------6个分类
   
    NSArray *buttonList = @[@"艺术类",@"初中",@"其他",@"小学",@"高中",@"排序"];
   
    for (int i = 0; i<3; i++) {
       
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
       
        leftButton.backgroundColor = [UIColor colorWithRed:20/225.0 green:150/225.0 blue:120/225.0 alpha:1];
       
        leftButton.tag = i;
       
        leftButton.frame = CGRectMake(20, CGRectGetMaxY(scrollShowTeacher.frame)+20+i*(80+10), w/2-40, 80);
       
        leftButton.layer.cornerRadius = 80/5;
       
        leftButton.clipsToBounds = YES;

       
        [leftButton setTitle:buttonList[i] forState:UIControlStateNormal];
       
        leftButton.showsTouchWhenHighlighted = YES;
       
        [leftButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
       
        [self.view addSubview:leftButton];
    }
   
    for (int i =0; i<3; i++) {
       
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
       
        rightButton.backgroundColor = [UIColor colorWithRed:20/225.0 green:150/225.0 blue:120/225.0 alpha:1];
       
        rightButton.tag = 3+i;
       
        rightButton.frame = CGRectMake(w/2+20, CGRectGetMaxY(scrollShowTeacher.frame)+20+i*(80+10), w/2-40, 80);
       
        rightButton.layer.cornerRadius = 80/5;
       
        rightButton.clipsToBounds = YES;
       
        [rightButton setTitle:buttonList[3+i] forState:UIControlStateNormal];
       
        rightButton.showsTouchWhenHighlighted = YES;
       
        [rightButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
       
        [self.view addSubview:rightButton];
    }
   
   
   
#pragma mark ---------  选择城市  UIButton
   
    left = [UIButton buttonWithType:UIButtonTypeCustom];
   
    left.frame = CGRectMake(50, 20, 150, 25);
   
    left.backgroundColor = [UIColor clearColor];
   
    [left setTitle:@"选择城市" forState:UIControlStateNormal];
   
    [left setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
   
    left.titleEdgeInsets = UIEdgeInsetsMake(0, -75, 0, 0);
   
    left.showsTouchWhenHighlighted = YES;
   
    [left addTarget:self action:@selector(showLocation:) forControlEvents:UIControlEventTouchUpInside];
   
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:left];
   
    self.navigationItem.leftBarButtonItem = item;

}



#pragma mark --------- 调用 选择城市(省) tableView


- (void)showLocation:(UIBarButtonItem *)sender
{
   
   
    if (!locTableView) {
       
        NSString *path = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
       
        allLoction = [NSArray arrayWithContentsOfFile:path];
       
        locTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, w/3, h/3) style:UITableViewStylePlain];
       
        locTableView.tag = 50;
       
        locTableView.dataSource = self;
       
        locTableView.delegate = self;
       
        [self.view addSubview:locTableView];
    }
  
}





- (void)ButtonAction:(UIButton *)sender
{
    ArtViewController *art = [[ArtViewController alloc]init];
     art.hidesBottomBarWhenPushed = YES;
   
    PrimaryViewController *primary = [[PrimaryViewController alloc]init];
    primary.hidesBottomBarWhenPushed = YES;
   
    MiddleViewController *middle = [[MiddleViewController alloc]init];
    middle.hidesBottomBarWhenPushed = YES;
   
    HighSchoolViewController *highSchool = [[HighSchoolViewController alloc]init];
    highSchool.hidesBottomBarWhenPushed = YES;
   
    OtherViewController *other = [[OtherViewController alloc]init];
    other.hidesBottomBarWhenPushed = YES;
   
    SequenceViewController *sequence = [[SequenceViewController alloc]init];
     sequence.hidesBottomBarWhenPushed = YES;
   
   
    switch (sender.tag) {
        case 0:
          
            [self.navigationController pushViewController:art animated:YES];
           
            break;
           
        case 1:
           
            [self.navigationController pushViewController:middle animated:YES];
           
            break;
           
        case 2:
           
            [self.navigationController pushViewController:other animated:YES];
           
            break;
           
        case 3:
           
            [self.navigationController pushViewController:primary animated:YES];
           
            break;
           
        case 4:
           
            [self.navigationController pushViewController:highSchool animated:YES];
           
            break;
           
        case 5:
           
            [self.navigationController pushViewController:sequence animated:YES];
           
            break;
       
           
        default:
            break;
    }
}


// 滚动视图+page 调用函数

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  
   
   
    CGFloat x = scrollView.contentOffset.x;
   
//    NSLog(@"%f",x);
//    teacherPage.currentPage = x/w;
   
    NSInteger curPage = x/w;
   
    if (last == YES&&teacherPage.currentPage>=imageList.count-1) {
       
        last = NO;
       
        teacherPage.currentPage = 0;
       
        scrollView.contentOffset = CGPointMake(0, 0);
       
    }else{
       
        teacherPage.currentPage = curPage;
       
        if (teacherPage.currentPage == imageList.count-1) {
           
            last = YES;
           
        }
    }
   
   
}


#pragma mark -------timer 调用

//添加定时器

- (void)addTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
   
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

//移除定时器
- (void)removeTimer
{
    [timer invalidate];
   
    timer = nil;
}

- (void)nextImage
{
    // 1.增加pageControl的页码

    if (scrollShowTeacher.contentOffset.x ==0) {
       
        CGFloat x = w*(teacherPage.currentPage+1);
    
        NSInteger curPage = x/w;
       
        if (last == YES&&teacherPage.currentPage>=imageList.count-1) {
           
            last = NO;
           
            teacherPage.currentPage = 0;
           
            scrollShowTeacher.contentOffset = CGPointMake(0, 0);
           
        }else{
           
            teacherPage.currentPage = curPage;
           
            if (teacherPage.currentPage == imageList.count-1) {
               
                last = YES;
               
            }
           
        }
       
    }else{
       
        CGFloat x = scrollShowTeacher.contentOffset.x+w*teacherPage.currentPage;
       
//        NSLog(@"%f",scrollShowTeacher.contentOffset.x);
       
        NSInteger curPage = x/w;
       
        if (last == YES&&teacherPage.currentPage>=imageList.count-1) {
           
            last = NO;
           
            teacherPage.currentPage = 0;
           
            scrollShowTeacher.contentOffset = CGPointMake(0, 0);
           
        }else{
           
            teacherPage.currentPage = curPage;
           
            if (teacherPage.currentPage == imageList.count-1) {
               
                last = YES;
            }
        }
    }
   
//  ------------------------------------------------
   

   
// 2.计算scrollView滚动的位置
   
    CGFloat offsetX = teacherPage.currentPage * w;
   
    CGPoint offset = CGPointMake(offsetX, 0);
   
//    NSLog(@"------");
   
    [scrollShowTeacher setContentOffset:offset animated:YES];
   
}


//开始拖拽的时候移除定时器

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}


//停止拖拽的时候添加定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}



// UITableViewDataSource 必须调用的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return allLoction.count;
   
    if (tableView.tag == 50) {
        return allLoction.count;
    }
   eachLoction = allLoction[index][@"Cities"];
    if (tableView.tag == 51) {
        return eachLoction.count;
    }
   
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    if (tableView.tag == 50) {
       
        NSString *cellID = @"cellID";
       
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
       
        if (!cell) {
           
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
           
        }
       
        cell.backgroundColor = [UIColor grayColor];
       
        cell.textLabel.text = allLoction[indexPath.row][@"State"];
       
        return cell;
    }
   
    if (tableView.tag == 51) {
        NSString *cellIDID = @"cellIDID";
       
        UITableViewCell *cellID = [tableView dequeueReusableCellWithIdentifier:cellIDID];
       
        if (!cellID) {
           
            cellID = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIDID];
           
        }
       
        cellID.backgroundColor = [UIColor grayColor];
       
        cellID.textLabel.text = allLoction[index][@"State"][@"Cities"];
       
        return cellID;
    }
   
    return nil;
   
   
}

// 点击tableView 后调用函数

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    locTableView.scrollsToTop = YES;
    eachLocTableView.scrollsToTop = YES;
   
//    NSString *title =  allLoction[indexPath.row][@"State"];
//   
//    [left setTitle:title forState:UIControlStateNormal];
//   
//    [locTableView removeFromSuperview];
//   
//    locTableView = nil;

   
    if (tableView.tag == 50) {

       
        [self showEachLocat];
    }
   
    if (tableView.tag == 51) {
       
        NSString *title = [NSString stringWithFormat:@"%@-%@",allLoction[indexPath.row][@"State"],allLoction[index][@"State"][@"Cities"]];
        [left setTitle:title forState:UIControlStateNormal];
        [locTableView removeFromSuperview];
        locTableView = nil;
        [eachLocTableView removeFromSuperview];
        eachLocTableView = nil;
    }
   
   
  
   
}

#pragma mark  ------二级显示地址

- (void)showEachLocat
{
    if (!eachLocTableView) {
       
//        NSString *path = [[NSBundle mainBundle]pathForResource:@"citys" ofType:@"plist"];
       
//        allLoction = [NSArray arrayWithContentsOfFile:path];
       
        eachLocTableView = [[UITableView alloc]initWithFrame:CGRectMake(w/3, CGRectGetMinY(locTableView.frame), w/3, h/3) style:UITableViewStylePlain];
       
        eachLocTableView.tag = 51;
       
        eachLocTableView.dataSource = self;
       
        eachLocTableView.delegate = self;
       
        [self.view addSubview:eachLocTableView];
    }
   
   
   
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
原文地址:https://www.cnblogs.com/wukun16/p/4844763.html