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的更多相关文章
随机推荐
- Linux中搭建SVNserver
一 下载工具 1. subversion-1.6.17.tar.gz 2. subversion-deps-1.6.17.tar.gz 二 解压两个包: 1.在此之前,我已经创建了一个用户svnroo ...
- ScrollView反弹效果 仿小米私密短信效果
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/28441197 如今非常多APP都给ScrollView加入了反弹效果.QQ.小米 ...
- js获取非行间样式/定义样式
<!--DOCTYPE html--> <html> <head> <meta charset="utf-8" /> <sty ...
- Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8
在使用Eclipse 直接编译NDK,有时候会报类似以下错误 Android NDK: WARNING: APP_PLATFORM android-14 is larger than android: ...
- Xcode快照——管理应用程序版本
转自:http://blog.csdn.net/yuanbohx/article/details/8919474 1.创建快照:FIle → Create Snapshot 2.查看快照:Window ...
- C++ Qt 访问权限总结
总结:C++的访问修饰符的作用是以类为单位,而不是以对象为单位. 通俗的讲,同类的对象间可以“互相访问”对方的数据成员,只不过访问途径不是直接访问. 步骤是:通过一个对象调用其public成员函数,此 ...
- fcitx的安装_配置
sudo apt-get purger ibus 安装Fcitx:sudo apt-get install im-switch fcitx #修改当前用户的默认输入法, 具体看man im-switc ...
- CentOS安装VMware Tools
新建cdrom挂载目录 mkdir /mnt/cdrom 挂载光驱 mount -t auto /dev/cdrom /mnt/cdrom这命令就是把CentOS CDROM挂载在/mnt/cdrom ...
- 转:关于视频H264编解码的应用实现
转:http://blog.csdn.net/scalerzhangjie/article/details/8273410 项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时 ...
- 设置navigationBar上面的item
//创建UIBarButtonItem UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemI ...