相册,循环滚动,可缩放

#import "ViewController.h"
#define HEIGHT 400
#define WIDTH self.view.frame.size.width
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIPageControl *page;
@property(nonatomic, assign)NSInteger picIndex;
@end

@implementation ViewController
- (void)dealloc
{
    [_page release];
    [_scrollView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}
- (void)createView
{
    // 创建UIScrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 9, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(WIDTH , 0);
    [self.view addSubview:self.scrollView];
    [_scrollView release];

    // 创建子视图,前后加一个imageview,可以无限滚动
    UIScrollView *smallFirstScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    smallFirstScroll.delegate = self;
    smallFirstScroll.maximumZoomScale = 2;
    smallFirstScroll.minimumZoomScale = 0.5;
    [self.scrollView addSubview:smallFirstScroll];
    [smallFirstScroll release];

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

    for (NSInteger i = 1; i < 8; i++) {
        UIScrollView *smallScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        smallScroll.delegate = self;
        smallScroll.maximumZoomScale = 2;
        smallScroll.minimumZoomScale = 0.5;
        [self.scrollView addSubview:smallScroll];
        [smallScroll release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i]];
        [smallScroll addSubview:imageView];
        [imageView release];
    }
    UIScrollView *smallLastScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT)];
    smallLastScroll.delegate = self;
    smallLastScroll.maximumZoomScale = 2;
    smallLastScroll.minimumZoomScale = 0.5;
    [self.scrollView addSubview:smallLastScroll];
    [smallLastScroll release];

    UIImageView *lastView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    lastView.image = [UIImage imageNamed:@"h1.jpeg"];
    [smallLastScroll addSubview:lastView];
    [lastView release];
    // 显示当前是第几页
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 450, 200, 50)];
    [self.view addSubview:self.page];
    [_page release];
    self.page.backgroundColor = [UIColor cyanColor];
    self.page.numberOfPages = 7;
    self.page.layer.borderWidth = 1;
    self.page.layer.cornerRadius = 10;
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    self.page.currentPageIndicatorTintColor = [UIColor blackColor];
    self.page.pageIndicatorTintColor = [UIColor redColor];

}
- (void)pageAction:(UIPageControl *)page
{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1)* WIDTH, 0) animated:YES];
}
// 缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    UIImageView *imageView = [scrollView.subviews objectAtIndex:0];
    NSLog(@"%@", scrollView.subviews);
    return imageView;
}
// 滚动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
    } else if (self.scrollView.contentOffset.x == 8 * WIDTH) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.page.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;
    // 滚动完把视图缩放成原有大小
    for (UIScrollView *sView in scrollView.subviews) {

        if ([sView isKindOfClass:[UIScrollView class]]) {
            sView.zoomScale = 1.0;

        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值