UIScrollView

程序运行流程:

main->AppDelegate- >创建window- >根视图控制器ViewController(初始化init-->load view-->viewDidLoad)

注意:进图viewController之后,第一次访问视图(view)的时候(self.view),会判断是否已经创建了view,如果没有,接着走load view,viewDidLoad

UIScrollView可以实现在一个界面看到所有内容,同时也不需要担心所显示的内容超出屏幕的大小,当超出之后可以翻阅至下一页浏览。

一、创建

 1. CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ] ;  
 2. UIScrollView* scrollView = [ [UIScrollView alloc ] initWithFrame:bounds ]; 

当你创建完滚动视图后,你可以将另一个视图的内容粘合到滚动视图的空白页上。这回创建一个滚动的内容窗口:
[ scrollView addSubview:myView];   
你必须给出内容的实际大小,这样滚动视图才知道滚动的范围:
scrollView.contentSize = myView.frame.size; 
要开启缩放功能需要调整视图的两个属性,分别是maxinumZoomScale 和 mininumZoomScale 。这样就可以允许用户使用捏合手势调整内容大小:
 1. scrollView.maxinumZoomScale = 2.0;//允许放大2倍  
 2. scrollView.mininumZoomScale = 0.5;//允许放大到0.5倍  

要打开缩放功能,你还需要增加一个UIScrollViewDelegate 代理,通过一个名为 viewForZoomingScrollView的方法做出响应。这个方法会返回进行缩放时所使用的 UIView 对象:
 1. scrollView.delegate = self;  
 2. - (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{  
 3. <span style="white-space:pre">    </span>retutn myView;  
 4. }  

二、属性
除了上面用到的缩放属性外,滚动视图还有会让你多其他属性,可以对现实内容的行为进行微调。你可以对 UIScrollView 类进行多方位定制。下面这些属性是最常用的。
1.indicatorStyle
指定你希望使用的滚动条指示器的类型。默认行为是在白边界上绘制黑色的滚动条,这在大多数背景下都适用。可用风格如下:
 1. UIScrollViewIndicatorStyleDefault  
 2. UIScrollViewIndicatorStyleBlack  
 3. UIScrollViewIndicatorStyleWhite  

2.contentOffset
一个CGPoint结构,其中包含有要显示内容相对于窗口左上角的偏移量。默认是从0×0开始的,但是你也可以将显示内容放在其他位置。

3.directionalLockEnabled
默认行为是允许用户同时进行横向和纵向的滚动。将这个属性设置为YES会导致将用户的滚动行为锁定成只允许横向或纵向进行,具体方向由初始姿态决定。

4.bounces
当用户抵达滚动区域边缘时,这个功能允许用户稍微拖动到边界外一点。当用户松开手指后,这个区域会像个橡皮筋一样,弹回到原位,给用户一个可见的提示,表示他已经到达了文档开始或结束位置。如果不想让用户的滚动范围能够超出可见内容,可以将这个属性设置为NO。

5.bouncesZoom
类似于bounces 选项,这个方法可以令用户的缩放操作超出最大或最小缩放级别,然后弹回到范围以内。如果你不想让用户能够超出你指定的范围进行缩放,将这个属性设置为NO。

6.pagingEnabled
当开启了分页功能时,滚动视图被分割成多个独立区段,二用户的滚动体验则变成了页面翻转,你可以用这个属性来进行页面翻转。

三、委托方法
可以赋予滚动视图一个委托,下列委托方法会在特定时刻收到通知。
-(void)scrollViewDidScroll:(UIScrollView*)scrollView;//会在视图滚动时收到通知。包括一个指向被滚动视图的指针,从中可以读取contentOffset属性以确定其滚动到的位置。  

-(void)scrollViewWillBeginDragging:(UISCrollView*)scrollView;//当用户首次在某个方向上进行拖动时得到通知。这个方法会得到被作为参数传递的滚动视图指针,也同样可以从中读取 contentOffset 属性。  

-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate;  //当用户抬起拖动到手指时得到通知。还会得到一个布尔值,知名报告滚动视图最后位置之前,是否需要减速。  

-(void)scrollViewWillBeginDecelerate:(UIScrollView*)scrollView;//当用户抬起手指而视图需要继续移动时,会收到通知。这个方法可以用来读取 contentOffset属性,从而判断出当用户抬起手指钱最后一次滚动到的位置,虽然这个位置并不会使滚动条的最终停止位置。  

-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView;//当上面提到的减速完毕、滚动视图停止时得到通知。收到这个通知的时刻,滚动视图的contentOffset属性会反映出滚动条最终停止的位置。  

-(void)scrollViewDidEndZooming:(UIScrollview*)scrollView withView:(UIView*)view atScale:(float)scale;//当用户对内容进行缩放时,会收到通知。缩放比例表示为一个浮点值,会作为参数传递进来。 

-(BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView;  
-(void)scrollViewDidScrollToTop:(UIScrollView*)scrollView;  
//当用户点出iPhone状态条时,滚动视图委托可以决定视图是否滚动回到开头。



n张图片循环滚动显示 ,且和pageController的n个点匹配:

1.在要显示的第一张前面加一张(加的是要显示的最后一张),在要显示的最后一张后面加一张(加的是要显示的第一张)

因为循环滚动时,如果没有添加的那两张图片,第一页到最后一页和最后一页到第一页的过渡是空白的,滑动体验很不舒服

2.点击图片调到当前页

如果不写这句话,点击图片跳到对应图片的同时,它的currentPage总是0

second.page.currentPage = sender.tag - 101;

3.pageController的currentPage默认从0开始,和要显示的第一张图片的数值不对应,可以利用大的ScrollView的偏移量 除以 小ScrollView的宽度 再减1,来匹配

_page.currentPage = _bigScrollView.contentOffset.x/ scrollView.frame.size.width - 1;


创建相册:

AppDelegate:(.h)

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain, nonatomic) UIWindow *window;

@end

(.m)

#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [_window setBackgroundColor:[UIColor whiteColor]];

    [_window makeKeyAndVisible];

    [_window release];

    

    MainViewController *main = [[MainViewController alloc] initWithNibName:nil bundle:nil];

    [_window setRootViewController:main];

    [main release];


    return YES;

}


MainViewController(.h)

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@end

(.m)

#import "MainViewController.h"

#import "SecondViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)dealloc

{

    [super dealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }

    return self;

}

- (void)loadView

{

    [super loadView];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor magentaColor]];

   

    [self createButton];

 

}

- (void)createButton

{

//button上面放照片

    //点击显示大图片(可以缩放),大图片可以循环滑动

     //NSLog(@"%g",self.view.frame.size.height);//iPhone6 宽375,高度667

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button1 setFrame:CGRectMake(10, 20, 150, 150)];

    [button1 setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];

    [self.view addSubview:button1];

    [button1 release];

    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    button1.tag = 101;


    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button2 setFrame:CGRectMake(10, 190, 150, 150)];

    [button2 setBackgroundImage:[UIImage imageNamed:@"3.jpg"] forState:UIControlStateNormal];

    [self.view addSubview:button2];

    [button2 release];

    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    button2.tag = 102;


    

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button3 setFrame:CGRectMake(10, 360, 150, 150)];

    [button3 setBackgroundImage:[UIImage imageNamed:@"4.jpg"] forState:UIControlStateNormal];

    [self.view addSubview:button3];

    [button3 release];

    [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    button3.tag = 103;


    

    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button4 setFrame:CGRectMake(210, 20, 150, 150)];

    [button4 setBackgroundImage:[UIImage imageNamed:@"5.jpg"] forState:UIControlStateNormal];

    [self.view addSubview:button4];

    [button4 release];

    [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    button4.tag = 104;


    

    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button5 setFrame:CGRectMake(210, 190, 150, 150)];

    [button5 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateNormal];

    [self.view addSubview:button5];

    [button5 release];

[button5 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    button5.tag = 105;


    

    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button6 setFrame:CGRectMake(210, 360, 150, 150)];

    [button6 setBackgroundImage:[UIImage imageNamed:@"7.jpg"] forState:UIControlStateNormal];

    [self.view addSubview:button6];

    [button6 release];

    [button6 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    button6.tag = 106;

   

}

- (void)buttonAction:(UIButton *)sender

{

    SecondViewController *second = [[SecondViewController alloc] init];

    [self presentViewController:second animated:YES completion:^{

        NSLog(@"跳到第二个页面");

    }];

  //通过设置大scrollView的偏移量来实现 点击图片跳到当前页

    second.bigScrollView.contentOffset = CGPointMake((sender.tag - 100) * 375, 0);

    //如果不写这句话,点击图片k跳到图片k的同时,它的currentPage总是从0开始跳到k+1

    //tag(101-106)值减去101,对应currentPage的0-5

    second.page.currentPage = sender.tag - 101;

    //在黑的第二页显示大的图片


  }


SecondViewController(.h)

#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController

@property(nonatomic,retain) UIScrollView *bigScrollView;

@property(nonatomic,retain) UIPageControl *page;

@end

(.m)

#import "SecondViewController.h"

#import "MainViewController.h"

@interface SecondViewController ()<UIScrollViewDelegate>


@end


@implementation SecondViewController


- (void)dealloc

{

    [super dealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }

    return self;

}

- (void)loadView

{

    [super loadView];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor blackColor]];

    

    [self createControl];

    [self createBackButton];

    

}

- (void)createControl

{

    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 300)];

    _bigScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 8, 300);

    _bigScrollView.pagingEnabled = YES;

    _bigScrollView.delegate = self;

//    _bigScrollView.contentOffset = CGPointMake(375, 0);

    _bigScrollView.tag = 20;

    

    [self.view addSubview:_bigScrollView];

    [_bigScrollView release];


    for (int i = 1; i <= 8; i ++) {

        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width *(i - 1), 100, 375, 300)];

        [_bigScrollView addSubview:smallScrollView];

        smallScrollView.contentSize = CGSizeMake(375, 300);

        smallScrollView.pagingEnabled = YES;

        smallScrollView.minimumZoomScale = 0.5;

        smallScrollView.maximumZoomScale = 2.0;

        smallScrollView.tag = 10 + i;

        

        smallScrollView.delegate = self;

        [smallScrollView release];

        

        NSString *name = [NSString stringWithFormat:@"%d",i];//注意不写jpg

        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];

        UIImage *myImage = [UIImage imageWithContentsOfFile:path];

        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, smallScrollView.frame.size.width, smallScrollView.frame.size.height)];

        [imageview setImage:myImage];

        [smallScrollView addSubview:imageview];

        [imageview release];

        

        }

    

    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 500, 375, 30)];

    [_page setBackgroundColor:[UIColor clearColor]];

    _page.numberOfPages = 6;

    [self.view addSubview:_page];

    [_page release];

    //[_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];


}

//- (void)pageAction:(UIPageControl *)sender

//{

//    NSInteger num = sender.currentPage;

//    [_bigScrollView setContentOffset:CGPointMake(num * 375, 20) animated:YES];

//    

//}


//返回键

- (void)createBackButton

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];

    button.frame = CGRectMake(300, 550, 40, 40);

    [self.view addSubview:button];

    [button release];

    [button addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)backButtonAction:(id)sender

{

    MainViewController *first = [[MainViewController alloc] init];

    [self presentViewController:first animated:YES completion:^{

        NSLog(@"返回上一页");

    }];

  

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    //    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));

    //    NSLog(@"%s",__func__);

}

//将要停止拖拽

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0)

{

    

}

//已经停止拖拽drag

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{


}

//将要开始减速decelerate

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

{

    

}



//已经停止减速

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));

    //小的放大了可以滚动,大的本身就可以滚动

    //scrollView.zoomScale = 1.0;

    //显示当前page控点

   

     //循环显示

    if (_bigScrollView.contentOffset.x ==  375 * 7) {

        _bigScrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);

//        _page.currentPage = 1;

    }

    else if(_bigScrollView.contentOffset.x == 0)

    {

        _bigScrollView.contentOffset = CGPointMake(self.view.frame.size.width * 6, 0);

//        _page.currentPage = 5;

    }

     _page.currentPage = _bigScrollView.contentOffset.x/ scrollView.frame.size.width - 1;

    

    

    //遍历大scrollView,还原已经缩放的小scrollView

        //断点调试,PO断点设置好后,PO所有的东西,看哪里不对

        //一定要经常PO自己排查错误

    

    

    //缩放还原 法一:用遍历到的图片类型进行判断

//    if (scrollView == _bigScrollView) {

//        for (UIScrollView *sv in _bigScrollView.subviews) {

//            if ([sv isKindOfClass:[UIScrollView class]]) {

//                sv.zoomScale = 1.0f;

//            }

//        }

//    }

    //法二:用tag值

    if (scrollView == _bigScrollView) {

        for (int i = 1; i < 9; i++) {

            int num = 10 + i;//数字跟上面设置的tag值对应,找到目标

            UIScrollView *sc = (UIScrollView *)[_bigScrollView viewWithTag:num];

            [sc setZoomScale:1.0f];

            }

    }

}

#pragma mark -

#pragma mark 放大协议

//告诉scrollview你将要放大的图片

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    NSArray *array = scrollView.subviews;

    return [array objectAtIndex:0];

//    return [array firstObject];//总是返回第一页

    

}








  
















  










 



















  

  












 

 

















 



 

    

  

   



 






   


 

    

   









  
















  










 



















  

  












 

 















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值