#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
相册,循环滚动,可缩放
最新推荐文章于 2017-04-18 21:03:18 发布