----
创建一个
UIScrollView
UIScrollView
*scrollView = [[
UIScrollView
alloc
]
initWithFrame
:[
UIScreen
mainScreen
].
bounds
];
scrollView. backgroundColor = [ UIColor redColor ];
[ self . view addSubview :scrollView];
scrollView. tag = 2 ;
[scrollView release ];
scrollView. backgroundColor = [ UIColor redColor ];
[ self . view addSubview :scrollView];
scrollView. tag = 2 ;
[scrollView release ];
----添加一张图片到scorllview上去
UIImageView
*imageView = [[
UIImageView
alloc
]
initWithFrame
:[
UIScreen
mainScreen
].
bounds
];
imageView. image = [ UIImage imageNamed : @"faye1" ];
[scrollView addSubview :imageView];
imageView. tag = 1 ;
[imageView release ];
imageView. image = [ UIImage imageNamed : @"faye1" ];
[scrollView addSubview :imageView];
imageView. tag = 1 ;
[imageView release ];
----比较重要的属性
// 1
显示范围
能滑动的范围
// 左右改变宽
scrollView. contentSize = CGSizeMake ( kScreenWidth * 2 , kScreenHeight * 1 );
// 这样用户体验特别垃圾 会出现卡半屏的情况 调用一滚一屏幕的属性 超过一半就换
scrollView. pagingEnabled = YES ;
scrollView. alwaysBounceVertical = NO ;
scrollView. alwaysBounceHorizontal = YES ;
// 边界回弹
scrollView. bounces = YES ;
// 水平滚动条是否显示
scrollView. showsHorizontalScrollIndicator = YES ;
// 垂直滚动条是否显示
scrollView. showsVerticalScrollIndicator = YES ;
// 重要属性 默认是 0,0 滑动的左上角的点 距离原点 的位置
// scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
// 控制缩放的属性
scrollView. maximumZoomScale = 3.0 ;
scrollView. minimumZoomScale = 0.5 ;
// 最重要的就是一堆代理方法
// 左右改变宽
scrollView. contentSize = CGSizeMake ( kScreenWidth * 2 , kScreenHeight * 1 );
// 这样用户体验特别垃圾 会出现卡半屏的情况 调用一滚一屏幕的属性 超过一半就换
scrollView. pagingEnabled = YES ;
scrollView. alwaysBounceVertical = NO ;
scrollView. alwaysBounceHorizontal = YES ;
// 边界回弹
scrollView. bounces = YES ;
// 水平滚动条是否显示
scrollView. showsHorizontalScrollIndicator = YES ;
// 垂直滚动条是否显示
scrollView. showsVerticalScrollIndicator = YES ;
// 重要属性 默认是 0,0 滑动的左上角的点 距离原点 的位置
// scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
// 控制缩放的属性
scrollView. maximumZoomScale = 3.0 ;
scrollView. minimumZoomScale = 0.5 ;
// 最重要的就是一堆代理方法
scrollView.delegate = self;
UIButton *returnButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
returnButton.
backgroundColor
= [
UIColor
greenColor
];
[returnButton addTarget : self action : @selector (actionReturnButton:) forControlEvents :( UIControlEventTouchUpInside )];
returnButton. frame = CGRectMake ( 10 , 10 , 50 , 30 );
[returnButton addTarget : self action : @selector (actionReturnButton:) forControlEvents :( UIControlEventTouchUpInside )];
returnButton. frame = CGRectMake ( 10 , 10 , 50 , 30 );
[self.view addSubview:returnButton];
#pragma mark -- UIScrollDelegate--
- ( void )scrollViewDidScroll:( UIScrollView *)scrollView
{
NSLog ( @" 你拖动我就触发 " );
}
- ( void )scrollViewWillBeginDecelerating:( UIScrollView *)scrollView
{
NSLog ( @" 将要开始减速 " );
}
- ( void )scrollViewDidEndDecelerating:( UIScrollView *)scrollView
{
NSLog ( @" 已经结束减速 " );
}
- ( void )scrollViewWillBeginDragging:( UIScrollView *)scrollView
{
NSLog ( @" 将要开始拖拽 " );
}
- ( void )scrollViewDidEndDragging:( UIScrollView *)scrollView willDecelerate:( BOOL )decelerate
{
NSLog ( @" 已经结束拖拽 " );
}
/**
* 缩放
*/
- ( void )scrollViewWillBeginZooming:( UIScrollView *)scrollView withView:( UIView *)view
{
NSLog ( @" 将要开始缩放 " );
}
- ( void )scrollViewDidEndZooming:( UIScrollView *)scrollView withView:( UIView *)view atScale:( CGFloat )scale
{
NSLog ( @" 已经结束缩放 " );
}
- ( void )scrollViewDidZoom:( UIScrollView *)scrollView
{
UIImageView *view1 = ( UIImageView *)[ self . view viewWithTag : 1 ];
view1. center = self . view . center ;
NSLog ( @" 一缩放 就触发 " );
}
- ( void )scrollViewDidScroll:( UIScrollView *)scrollView
{
NSLog ( @" 你拖动我就触发 " );
}
- ( void )scrollViewWillBeginDecelerating:( UIScrollView *)scrollView
{
NSLog ( @" 将要开始减速 " );
}
- ( void )scrollViewDidEndDecelerating:( UIScrollView *)scrollView
{
NSLog ( @" 已经结束减速 " );
}
- ( void )scrollViewWillBeginDragging:( UIScrollView *)scrollView
{
NSLog ( @" 将要开始拖拽 " );
}
- ( void )scrollViewDidEndDragging:( UIScrollView *)scrollView willDecelerate:( BOOL )decelerate
{
NSLog ( @" 已经结束拖拽 " );
}
/**
* 缩放
*/
- ( void )scrollViewWillBeginZooming:( UIScrollView *)scrollView withView:( UIView *)view
{
NSLog ( @" 将要开始缩放 " );
}
- ( void )scrollViewDidEndZooming:( UIScrollView *)scrollView withView:( UIView *)view atScale:( CGFloat )scale
{
NSLog ( @" 已经结束缩放 " );
}
- ( void )scrollViewDidZoom:( UIScrollView *)scrollView
{
UIImageView *view1 = ( UIImageView *)[ self . view viewWithTag : 1 ];
view1. center = self . view . center ;
NSLog ( @" 一缩放 就触发 " );
}
--------------
*
缩放中最重要的方法
1. 指定 scrollview 的子视图进行缩放
2. 返回的就是 scrollview 的子视图
3. 可以写属性 可以用 tag 值去取 还可以用 for in 进行遍历 还可以用下面的方法
*/
- ( UIView *)viewForZoomingInScrollView:( UIScrollView *)scrollView
{
return scrollView. subviews [ 0 ];
}
/**
* 点击按钮 返回原图大小
*/
#pragma mark -------- 缩放
/**
* 1. 缩放的是 scorllView
2. 缩放的属性是给 scorllview 设置的
3. 是通过缩放 scorllview 改变其指定的子视图的大小
4. 那么 我们要是一个相册 的每张图片都能缩放 如何操作 ?
5. 我们需要一个大的 bgscorllview 控制左右滑动
6. 然后有一堆 subScorllview 上面分别加上图片
7. 将这堆 subScorllview 加到大的 bgscorllview 上面
8. subScorllview 控制缩放
*/
- ( void )actionReturnButton:( UIButton *)button
{
// 先把 scroll 的 VIew 取出来
UIScrollView *view2 = ( UIScrollView *)[ self . view viewWithTag : 2 ];
[view2 setZoomScale : 1 animated : YES ];
//view2.zoomScale = 1;
/**
1. 指定 scrollview 的子视图进行缩放
2. 返回的就是 scrollview 的子视图
3. 可以写属性 可以用 tag 值去取 还可以用 for in 进行遍历 还可以用下面的方法
*/
- ( UIView *)viewForZoomingInScrollView:( UIScrollView *)scrollView
{
return scrollView. subviews [ 0 ];
}
/**
* 点击按钮 返回原图大小
*/
#pragma mark -------- 缩放
/**
* 1. 缩放的是 scorllView
2. 缩放的属性是给 scorllview 设置的
3. 是通过缩放 scorllview 改变其指定的子视图的大小
4. 那么 我们要是一个相册 的每张图片都能缩放 如何操作 ?
5. 我们需要一个大的 bgscorllview 控制左右滑动
6. 然后有一堆 subScorllview 上面分别加上图片
7. 将这堆 subScorllview 加到大的 bgscorllview 上面
8. subScorllview 控制缩放
*/
- ( void )actionReturnButton:( UIButton *)button
{
// 先把 scroll 的 VIew 取出来
UIScrollView *view2 = ( UIScrollView *)[ self . view viewWithTag : 2 ];
[view2 setZoomScale : 1 animated : YES ];
//view2.zoomScale = 1;
/**
* *******-******上面的方法还有动画,下面的不高大-上******************************
*/
// UIImageView *view1 = (UIImageView *)[self.view viewWithTag:1];
// view1.frame = self.view.frame;
// UIImageView *view1 = (UIImageView *)[self.view viewWithTag:1];
// view1.frame = self.view.frame;
}
==========================================================================
-------创建一个
UIpageControl
-
创建的时候一定要够宽
够现实所有的点
UIPageControl
*pageControl = [[
UIPageControl
alloc
]
initWithFrame
:
CGRectMake
(
0
,
100
,
375
,
100
)];
pageControl. backgroundColor = [ UIColor blueColor ];
[ self . view addSubview :pageControl];
[pageControl release ];
pageControl. backgroundColor = [ UIColor blueColor ];
[ self . view addSubview :pageControl];
[pageControl release ];
-设置总页数
pageControl.
numberOfPages
=
10
;
-默认的颜色 (未选中)
pageControl.
pageIndicatorTintColor
= [
UIColor
greenColor
];
-当前页面的颜色
pageControl.
currentPageIndicatorTintColor
= [
UIColor
blackColor
];
-当前选中第几个
pageControl.
currentPage
=
10
;
-添加点击方法
[pageControl
addTarget
:
self
action
:
@selector
(actionPageControl:)
forControlEvents
:(
UIControlEventValueChanged
)];
- (
void
)actionPageControl:(
UIPageControl
*)pageControl
{
{
}
===================================相册==========================
- (
void
)addSubViews
{
// 创建一个 ~ 大 S~ 出来
UIScrollView *scorllView = [[ UIScrollView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
scorllView. tag = kBGScrollViewTag ;
// 滑动区域
scorllView. contentSize = CGSizeMake ( kScreenWidth * 6 , kScreenHeight );
scorllView. backgroundColor = [ UIColor whiteColor ];
[ self . view addSubview :scorllView];
// 给 大 S 设置代理 触发滑动的代理方法
scorllView. delegate = self ;
// 整屏滚动
scorllView. pagingEnabled = YES ;
// 循环创建 ~~ 小 S~~
for ( int i = 0 ; i < 6 ; i++) {
UIScrollView *smallScrollView = [[ UIScrollView alloc ] initWithFrame :
CGRectMake ( kScreenWidth * i, 0 , kScreenWidth , kScreenHeight )];
smallScrollView. backgroundColor = [ UIColor greenColor ];
// 设置缩放属性
smallScrollView. minimumZoomScale = 0.5 ;
smallScrollView. maximumZoomScale = 2.0 ;
NSString *imageNameString = [ NSString stringWithFormat : @"faye%d" , i + 1 ];
UIImageView *imageView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , kScreenWidth , kScreenHeight )];
imageView. image = [ UIImage imageNamed :imageNameString];
[smallScrollView addSubview :imageView];
[imageView release ];
[scorllView addSubview :smallScrollView];
[smallScrollView release ];
imageView. tag = i + 1 ;
smallScrollView. tag = kSmallScrollViewTag + i;
smallScrollView. delegate = self ;
}
// 创建一个 pageConrol
UIPageControl *pageControl = [[ UIPageControl alloc ] initWithFrame : CGRectMake ( 0 , kScreenHeight - 50 , kScreenWidth , 50 )];
pageControl. numberOfPages = 6 ;
pageControl. currentPage = 0 ;
pageControl. currentPageIndicatorTintColor = [ UIColor blackColor ];
pageControl. pageIndicatorTintColor = [ UIColor grayColor ];
[ self . view addSubview :pageControl];
[pageControl addTarget : self action : @selector (actionPageControl:) forControlEvents :( UIControlEventValueChanged )];
// 加上 tag 值
pageControl. tag = 888 ;
}
// 最重要的方法
- ( UIView *)viewForZoomingInScrollView:( UIScrollView *)scrollView
{
{
// 创建一个 ~ 大 S~ 出来
UIScrollView *scorllView = [[ UIScrollView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
scorllView. tag = kBGScrollViewTag ;
// 滑动区域
scorllView. contentSize = CGSizeMake ( kScreenWidth * 6 , kScreenHeight );
scorllView. backgroundColor = [ UIColor whiteColor ];
[ self . view addSubview :scorllView];
// 给 大 S 设置代理 触发滑动的代理方法
scorllView. delegate = self ;
// 整屏滚动
scorllView. pagingEnabled = YES ;
// 循环创建 ~~ 小 S~~
for ( int i = 0 ; i < 6 ; i++) {
UIScrollView *smallScrollView = [[ UIScrollView alloc ] initWithFrame :
CGRectMake ( kScreenWidth * i, 0 , kScreenWidth , kScreenHeight )];
smallScrollView. backgroundColor = [ UIColor greenColor ];
// 设置缩放属性
smallScrollView. minimumZoomScale = 0.5 ;
smallScrollView. maximumZoomScale = 2.0 ;
NSString *imageNameString = [ NSString stringWithFormat : @"faye%d" , i + 1 ];
UIImageView *imageView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , kScreenWidth , kScreenHeight )];
imageView. image = [ UIImage imageNamed :imageNameString];
[smallScrollView addSubview :imageView];
[imageView release ];
[scorllView addSubview :smallScrollView];
[smallScrollView release ];
imageView. tag = i + 1 ;
smallScrollView. tag = kSmallScrollViewTag + i;
smallScrollView. delegate = self ;
}
// 创建一个 pageConrol
UIPageControl *pageControl = [[ UIPageControl alloc ] initWithFrame : CGRectMake ( 0 , kScreenHeight - 50 , kScreenWidth , 50 )];
pageControl. numberOfPages = 6 ;
pageControl. currentPage = 0 ;
pageControl. currentPageIndicatorTintColor = [ UIColor blackColor ];
pageControl. pageIndicatorTintColor = [ UIColor grayColor ];
[ self . view addSubview :pageControl];
[pageControl addTarget : self action : @selector (actionPageControl:) forControlEvents :( UIControlEventValueChanged )];
// 加上 tag 值
pageControl. tag = 888 ;
}
// 最重要的方法
- ( UIView *)viewForZoomingInScrollView:( UIScrollView *)scrollView
{
-咱们指缩放的是小的 进来这个方法的只会是小S
if
(scrollView.
tag
!=
kBGScrollViewTag
) {
return scrollView. subviews [ 0 ];
}
return nil ;
}
- ( void )scrollViewDidZoom:( UIScrollView *)scrollView
{
return scrollView. subviews [ 0 ];
}
return nil ;
}
- ( void )scrollViewDidZoom:( UIScrollView *)scrollView
{
-按中心点进行缩放
// UIImageView *imageView = scrollView.subviews[0];
// imageView.center = self.view.center;
for ( int i = 0 ; i < 6 ; i++) {
UIImageView *view1 = ( UIImageView *)[ self . view viewWithTag : 1 + i];
view1. center = self . view . center ;
}
// NSLog(@" 正在缩放 ");
}
- ( void )scrollViewDidEndZooming:( UIScrollView *)scrollView withView:( UIView *)view atScale:( CGFloat )scale
{
// NSLog(@" 已经结束 ");
}
-( void )scrollViewWillBeginZooming:( UIScrollView *)scrollView withView:( UIView *)view
{
// NSLog(@" 将要开始 ");
}
// imageView.center = self.view.center;
for ( int i = 0 ; i < 6 ; i++) {
UIImageView *view1 = ( UIImageView *)[ self . view viewWithTag : 1 + i];
view1. center = self . view . center ;
}
// NSLog(@" 正在缩放 ");
}
- ( void )scrollViewDidEndZooming:( UIScrollView *)scrollView withView:( UIView *)view atScale:( CGFloat )scale
{
// NSLog(@" 已经结束 ");
}
-( void )scrollViewWillBeginZooming:( UIScrollView *)scrollView withView:( UIView *)view
{
// NSLog(@" 将要开始 ");
}
#pragma mark ---
滑动的代理方法
-在结束减速的时候触发
pagecontrol
原点的位置
- (
void
)scrollViewDidEndDecelerating:(
UIScrollView
*)scrollView
{
{
-改变PageControl 的圆点位置
UIPageControl
*pageControl1 = (
UIPageControl
*)[
self
.
view
viewWithTag
:
888
];
pageControl1. currentPage = scrollView. contentOffset . x / 375 ;
for ( int i = 0 ; i < 6 ; i++) {
UIScrollView *lastView = ( UIScrollView *)[ self . view viewWithTag : 2000 + i];
[lastView setZoomScale : 1.0 animated : YES ];
}
}
- ( void )actionPageControl:( UIPageControl *)pageControl
{
UIScrollView *view1 = ( UIScrollView *)[ self . view viewWithTag : kBGScrollViewTag ];
[view1 setContentOffset : CGPointMake ( kScreenWidth * pageControl. currentPage , 0 ) animated : YES ];
pageControl1. currentPage = scrollView. contentOffset . x / 375 ;
for ( int i = 0 ; i < 6 ; i++) {
UIScrollView *lastView = ( UIScrollView *)[ self . view viewWithTag : 2000 + i];
[lastView setZoomScale : 1.0 animated : YES ];
}
}
- ( void )actionPageControl:( UIPageControl *)pageControl
{
UIScrollView *view1 = ( UIScrollView *)[ self . view viewWithTag : kBGScrollViewTag ];
[view1 setContentOffset : CGPointMake ( kScreenWidth * pageControl. currentPage , 0 ) animated : YES ];
}
======================循环播放==============
- (
void
)addSubViews
{
UIScrollView *bgScrollView = [[ UIScrollView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
bgScrollView. contentSize = CGSizeMake ( kScreenWidth * ( 3 + 2 ), kScreenHeight );
bgScrollView. backgroundColor = [ UIColor whiteColor ];
bgScrollView. delegate = self ;
{
UIScrollView *bgScrollView = [[ UIScrollView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
bgScrollView. contentSize = CGSizeMake ( kScreenWidth * ( 3 + 2 ), kScreenHeight );
bgScrollView. backgroundColor = [ UIColor whiteColor ];
bgScrollView. delegate = self ;
-一上来就显示第二张照片
bgScrollView. contentOffset = CGPointMake ( kScreenWidth , 0 );
[ self . view addSubview :bgScrollView];
bgScrollView. pagingEnabled = YES ;
[bgScrollView release];
- 循环小的
for
(
int
i =
0
; i <
5
; i++) {
UIScrollView *smallScrollView = [[ UIScrollView alloc ] initWithFrame : CGRectMake ( kScreenWidth * i, 0 , kScreenWidth , kScreenHeight )];
UIScrollView *smallScrollView = [[ UIScrollView alloc ] initWithFrame : CGRectMake ( kScreenWidth * i, 0 , kScreenWidth , kScreenHeight )];
-设置缩放属性
smallScrollView.
minimumZoomScale
=
0.5
;
smallScrollView. maximumZoomScale = 2.0 ;
[bgScrollView addSubview :smallScrollView];
[smallScrollView release ];
// 忍得住寂寞 守得住繁华
smallScrollView. maximumZoomScale = 2.0 ;
[bgScrollView addSubview :smallScrollView];
[smallScrollView release ];
// 忍得住寂寞 守得住繁华
- 创建imageView
UIImageView
*imageView = [[
UIImageView
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
kScreenWidth
,
kScreenHeight
)];
-添加图片
-等于
0
就显示第三张
等于
4
就显示第一张
if (i == 0 ) {
imageView. image = [ UIImage imageNamed : @"3" ];
} else if (i == 4 ) {
imageView. image = [ UIImage imageNamed : @"1" ];
} else {
NSString *imageName = [ NSString stringWithFormat : @"%d" , i];
imageView. image = [ UIImage imageNamed :imageName];
}
[smallScrollView addSubview :imageView];
[imageView release ];
}
}
#pragma mark - 滑动代理方法
- ( void )scrollViewDidEndDecelerating:( UIScrollView *)scrollView
{
-结束减速后(停止时 开始骗人)
-获取当前是第几张
NSInteger
index = scrollView.
contentOffset
.
x
/
kScreenWidth
;
if (index == 4 ) {
scrollView. contentOffset = CGPointMake ( kScreenWidth , 0 );
} else if (index == 0 ) {
scrollView. contentOffset = CGPointMake ( kScreenWidth * 3 , 0 );
}
if (index == 4 ) {
scrollView. contentOffset = CGPointMake ( kScreenWidth , 0 );
} else if (index == 0 ) {
scrollView. contentOffset = CGPointMake ( kScreenWidth * 3 , 0 );
}
}