UI1_ScrollViewHomeWork
2015-07-13 17:57
211 查看
// // 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
相关文章推荐
- 探秘Java中的String、StringBuilder以及StringBuffer
- UIButton上字体的对齐方式
- UIProgressViewController学习记录
- IOS--UI--LessonUITableView
- 确保客户端可以接收到服务端的异常serviceDebug includeExceptionDetailInFaults="true"
- Leetcode Implement Stack using Queues
- UEditor 第一次加载正常,第二次无法正常加载问题
- EasyUi Datagrid的基础使用
- Android开发UI之Navigation Drawer
- easyui常用控件及样式收藏
- C# GUID
- iOS实现UIImageView透明区域点击事件穿透
- UItableView性能优化
- 给iOS工程增加Daily Build
- easyUI 获取当前页数
- duilib进阶教程 -- 实现List排序
- 如何判断EasyUi中treegride的某一节点是不是叶子节点
- 深入理解Windows Phone 8.1 UI控件编程
- 将UIButton的SEL转成block
- ImportError: matplotlib requires dateutil