UIScrollView有滚动和缩放两个特征,一般滚动属性要和UIPagecontrol结合使用
//
// MainViewController.m
// UI_lesson08
//
// Created by xalo on 15/10/23.
// Copyright (c) 2015年 李见辉. All rights reserved.
//
#import "MainViewController.h"
#define aColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0
@interface MainViewController ()<UIScrollViewDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
#pragma mark==========================UIScrollView滚动属性==============================
UIScrollView *aScrollView=[[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]autorelease];
aScrollView.backgroundColor=[UIColor aColor];
[self.view addSubview:aScrollView];
//滚动视图的子视图
NSArray *titles=@[@"测",@"试",@"文",@"字"];
for (int i=0; i<4; i++) {
UILabel *aLabel=[[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)*i, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))]autorelease];
aLabel.text=titles[i];
aLabel.textColor=[UIColor aColor];
aLabel.backgroundColor=[UIColor aColor];
aLabel.textAlignment=NSTextAlignmentCenter;
aLabel.font=[UIFont systemFontOfSize:375 weight:600];
aLabel.font=[UIFont fontWithName:@"Wawati SC" size:375];
[aScrollView addSubview:aLabel];
}
//通过设置滑动视图的内容区域大小来让scrollView产生滑动
//scrollView横向滑动取决于内容区域的宽度大于自身的宽度,内容区域的高度不大于自身的高度,scrollView的纵向滑动取决于内容区域的高度大于自身的高度,内容区域的宽度不大于自身的宽度
aScrollView.contentSize=CGSizeMake(CGRectGetWidth(self.view.bounds)*4, CGRectGetHeight(self.view.bounds));//滑动范围
aScrollView.contentOffset=CGPointMake(0, 0);//位置偏移量(scrollView自身的坐标轴距离左上角的相对位置,在滑动的过程中contentOffset会因为平移手势而产生改变。contentOffset的值与scrollView的bounds的orgin值一样)
//滑动视图的scrollToTop可以控制轻触电池条时让scrollView纵向到原始位置。也叫回到顶部,横向滑动时并没有此功能,默认此功能为YES,表示轻触回到顶部
aScrollView.scrollsToTop=YES;
//通过pagingEnabled属性可以控制视图在滑动的时候整屏滑动(一次滑动的偏移量是scrollView的宽度或者高度)
aScrollView.pagingEnabled=YES;//默认为NO表示连续滑动
aScrollView.bounces=YES;//控制scrollView的边缘动画是否开启(默认YES开启)
//设置横向和纵向滑动指示器是否显示,默认为显示状态(在滑动的过程中才会显示)
aScrollView.showsHorizontalScrollIndicator=NO;//水平的
aScrollView.showsVerticalScrollIndicator=NO;//竖直的
#pragma mark==============================================================================
aScrollView.delegate=self;//遵守代理对象
#pragma mark====================UIPageControl页面控制========================================
UIPageControl *pageControl=[[[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-80, CGRectGetWidth(self.view.bounds), 40)]autorelease];
//pageControl.backgroundColor=[UIColor blackColor];
//设置pageControl的总页数,与当前的scollView当前的子视图数量相同
pageControl.numberOfPages=4;
//设置当前的页码,与当前的scrollview当前显示的第几个视图有关,从0开始
pageControl.currentPage=0;
//设置pageControl的外观颜色
pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];//当前页码指示器的前景色
pageControl.pageIndicatorTintColor=[UIColor brownColor];//页码指示器的前景色
[self.view addSubview:pageControl];
[pageControl addTarget:self action:@selector(handlePageControlAction:) forControlEvents:UIControlEventValueChanged];//绑定响应方法
}
//UIScrollView滚动代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{//滚动触发
NSLog(@"%s",__FUNCTION__);
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{//开始拖拽时触发
NSLog(@"%s",__FUNCTION__);
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"%s",__FUNCTION__);
}//结束拖拽时触发
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"%s",__FUNCTION__);
}//开始减速时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"%s",__FUNCTION__);
//通过偏移量除以一次偏移的大小得到页码
NSInteger index=scrollView.contentOffset.x/CGRectGetWidth(scrollView.bounds);
//通过快速枚举子视图数组得到对应的控件
for (id object in self.view.subviews) {
//判断对应的类型
if ([object isKindOfClass:[UIPageControl class]]) {
[(UIPageControl *)object setCurrentPage:index];
}
}
}//停止(减速)时触发
//联动效果
-(void)handlePageControlAction:(UIPageControl *)sender{
//通过currenPage和一次滑动的增量得到偏移量
CGPoint offset=CGPointZero;
offset.x=sender.currentPage *CGRectGetWidth(self.view.bounds);
//通过快速枚举视图的子视图数组得到对应的scrollView修改其偏移量
for (id object in self.view.subviews) {
if ([object isKindOfClass:[UIScrollView class]]) {
[(UIScrollView *)object setContentOffset:offset animated:YES];
}
}
}
//
// ZoomViewController.m
// UI_lesson08
//
// Created by xalo on 15/10/23.
// Copyright (c) 2015年 李见辉. All rights reserved.
//
#import "ZoomViewController.h"
@interface ZoomViewController ()<UIScrollViewDelegate>
@end
@implementation ZoomViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
#pragma mark==========================UIScrollView缩放属性==============================
UIScrollView *aScrollView=[[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]autorelease];
[self.view addSubview:aScrollView];
UIImage *image=[UIImage imageNamed:@"/Users/xalo/Desktop/李见辉/UI/UI课程/UI_lesson08/UI_lesson08/a.jpg"];
CGFloat width=CGRectGetWidth(self.view.bounds);
CGFloat height=width*image.size.height/image.size.width;//根据视图的原始宽高比得到该宽度对应的高度
UIImageView *imageView=[[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)]autorelease];
imageView.center=self.view.center;
imageView.image=image;
[aScrollView addSubview:imageView];
//设置scrollView的缩放的相关属性
//设置最小缩放比例
aScrollView.minimumZoomScale=0.5;//50%
//设置最大缩放比例
aScrollView.maximumZoomScale=2.0;//200%
//为imageView设置tag
imageView.tag=123;
//为scrollView设置代理
aScrollView.delegate=self;
}
//缩放过程中会重复响应此协议的方法
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize));
UIImageView *imageView=(UIImageView *)[scrollView viewWithTag:123];
CGFloat content_width=scrollView.contentSize.width;
CGFloat content_height=scrollView.contentSize.height;
CGFloat width=CGRectGetWidth(scrollView.bounds);
CGFloat height=CGRectGetHeight(scrollView.bounds);
//计算缩放产生的x和y轴的增量
CGFloat delta_x=width>content_width?(width-content_width)/2:0;
CGFloat delta_y=height>content_height?(height-content_height)/2:0;
//重新设置视图的中心点
imageView.center=CGPointMake(content_width/2+delta_x, content_height/2+delta_y);
}
//缩放响应
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:123];
}