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的更多相关文章
随机推荐
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记
第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...
- STC51系列单片机免掉电下载(热启动下载)
相信喜欢单片机的朋友都用过STC的单片机,用过STC单片机的朋友都有这种感受:实惠.易用.功能强大!就是每次下载都要冷启动特别恶心,相信很多朋友的开发板上的电源键都按烂了. 其实STC单片机可以不用免 ...
- 【手把手教你Elmah】如何在MVC.NET项目中在线查看【错误日志】
一. 在NuGet下载Elmah.MVC dll文件! 或者点击下载dll文件,并且引用客户端. 二.配置WebConfig <sectionGroup name="elmah& ...
- MyBatis优化技巧
☬配置日志文件 封装工具类 代码贴一下: package com.shxt.utils; import java.io.InputStream; import org.apache.ibatis.io ...
- 在xml文件中写入&符号时需要对其进行转义
如:在xml文件中保存这样的测试文件: <?xml version="1.0" encoding="utf-8"?> <datas> & ...
- IOS 给图片添加水印(文字)
有时候上传图片要加唯一标识,简单的就是添加一个水印.这里水印我们讲文字,可以是当前系统时间.坐标.地理位置等 原理就是把一个字符串写到图片上,并且字(font)的大小由图片大小控制. 以下是封装好的一 ...
- 【Android 界面效果23】LayoutInflater作用及使用
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方 ...
- android中的一些问题
1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚 ...
- [转]15 个顶级 HTML5 游戏引擎
本文转自:http://www.open-open.com/news/view/13874db 1) HTML5 Game Engine Construct 2 is a leading high q ...