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的更多相关文章
随机推荐
- 使用generator自动生成Mybatis映射配置文件
在使用mybatis时,映射文件的配置非常麻烦,对于做逻辑不是很复杂,功能不是特别关键的模块的时候,我们没有必要手动书写,可以使用generator工具生成. generator工具实际上就是根据数据 ...
- OS_TASK.C
/*************************************************************************************************** ...
- Swift基础使用方法(Swift开发之中的一个)
昨晚苹果公布了新一代编程语言Swift,官方提供了一个iBook的说明文档.有须要的能够看下.地址:mt=11" target="_blank">https://i ...
- vm.dirty_background_ratio and vm.dirty_ratio
http://hellojava.info/?p=264&utm_source=tuicool&utm_medium=referral 解决磁盘io紧张的一种临时方法 有些时候可能会碰 ...
- DOS攻击之详解--转载
源地址没有找到,间接引用地址:http://wushank.blog.51cto.com/3489095/1156004 DoS到底是什么?接触PC机较早的同志会直接想到微软磁盘操作系统的DOS--D ...
- 两种方式连接mysql
一种方式:运行命令符后,mysql -u root -p(如果不成功,说明环境变量没配,命令行到 mysql的bin目录下,然后运行mysql -u root -p 应该成功了) 另外一种方式,直接有 ...
- 通过布赛尔曲线以及CAShapeLayer的strokeStart 、strokeEnd 属性来实现一个圆形进度条
#import <UIKit/UIKit.h> @interface CircleProgressView : UIView /**起始值(0-1)*/ @property(nonatom ...
- Java基础知识强化之IO流笔记74:NIO之 Buffer
Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...
- 【开源项目7】Android视图注入库:butterknife
介绍 ButterKnife通过@InjectView和视图的ID注解的变量去找到并自动转换为你布局上相应的布局视图. class ExampleActivity extends Activity { ...
- oracle PL/SQL(procedure language/SQL)程序设计之异常(exception)
什么是异常?在PL/SQL中的一个标识.在程序运行期间被触发的错误.异常是怎样被触发的?产生一个Oracle错误.用户显示触发.怎样处理异常?用异常处理句柄捕获异常.传播异常到调用环境. 捕获异常 E ...