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

时间: 2024-10-25 18:04:05

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

UIScrollView 滚动视图的方法总结

UIScrollView的属性总结 属性 作用 CGPoint contentOffSet-------------------------- 监控目前滚动的位置 CGSize contentSize------------------------------ 滚动范围的大小 UIEdgeInsets contentInset--------------------- 视图在scrollView中的位置 id<UIScrollerViewDelegate> delegate------ 设置协

UIScrollVIew 滚动视图内容总结

1 - (void)viewDidLoad 2 3 { 4 5 [super viewDidLoad]; 6 7 // Do any additional setup after loading the view. 8 9 10 11 // 滚动视图 12 13 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 440)]; 14 15 scrollView.backgro

UIScrollView滚动视图

一.基本知识 1.初始化 UIScrollView #import "ViewController.h" #define WIDTH[[UIScreen mainScreen]bounds].size.width #define HEIGHT[[UIScreen mainScreen]bounds].size.height@interface ViewController ()<UIScrollViewDelegate> @end UIScrollView *scroll

UI:UIScrollView滚动视图

1.初始化 UIScrollView *scrollView = [[UIScrollViewalloc] init]; 2.设置尺寸 (1)按照屏幕尺寸 scrollView.frame =self.view.bounds; (2)自定义尺寸 scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 3.设置背景色 scrollView.backgroundColo

UI06-scrollView 滚动视图 pagecontroll 页面控制

1.scrollView  滚动视图 2.pagecontroll 页面控制 3.轮播图 1.scrollview是可以滚动的视图,UI view本身不具备滚动效果的,scrollview拓展了滚动功能,scrollview是所有可以滚动视图的基类 ,uitableview,uicollectionview都是继承自该类.意味着这两个类可以使用scrollview的属性‘ 2.scrollview的应用场景,(1)长图,大图,显示不了的,(2)内容太多,放不下的 3.scrollview 主体架

OCUI界面设计:滚动视图与分页控件初探

滚动视图(UIScrollView) 简单介绍 1.UIScrollView滚动视图能够排列并显示超出自身显示范围的内容. 2.UIScrollView内部整合了多种手势来达到丰富的界面展示效果. 3.UITableView.UITextView都继承于UIScrollView. UIScrollView例子 1.上下滚动:如信息界面或者设置界面. 2.左右滚动:如音乐播放器里面的专辑信息展示. UIScrollView实现机制 1.视图被加入到父视图上进行显示.超出父视图边界的部分将被隐藏.

IOS-UI- UIScrollView 滚动视图(1)

滚动视图多个页面实现的原理 滚动视图位置不变 内容的位置发生改变. 滚动视图的运用1.分页查看图片 2.查看大图片 3.当内容过多需要一个页面显示,如:注册,修改个人信息等等4.当不希望用户感觉咱们的界面是一张图片的时候...... 设置滚动视图的contentSize 时,如果contentSize小于 滚动视图的bounds就不会滚动 设置滚动视图的contentSize跟图片的尺寸一样 scrollview.contentSize =bgimg.size; 在滚动视图上添加图片 (在滚动视

UIScrollView 按页面滚动视图 UIPageControl

1.UIScrollView是可以滚动的view,UIView本?身不能滚动,子类UIScrollview拓展了滚动?方?面的功能. 2.UIScrollView是所有滚动视图的基类.以后的UITableView,UITextView等视图都是继承于该类. 3.UIScrollview主要专?长于两个?方?面: (1).滚动:contentSize?大于frame.size的时候,能够滚动. (2).缩放:?自带缩放,可以指定缩放倍数. UIScrollView滚动的相关属性 1.content

滚动视图和页面控制UIScollView,UIpageControlDemo

////  ViewController.m//  UIScollView////  Created by hehe on 15/9/25.//  Copyright (c) 2015年 wang.hehe. All rights reserved.// #import "ViewController.h"#define width_screen self.view.bounds.size.width#define height_screen self.view.bounds.size