UI1_ScrollViewHomeWork
//
// 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的更多相关文章
随机推荐
- ntoskrnl.exe损坏或丢失的解决方式
同事的电脑启动时出现下面提示:"因下面文件损坏或丢失Windows无法启动 %systemroot%\system32\ntoskrnl.exe,请又一次安装以上文件的拷贝"(Wi ...
- 使用命令行工具将Android应用转换成BlackBerry PlayBook应用
昨天写了篇文章关于Android应用转换的,通过BlackBerry的在线转换工具将Android应用转换成BlackBerry PlayBook应用.有网友反映说方法有点麻烦,所以今天补上新的转换方 ...
- Java Web系统经常使用的第三方接口
1. Web Service 接口 1.1 接口方式说明和长处 在笔者的开发生涯中,当作为接口提供商给第三方提供接口时,以及作为client去调用第三方提供的接口时,大部分时候都是使用 Web Se ...
- CSDN蒋涛:我为什么和王峰一起创办极客帮天使基金?
i 黑马 记者:王静静 7月15日,i黑马在一家咖啡厅见到了CSDN创始人蒋涛,这位中国最大的程序猿社区的创始人,正在经营一份新事业,他和蓝港在线创始人王峰正式成立了天使基金"极客 ...
- box-flex等分总结
首先要知道在应用 box-flex 时必须给父容器定义 css 属性 display:box 其子容器才可以进行划分. .box{ display: -webkit-box; display: -mo ...
- MHA 安装过程 原创
root@monitor yum.repos.d]# cat CentOS-Base.repo [base]name=CentOS-$releasever - Basefailovermethod=p ...
- 在MVC中动态读取JSON数据创建表格
//使用getJSON // ("@Url.Action("GetAllUsers","User")" ,json文件的路径.也可以是 /M ...
- 对Kernel panic-not syncing:No init found...init=option to kernel错误总结!
转载:http://blog.csdn.net/wavemcu/article/details/6950053 在移植Linux中很容易出现这样那样的问题,我也遇到过,现在就共享下人家的一些经验和自己 ...
- WPF 之 TreeView右键选中节点及节点重命名
下面的TreeView节点是通过数据双向绑定的方式,绑定到TextBlock控件和TextBox控件的Text属性上,并且让两者绑定相同的属性,同时使TextBox控件刚好完全覆盖TextBlock控 ...
- [Java] log4j异常输出到日志
当程序运行异常时,e.printStackTrace()会打印出异常.但是很多时候我们希望将这些异常输出到日志中,以便日后可以随时查看到,可以通过这些异常快速的找到程序发生异常的代码. 那么有办法可以 ...