// // AppDelegate.m // UI1_ScrollViewHomeWork // // Created by zhangxueming on 15/7/13. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. ViewController *root = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root]; self.window.rootViewController = nav; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } // // ViewController.h // UI1_ScrollViewHomeWork // // Created by zhangxueming on 15/7/13. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIScrollViewDelegate> @end
// // ViewController.m // UI1_ScrollViewHomeWork // // Created by zhangxueming on 15/7/13. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ViewController.h" @interface ViewController () { UIScrollView *_indexScrollView; UIScrollView *_scrollView; NSInteger _currentIndex; //记录当前显示的图片 NSMutableArray *_imageArray; //存储图片 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _currentIndex = 0; _imageArray = [NSMutableArray array]; for (int i=0; i<10; i++) { NSString *pictureName = [NSString stringWithFormat:@"superCar%i", i]; NSString *path = [[NSBundle mainBundle] pathForResource:pictureName ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; [_imageArray addObject:image]; } _indexScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10,64, self.view.frame.size.width-20, 80)]; _indexScrollView.bounces = NO; self.automaticallyAdjustsScrollViewInsets = NO; CGFloat indexImageViewWidth = (self.view.frame.size.width-20)/4; CGFloat indexImageViewHeight= 80; for (int i=0; i<10; i++) { UIImageView *imageView = [[UIImageView alloc] initWithImage:_imageArray[i]]; imageView.frame = CGRectMake(i*indexImageViewWidth, 0, indexImageViewWidth, indexImageViewHeight); UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIndexImageView:)]; imageView.userInteractionEnabled = YES; imageView.tag = 100+i; [imageView addGestureRecognizer:tap]; [_indexScrollView addSubview:imageView]; } _indexScrollView.showsHorizontalScrollIndicator = NO; _indexScrollView.showsVerticalScrollIndicator = NO; _indexScrollView.contentSize = CGSizeMake(10*indexImageViewWidth, indexImageViewHeight); [self.view addSubview:_indexScrollView]; CGFloat scrollViewWidth = self.view.frame.size.width-20; CGFloat scrollViewHeight = 400; _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 80+64, scrollViewWidth, scrollViewHeight)]; for (int i=0; i<10; i++) { UIImageView *imageView = [[UIImageView alloc] initWithImage:_imageArray[i]]; imageView.frame = CGRectMake(scrollViewWidth*i, 0, scrollViewWidth, scrollViewHeight); [_scrollView addSubview:imageView]; } _scrollView.contentSize = CGSizeMake(scrollViewWidth*10, scrollViewHeight); _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.bounces = NO; _scrollView.pagingEnabled = YES; //设置代理 _scrollView.delegate = self; [self.view addSubview:_scrollView]; } - (void)tapIndexImageView:(UITapGestureRecognizer *)tap { UIImageView *imageView = (UIImageView *)tap.view; _currentIndex = imageView.tag-100; [_scrollView setContentOffset:CGPointMake((self.view.frame.size.width-20)*_currentIndex, 0) animated:YES]; } #pragma mark ---ScrollViewDelegate--- //分页使能的话, 该方法一定被调用 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"---------"); _currentIndex = scrollView.contentOffset.x/(self.view.frame.size.width-20); NSLog(@"currentIndex = %li", _currentIndex); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
时间: 2024-11-06 03:55:22