UI06_ScrollviewLoop

来源:互联网 发布:淘宝天猫品牌旗舰店 编辑:程序博客网 时间:2024/06/16 12:38

 ==////  MainViewController.m//  UI06_ScrollViewLoop////  Created by dllo on 15/8/5.//  Copyright (c) 2015年 Clare. All rights reserved.//#import "MainViewController.h"#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface MainViewController ()<UIScrollViewDelegate>@end@implementation MainViewController- (void)dealloc{    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    // 他继承UIView,可以通过创建view的四步对ScrollView进行创建    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    scrollView.backgroundColor = [UIColor lightGrayColor];    [self.view addSubview:scrollView];    [scrollView release];        // 重要的属性,这个属性可以让scrollview滚动起来    // contentSize设置scrollView的滚动范围    scrollView.contentSize = CGSizeMake(WIDTH * 10, HEIGHT);    // 按页来进行滚动    scrollView.pagingEnabled = YES;        UIImageView *firstImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"07.jpg"]];    firstImage.frame = CGRectMake( 0, 0, WIDTH, HEIGHT);    [scrollView addSubview:firstImage];        for (NSInteger i = 0; i < 8; i++) {        // 先拼接图片名        NSString *picName = [NSString stringWithFormat:@"%02ld.jpg", i];        // 通过图片名来创建UIImageView        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];        imageView.frame = CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT);        // imageView放到scrollview上        [scrollView addSubview:imageView];        // 释放imageView        [imageView release];    }        UIImageView *lastImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"00.jpg"]];    lastImage.frame = CGRectMake(9 * WIDTH, 0, WIDTH, HEIGHT);    [scrollView addSubview:lastImage];    // 偏移量    scrollView.contentOffset = CGPointMake(WIDTH, 0);        // 关掉边界回弹的效果,默认是YES    scrollView.bounces = NO;        // 水平和垂直的滚动条会作为两个子视图添加到scrollView的子视图里,如果把滚动条效果关闭,这两个视图就不会添加到scrollView的子视图里    NSLog(@"%@", scrollView.subviews);        // 关闭滚动条    scrollView.showsHorizontalScrollIndicator = NO;    scrollView.showsVerticalScrollIndicator = NO;        // 设置代理人    scrollView.delegate = self;    // 设置tag值    scrollView.tag = 1000;            // 创建一个计时器    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];}- (void)changeImage{    // 通过tag值找scrollview    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1000];//    scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x + WIDTH, 0);    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + WIDTH, 0) animated:YES];    // 对新的偏移量进行判断    if (scrollView.contentOffset.x == WIDTH * 9) {        scrollView.contentOffset = CGPointMake(WIDTH, 0);    }    }#pragma mark 只要滚动就好触发的协议方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    NSLog(@"开始滚动");}#pragma mark 当scrollView减速停止的时候会触发的协议方法- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    NSLog(@"停止滚动");    if (scrollView.contentOffset.x == WIDTH * 9 ) {        scrollView.contentOffset = CGPointMake(WIDTH * 1, 0);    } else if (scrollView.contentOffset.x == 0){        scrollView.contentOffset = CGPointMake(WIDTH * 8, 0);    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/* #pragma mark - Navigation  // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */@end


0 0