美文网首页
UI总结-UIScrollView

UI总结-UIScrollView

作者: Dear丶Musk | 来源:发表于2016-05-17 17:12 被阅读25次

                   UI总结-UIScrollView

    #import "ViewController.h"

    #define WIDTH self.view.frame.size.width

    #define HEIGHT self.view.frame.size.height

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor whiteColor];

    UIScrollView *scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    [self.view addSubview:scr];

    scr.backgroundColor = [UIColor cyanColor];

    [scr release];

    //设置按页滑动

    scr.pagingEnabled = YES;

    //设置边界回弹

    scr.bounces = NO;

    //设置隐藏水平滚动条

    scr.showsHorizontalScrollIndicator = NO;

    //设置scrollView的重要属性,完事就可以滚动了(它可以滚动的范围)

    scr.contentSize = CGSizeMake(WIDTH * 9, 0);

    //将图片铺在scrollView上,通过改变scrollView的偏移量来将图片循环播放起来

    //先铺中间的部分

    for (NSInteger i = 0; i < 7; i++) {

    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * (i + 1) , 0, WIDTH, HEIGHT)];

    imagev.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i ]];

    [scr addSubview:imagev];

    [imagev release];

    }

    //在scrollView的第一页上铺最后一张图片

    UIImageView *imagevi = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    imagevi.image = [UIImage imageNamed:@"h6.jpeg"];

    [scr addSubview:imagevi];

    [imagevi release];

    //在scrollView的最后一页上铺第一页图片

    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT)];

    imagev.image = [UIImage imageNamed:@"h0.jpeg"];

    [scr addSubview:imagev];

    [imagev release];

    //第二个重要的属性:contentOffset(偏移量)

    scr.contentOffset = CGPointMake(WIDTH, 0);

    scr.tag = 1000;

    scr.delegate = self;

    //scrollView的缩放

    //设置缩放比例,如果不设置,缩放无法操作

    scr.maximumZoomScale = 2;

    scr.minimumZoomScale = 0.5;

    //加个定时器,可以让他自己循环播放

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changePic) userInfo:nil repeats:YES];

    }

    #pragma mark - 用来控制缩放的协议方法

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

    return [scrollView.subviews firstObject];

    }

    #pragma mark - 只要scrollView一滑动,就会触发这个协议方法

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSLog(@"开始滑动");

    }

    #pragma mark - 滑动结束的时候触发这个方法.

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

    UIScrollView *scr = (UIScrollView *)[self.view viewWithTag:1000];

    if (scr.contentOffset.x == WIDTH * 8 ) {

    scr.contentOffset = CGPointMake(WIDTH, 0);

    }else if (scr.contentOffset.x == 0){

    scr.contentOffset = CGPointMake(WIDTH * 7, 0);

    }

    }

    - (void)changePic{

    UIScrollView *scr = (UIScrollView *)[self.view viewWithTag:1000];

    [scr setContentOffset:CGPointMake(scr.contentOffset.x + WIDTH, 0) animated:YES];

    if (scr.contentOffset.x == WIDTH * 7) {

    scr.contentOffset = CGPointMake(0, 0);

    }

    }

    运行结果,不加计时器:

    加了计时器以后,就可以自己动了,运行结果:

    相关文章

      网友评论

          本文标题:UI总结-UIScrollView

          本文链接:https://www.haomeiwen.com/subject/jppirttx.html