码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-25 20:16:45      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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.height

@interface ViewController ()
{
 
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
    [self createUiScroview];  //创建滚动视图
    
    [self createLabel]; //第几页
    
    [self creatUIPageControl];   //创建页面控制器
    
}

#pragma mark    ------------------------createLabel
- (void)createLabel
{
    UILabel *label= [[UILabel alloc]init];
    
    label.frame = CGRectMake(0, height_screen-50, 50, 50);
    
    label.text = @"第一页";
    
    label.textColor = [UIColor redColor];
    
    label.font =  [UIFont systemFontOfSize:20];
    
    label.textAlignment = 1;
    
    label.adjustsFontSizeToFitWidth = YES;
    
    [self.view addSubview:label];
    
    label.tag = 11;
    
}

#pragma mark    ------------------------creatUIPageControl
- (void)creatUIPageControl
{
    UIPageControl *pc = [[UIPageControl alloc] init];
    
    pc.frame = CGRectMake(100, height_screen-30, 200, 30);
    
    [self.view addSubview:pc];
    
    pc.numberOfPages = 4;   //多少页
    
    pc.currentPage = 1;     //当前页数
    
    pc.pageIndicatorTintColor = [UIColor greenColor];   //页的颜色
    
    pc.currentPageIndicatorTintColor = [UIColor orangeColor];//当前页颜色
    
    pc.backgroundColor = [UIColor whiteColor];//加背景颜色
    
    [self.view addSubview:pc];
    
    pc.tag = 101;
}

#pragma mark    ------------------------滚动视图协议方法
//实现代理的方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    //实现缩放功能,需要两步,第二部
    //在滚动视图中对哪一个视图进行缩放
    return scrollView.subviews[0];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    UILabel *label1 = (id)[self.view viewWithTag:11];
    
    UIPageControl *pc1 = (id)[self.view viewWithTag:101];

    int offset = scrollView.contentOffset.x/self.view.bounds.size.width;
    
    if(offset==5)
    {
        scrollView.contentOffset = CGPointMake(self.view.bounds.size.width*1, 0);
    }
    else if (offset==0)
    {
        scrollView.contentOffset = CGPointMake(self.view.bounds.size.width*4, 0);
    }

    switch (offset)
    {
        case 0:
        {
            label1.text = @"第四页";
            pc1.currentPage = 3;
            break;
        }
        case 1:
        {
            pc1.currentPage = 0;
            label1.text = @"第一页";
            break;
        }
        case 2:
        {
            pc1.currentPage = 1;
            label1.text = @"第二页";
            break;
        }
        case 3:
        {
            pc1.currentPage = 2;
            label1.text = @"第三页";
            break;
        }
        case 4:
        {
            pc1.currentPage = 3;
            label1.text = @"第四页";
            break;
        }
        case 5:
        {
            pc1.currentPage = 0;
            label1.text = @"第一页";
            break;
        }
        default:
            break;
    }
}


- (void)createUiScroview
{
    //创建滚动视图对象
    UIScrollView *sv = [[UIScrollView alloc] init];
    
    sv.frame = self.view.bounds;
    
    [self.view addSubview:sv];
    
    sv.backgroundColor = [UIColor orangeColor];
    
    sv.contentOffset = CGPointMake(self.view.bounds.size.width,0);  //偏移量是一个点
    
    //设置每页的图片,大小
    for(int i=0;i<6;i++)
    {
        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        
        [sv addSubview:iv];
        
        if(i==0)
        {
            iv.image = [UIImage imageNamed:@"3"];
        }
        else if (i==5)
        {
            iv.image = [UIImage imageNamed:@"0"];
        }
        else
        {
            iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i-1]];
        }
        
    }
    sv.contentSize = CGSizeMake(self.view.bounds.size.width*6, self.view.bounds.size.height);
    
    //UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.jpg"]];//这个方法加入的原图大小
    
    sv.bounces = NO;   //控制是否反弹,默认是反弹的
    
    sv.indicatorStyle = UIScrollViewIndicatorStyleWhite;//设置滚动条的样式
    
    sv.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0 , 0, 30);//设置滚动条的位置
    
    sv.showsVerticalScrollIndicator = NO;//设置隐藏滚动条
    sv.showsHorizontalScrollIndicator = NO;
    
    //sv.scrollEnabled = NO;//设置是否滚动,让不让滚动
    
    sv.pagingEnabled = YES; //按页滚动,最后必须进去某一页//设置按页滚动
    
    sv.scrollsToTop = NO;  //默认是Yes//设置在下边的时候可以回到顶部
    
    sv.delegate = self; //有代理<UIScrollViewDelegate>
    
    //设置pinch第一步
    sv.minimumZoomScale = 0.5;//最小缩放比例,默认值是1
    sv.maximumZoomScale = 5;
    
    //设置减速率:1往后的差不多,值越大挺的越慢
    sv.decelerationRate = 0;
}

@end



技术分享技术分享

 

技术分享技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/wanghengheng/p/4839197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!