关闭

06_UIScrollView

标签: ScrollView
105人阅读 评论(0) 收藏 举报
#import "RootViewController.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface RootViewController ()<UIScrollViewDelegate>

@end


@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    // 创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor yellowColor];
    [scrollView release];

// 让scrollView进行滚动,这个属性很重要,只有设置了这个属性它才可以进行滚动
    // 水平滚动设置width,垂直滚动设置height
    scrollView.contentSize = CGSizeMake(8 * WIDTH, 0);
    // 按照页来进行滚动
    scrollView.pagingEnabled = YES;

//    NSLog(@"%@", scrollView.subviews);
    // 把图片放到scrollview上
    for (NSInteger i = 1; i < 8; i++) {
        // 拼接图片名
        NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
        // 把图片放到imageView上
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * (i - 1), 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:imageName];

        [scrollView addSubview:imageView];
        [imageView release];
    }
//    NSLog(@"%@", scrollView.subviews);
//    NSLog(@"%ld", scrollView.subviews.count);
    // 在第7张图片的后面,再加一个imageView,并且显示第一张图片内容,注意,相应的contentdize也要进行修改
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(7 * WIDTH, 0, WIDTH, HEIGHT)];
    lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
    [scrollView addSubview:lastImageView];
    [lastImageView release];


    // 关闭边界回弹效果
    scrollView.bounces = NO;

    // 把水平和垂直的滚动条关闭
    // 垂直
//    scrollView.showsVerticalScrollIndicator = NO;
    // 水平
    scrollView.showsHorizontalScrollIndicator = NO;

    scrollView.tag = 1000;
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];

    // 设置代理人
    scrollView.delegate = self;
}


#pragma mark 只要一拖拽,一滚动,就会触发这个协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%g", scrollView.contentOffset.x);
}
#pragma mark 当滑动结束,稳定之后才会被触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"我被触发了");
        if (scrollView.contentOffset.x == WIDTH * 7) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }
}


#pragma mark 通过NSTimer来让scrollView进行无限滚动
- (void)changeImage
{
    // 通过tag值找scrollview
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1000];
    // 通过偏移量,让哪一张视图显示在屏幕上,重要!!!
    [scrollView setContentOffset: CGPointMake(scrollView.contentOffset.x + WIDTH, 0)animated:YES];
//    scrollView.contentOffset =  CGPointMake(scrollView.contentOffset.x + WIDTH, 0);

    // 判断是否到当前的最后一张,如果到了需要把偏移量设置到第一张图片位置
    if (scrollView.contentOffset.x == WIDTH * 7) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }

}
...
@end
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4288次
    • 积分:393
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档