//
// 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

UI1_ScrollViewHomeWork的更多相关文章

随机推荐

  1. (转)用AGG实现高质量图形输出(三)

    转自 :http://www.cnblogs.com/CoolJie/archive/2011/04/27/2030260.html 线段生成器(Span Generator) 我们前面举的例子使用的 ...

  2. 一个简单的弹出层ProgressBar

    https://github.com/eltld/SimpleLoading

  3. JavaScript宝座:七大框架论剑

    JavaScript宝座:七大框架论剑 一周前,Throne of JS大会在多伦多召开,这应该是我参加过的最有料也最不一样的一次大会.大会官网如是说: 加载整个页面,然后再“渐进增强”以添加动态行为 ...

  4. [AngularJS] Accessing Data in HTML -- controllerAs, using promises

    <!DOCTYPE html> <html> <head> <title>Access Data From HTML</title> < ...

  5. Android Studio中导入第三方库

    之前开发Android都是使用的eclipse,近期因为和外国朋友Timothy一起开发一款应用,他是从WP平台刚切换使用Android的,使用的开发环境时Android Studio,为了便于项目的 ...

  6. MVC4数据注释与验证 2

    Using Validation Annotations Required必须项验证属性 [Required] public string FirstName { get; set; } [Requi ...

  7. 剑指 offer set 6 打印从 1 到 N 的所有数

    总结 1. 求全排列的变形题, 有些隐晦, 没看出来

  8. ios上比较好用的Cydia插件

    1.iFile查看系统文件 2.KuaiDial归属地数据库 3.KuaiDial电话拨号助手 4.搜狗输入法 Photo Editor 房贷计算器

  9. Golang学习 - io/ioutil 包

    ------------------------------------------------------------ // Discard 是一个 io.Writer 接口,调用它的 Write ...

  10. PHP|开发必知的良好实践

    过滤.验证.转义 所有这些外部资源都不能完全相信 $_GET $_POST $_REQUEST $_COOKIE $argv php://stdin php://input file_get_cont ...