关闭

06_无限滚动的相册

标签: scrollView
90人阅读 评论(0) 收藏 举报
#import "RootViewController.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH  self.view.frame.size.width

@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIPageControl *page;
@end

@implementation RootViewController

- (void)dealloc
{
    [_scrollView release];
    [_page release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.scrollView];
    [self.scrollView release];
    // 移动的范围
    self.scrollView.contentSize = CGSizeMake(9 * WIDTH, 0);
    // 按页来滚动
    self.scrollView.pagingEnabled = YES;

    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImageView.image = [UIImage imageNamed:@"h7.jpeg"];
    [self.scrollView addSubview:firstImageView];
    [firstImageView release];
    firstImageView.userInteractionEnabled = YES;


    for (NSInteger i = 1; i < 8; i++) {
        NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i , 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:imageName];

        [self.scrollView addSubview:imageView];
        [imageView release];
        imageView.userInteractionEnabled = YES;
    }

    // 在最后面加上一个视图,放在最后,显示第一张图片
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8 * WIDTH, 0, WIDTH, HEIGHT)];
    lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
    [self.scrollView addSubview:lastImageView];
    [lastImageView release];
    lastImageView.userInteractionEnabled = YES;

    // 为了能显示第一张图片,我们需要先设置一个偏移量,这样能直接显示罗宾的第一张图片
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);


//    // 关闭边界回弹效果
    self.scrollView.bounces = NO;
//
//    // 关闭水平的滚动条
//    self.scrollView.showsHorizontalScrollIndicator = NO;

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

    // 创建一个pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 200, 40)];
    self.page.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.page];
    [_page release];
    self.page.numberOfPages = 7;
    // 设置未被点的颜色
    self.page.pageIndicatorTintColor = [UIColor yellowColor];
    // 当前点的颜色
    self.page.currentPageIndicatorTintColor = [UIColor redColor];


    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    // 想要scrollView实现缩放功能,需要先设置最大最小的缩放比例
    self.scrollView.minimumZoomScale = 0.5;
    self.scrollView.maximumZoomScale = 2;

}
#pragma mark 这个方法用来管理scrollview的缩放功能
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{

    return [scrollView.subviews firstObject];
}

- (void)pageAction:(UIPageControl *)page
{
    // 还是设置偏移量
    NSLog(@"%ld", page.currentPage);
//    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * WIDTH, 0) animated:YES];


    [self.scrollView setContentOffset:CGPointMake(page.currentPage * WIDTH, 0)];
}

// 滑动结束之后,会触发的协议方法


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == WIDTH * 8) {
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }else if (scrollView.contentOffset.x == 0 ) {
        scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
    }

    // pageControl和scrollView进行关联,点的移动方向和scrollView的偏移量有关
    // 无论怎么方向滑动,当pageControl和scrollview关联的时候,偏移量都是修改好的,所以可以正确的滚动和显示
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;

}
...

@end
0
0

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