ScrollView(创建滚动View)

来源:互联网 发布:网络直播平台列表 编辑:程序博客网 时间:2024/05/01 08:09

第一步:

创建一个LoopScrollView类

LoopScrollView.h

#import <UIKit/UIKit.h>@interface LoopScrollView : UIView <UIScrollViewDelegate>//需要协议中的方法,用于UIPageControl的变化@property (strong , nonatomic)NSArray *imageNames;//用于存放contentView里的图片@end
LoopScreoll#import "LoopScrollView.h"

#import "LoopScrollView.h"@implementation LoopScrollView{    UIScrollView *imageScrollView;    UIPageControl *pageControl;//ScrollView底部的滚动点}//重写父类的setter方法、注意方法名不能写错!- (void) setImageNames:(NSArray *)imageNames{    _imageNames = imageNames;    [self createScrollView];}- (void) createScrollView{    CGSize size = self.frame.size;    //设置显示窗口    imageScrollView = [[UIScrollView alloc] initWithFrame:self.frame];    imageScrollView.showsHorizontalScrollIndicator = NO;//两边是否有滚动条    imageScrollView.showsVerticalScrollIndicator = NO;    imageScrollView.pagingEnabled = YES;//是否是整页翻    imageScrollView.delegate = self;    [self addSubview:imageScrollView];    //循环显示时,实际上多添加了一张图片到最前    UIImageView *firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];    firstImage.image = [UIImage imageNamed:_imageNames.lastObject];    [imageScrollView addSubview:firstImage];    //循环添加imageView到ScrollView上    for (int i = 0; i < _imageNames.count; i++) {        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i + 1) * size.width, 0, size.width, size.height)];        imageView.image = [UIImage imageNamed:_imageNames[i]];        [imageScrollView addSubview:imageView];    }    //设置内容View的大小    [imageScrollView setContentSize:CGSizeMake(size.width * (_imageNames.count + 1), size.height)];    //设置内容View的偏移量(起始位置),因为需要循环滚动,看到的第一张图片,起始是原始View的第二张图片    [imageScrollView setContentOffset:CGPointMake(size.width, 0)];        //设置ScrollView    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(size.width / 2 - 50, size.height - 50, 100, 30)];    pageControl.numberOfPages = _imageNames.count;    //这里的self指的是ScrollView    [self addSubview:pageControl];    //设置时间量    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(loopAnimation:) userInfo:nil repeats:YES];        }- (void) loopAnimation: (NSTimer *)timer{    CGSize size = self.frame.size;    //取出内容View的偏移量    CGPoint offSet = imageScrollView.contentOffset;    CGFloat x = offSet.x;    //当滚动到最后一张图的时候,回到第一张,    if (x == size.width * _imageNames.count ) {        //设置显示原点        [imageScrollView setContentOffset:CGPointMake(0, 0)];        //紧接着就让它滚动到第二张图,(解决最后一张显示时间长的问题)        [imageScrollView setContentOffset:CGPointMake(size.width, 0) animated:YES];    }    //滚动到可视位置, x2 = x1 + size.width;    [imageScrollView scrollRectToVisible:CGRectMake(x + size.width, 0, size.width, size.height) animated:YES];}// 更新pagecontrol<pre name="code" class="objc">- (void)pageUpdate{    //获取当前页面的x值    CGPoint offSet = imageScrollView.contentOffset;    CGFloat x = offSet.x;    int pag = (int)(x / self.frame.size.width) - 1;//判断是哪一页,因为数组中多加了第一个元素    pageControl.currentPage = pag;//当前页//    if (pag == _imageNames.count - 1) {//        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(loopAnimation:) userInfo:nil repeats:YES];//    }}
//协议中的方法:当结束一次滚动之前,减速的时候,执行...
//个人认为是在用手滑动有弹性的页面的时候才可被执行
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self pageUpdate];}
//协议中的方法:当结束一次滚动的时候,执行...
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ [self pageUpdate];}@end


ViewControll.m中

#import "ViewController.h"#import "LoopScrollView.h"@interface ViewController ()<UIScrollViewDelegate>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.//    [self createScrollView];    [self createLoopScrollView];}- (void) createLoopScrollView {    LoopScrollView *loopView = [[LoopScrollView alloc] initWithFrame:self.view.frame];    [self.view addSubview:loopView];    NSMutableArray *array = [[NSMutableArray alloc] init];    //添加content里的图片    for (int i = 1; i <= 5; i++) {        NSString *name = [NSString stringWithFormat:@"Welcome_3.0_%d.jpg",i];        [array addObject:name];    }    loopView.imageNames = [NSArray arrayWithArray:array];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)createScrollView{    CGRect rect = self.view.frame;    UIImage *bigImage = [UIImage imageNamed:@"1001.jpg"];    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, bigImage.size.width, bigImage.size.height)];    imageView.image = bigImage;//    imageView.backgroundColor = [UIColor yellowColor];    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:rect];    [scrollView setContentSize:imageView.frame.size];//设置contentView为那张大图片的尺寸    scrollView.showsVerticalScrollIndicator = NO;    scrollView.showsHorizontalScrollIndicator = NO;//    scrollView.bounces = NO;//是否有弹性    scrollView.delegate = self;    [scrollView addSubview:imageView];    [self.view addSubview:scrollView];    //缩放,两个值需要不同    scrollView.minimumZoomScale = 0.1;    scrollView.maximumZoomScale = 10;    } 

 ScroDelegate协议中的方法

//缩放scroll view里的视图- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    UIImageView *imageView = (UIImageView *)scrollView.subviews[0];    return  imageView;}//将要拖拽scroll view时- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    NSLog(@"scrollView willBeginDragging");}- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{//    NSLog(@"scrollView Will End Dragging");}- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{    NSLog(@"scrollView Did End Dragging");}//在滚动的时候调用- (void)scrollViewDidScroll:(UIScrollView *)scrollView{//    NSLog(@"scrollViewDidScroll");}//将要开始减速的时候调用- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{    NSLog(@"scrollView Will Begin Decerating");}- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    NSLog(@"scrollView Did End Decelerating");}


0 0