//
// RootViewController.m
// UI6_UIScrollView
//
// Created by YIem on 15/12/14.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "RootViewController.h"

@interface RootViewController () <UIScrollViewDelegate>// 协议

@end

@implementation RootViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    ////********* UIScrollView 滚动视图*************///
    
    //    UIScrollView   是所有滚动视图的基类
    // 功能 : 1.滚动 2.播放

if 0

// 1.创建
UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
// 2.设置
sv.backgroundColor = [UIColor yellowColor];
// 3.添加父视图
[self.view addSubview:sv];
// 4.内存
[sv release];
// 滚动的条件: 实际内容大小 > 滚动视图自身的frame
////*******<  contentSize  内容大小 >*******////
sv.contentSize = CGSizeMake(300, 300);
/// 只允许左右滚动

// sv.contentSize = CGSizeMake(300, 0);

// 只允许上下ai

// sv.contentSize = CGSizeMake(0, 300);

endif

if 1


// 相册练习
// 1. 创建相册
//  self.view.frame  和屏幕一样大
UIScrollView *photos = [[UIScrollView alloc]initWithFrame:self.view.frame];
photos.backgroundColor = [UIColor yellowColor];
[self.view addSubview:photos];
[photos release];
// 设置内容大小
photos.contentSize = CGSizeMake(self.view.frame.size.width * 6, 0);
// 3. 处理图片数组
NSMutableArray *photoArr = [NSMutableArray array];
for (NSInteger i = 1; i <= 6 ; i++) {
    // 图片名
    NSString *name = [NSString stringWithFormat:@"S%ld", i];
    // 图片路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
    // 图片对象
    UIImage *img = [UIImage imageWithContentsOfFile:path];
    // 添加数组
    [photoArr addObject:img];

if 0

    // 4.方式一:不使用数组
    // 创建相框
    UIImageView  *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(375 * (i - 1), 0, 375, 667)];
    imgView.image = img;
    [photos addSubview:imgView];
    [imgView release];

endif

}

// 4. 方式二:使用数组
for (NSInteger i = 0; i < photoArr.count; i++) {
    UIImageView  *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(375 * i, 0, 375, 667)];
    imgView.image = photoArr[i];
    [photos addSubview:imgView];
    [imgView release];
}

// 属性设置
/*******<  contentOffset 偏移量 >*****/
// 将设置的点移动到frame的原点
// 滚动时偏移量一定会改变  并且偏移量改变一定会滚动
photos.contentOffset = CGPointMake(0, 0);
//****** < contentInset 边界预留   > ******/
// 在滚动视图的内容之外添加预留区域(上左下右)
photos.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
//*****< pafeingenable整页翻动效果>**/
photos.pagingEnabled = YES;
//*****<  bouces 边缘效果>*****/默认YES
photos.bounces = YES;
// /.水平/垂直 边缘效果
// 当前允许在某一方向小于frame时 有边缘效果
photos.alwaysBounceHorizontal = YES;
photos.alwaysBounceVertical = NO;
/*****   <scrollsToTop  滚到头 >****/
photos.scrollsToTop = YES;
/**********< scrollEnabled  空否滚动>********/
photos.scrollEnabled = YES;// 默认允许
// 滚动条 显示/隐藏
photos.showsHorizontalScrollIndicator = NO;// 水平
photos.showsVerticalScrollIndicator = NO;//垂直
// 设置代理人
photos.delegate = self;
// 缩放
// 设置缩放比例
// 最小比例
photos.minimumZoomScale = 0.5;
// 最大比例
photos.maximumZoomScale = 2;
// 设置当前比例
photos.zoomScale = 1;
photos.tag = 2000;

endif

if 1

/******** PageControl 和 ScrollView 配合使用***********/

UIPageControl *page = [[UIPageControl alloc]    initWithFrame:CGRectMake(0, 600, 150, 30)];
//page.backgroundColor = [UIColor blackColor];
page.center = CGPointMake(self.view.center.x, page.center.y);
page.numberOfPages = 6;
page.tag = 1000;
[self.view addSubview:page];
// page.enabled = YES; 是否点击小图标变化
[page release];
// 添加事件
[page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

endif

}
// 页面控制器触发方法

  • (void)pageAction:(UIPageControl *)page
    {

    // 获取滚动视图
    UIScrollView *sv = (UIScrollView * )[self.view viewWithTag:2000];
    // 设置偏移量

    // sv.contentOffset = CGPointMake(page.currentPage * sv.frame.size.width, 0);// 没有动画效果

    [sv setContentOffset:CGPointMake(page.currentPage * sv.frame.size.width, 0 )animated:YES];// 有动画效果
    

    }

if 0

// 实现缩放响应方法
// 当缩放被触发时 哪个视图跟随变化

  • (UIView )viewForZoomingInScrollView:(UIScrollView )scrollView
    {
    // 让滚动视图上加载的第一张图片跟随缩放
    return scrollView.subviews.firstObject;
    }

endif

pragma mark - scrollView 滚动协议方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

// 只有视图滚动 就会一直触发该方法
// 任何偏移量的改变 都会触发该方法
NSLog(@"偏移量: %@", NSStringFromCGPoint(scrollView.contentOffset));
NSLog(@"滚啊滚");

}
// 一次完整的滚动非两个阶段: 拖追/减速

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {

    // 滚动开始执行的第一个方法
    // 滚动开始标志
    NSLog(@"将要开始");

    }

  • (void)scrollViewWillEndDragging:(UIScrollView )scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint )targetContentOffset
    {

    NSLog(@"将要结束");

    }

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {

    NSLog(@"已经结束");

    }

  • (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
    {

    NSLog(@"将要开始减速");

    }

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {

    // 滚动时 触发的最后一个方法
    // 滚动结束标识
    NSLog(@"已经结束减速");
    
    
    // 翻页结束
    // 获取page
    UIPageControl *page = (UIPageControl * )[self.view viewWithTag:1000];
    // 设置页数
    // 公式: 页数= x偏移量 / 单页宽度
    page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
    

    }

  • (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