UIScrollView
#import "RootViewController.h"
//定义保存屏幕宽度和高度的宏
#define kWidth self.view.frame.size.width
#define kHeidth self.view.frame.size.height
@interface RootViewController ()
@end
@implementation RootViewController
#pragma mark ------- 代理检测滚动过程中触发的方法----
//滚动过程中触发的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"正在滚动");
}
//即将开始拖拽,此时滚动视图即将加速
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
NSLog(@"即将开始拖拽");
}
//结束拖拽,不再加速
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"拖拽结束");
}
//即将开始减速
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
NSLog(@"即将开始减速");
}
//结束减速,也就是停止滚动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"结束滚动");
// 先从负责滚动的⼤scrollView找到他的⼦视图
for (UIScrollView *sView in scrollView.subviews){
// 在根据⼦类的对象类型进⾏判断
if ([sView isKindOfClass:[UIScrollView class]]){
// 把视图的尺⼨恢复到原有尺⼨
sView.zoomScale = 1.0;
}
}
}
//指定缩放视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subviews firstObject];
}
//中心缩放
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
UIImageView *imageView = [scrollView.subviews firstObject];
if (scrollView.zoomScale <= 1.0) {
imageView.center = self.view.center ;
}
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize));//只是打印
}
- (void)viewDidLoad {
[super viewDidLoad];
//创建滚动视图
int a = 7;
CGFloat totalWidth = self.view.frame.size.width;
CGFloat totalHeight = self.view.frame.size.height;
UIScrollView *scroll = [[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
scroll.backgroundColor = [UIColor grayColor];
//设置代理
scroll.delegate = self;
/**
如果想实现滚动视图的缩放功能,必须指定缩放的视图以及缩放的比例
指定缩放视图需要实现代理方法,指定缩放比例,shhiwei滚动设置缩放比例
*/
// scroll.minimumZoomScale = 0.5;//最小缩放比例
// scroll.maximumZoomScale = 2.0;//最大缩放比例
[self.view addSubview:scroll];
//设置滚动视图的相关属性
//1、滚动区域,内容区域的大小
//如果想要实现滚动操作,有三种情况
/*
1、内容视图的宽(滚动区域的宽大于滚动视图的宽)
2、内容视图(滚动区域)的高大于滚动视图的高
3、内容视图(滚动区域)的宽和高大于滚动视图的宽和高
*/
scroll.contentSize = CGSizeMake(totalWidth * a,0);
scroll.contentOffset =CGPointMake(0, 0); //偏移量
//设置整页滚动
scroll.pagingEnabled = YES;
//关闭横向显示条
// scroll.showsHorizontalScrollIndicator = NO;
//关闭滚动视图的回弹效果
scroll.bounces = NO;
//for循环创建七个UIImageView并且添加到滚动视图上去
for (int i = 0; i < a; i++) {
UIImageView *imageView =
[[UIImageView alloc]
initWithFrame:CGRectMake(0, 0,
totalWidth, totalHeight)];
imageView.image = [UIImage
imageNamed:[NSString stringWithFormat:@"%d.jpg", i + 1]];
//先创建小
UIScrollView *scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(totalWidth * i,0 , totalWidth, totalHeight)];
scroll1.delegate = self;
scroll1.minimumZoomScale = 0.5;
scroll1.maximumZoomScale = 2.0;
[scroll1 addSubview:imageView];
[scroll addSubview:scroll1];
[imageView release];
[scroll1 release];
}
// Do any additional setup after loading the view.
}