iPhone之手势切换图片

今天我们介绍一下iPhone手势切换图片,算是对之前的小小总结。程序功能和常用的图片浏览软件类似,手指向左划动就回卷到上一幅图片,向右划动就后卷到下一幅图片。别看功能简单,但真正实现起来还是挺麻烦的。下面开始吧:

1.

新建一个Window-based Application,名称为Switch:

2.

分别新建三个UIViewController:FirstViewController,SecondViewController 和 RootViewController,其中 FirstViewController,SecondViewController 需要勾选第三项“With XIB for user interface”,而 RootViewController 则不用勾选(这样做的原因是我想介绍一下手动创建一个XIB的过程)

3.

下面介绍一下手动创建 RootViewController.XIB 并与RootViewController 关联的过程:

新建一个 Empty XIB,名称是 RootViewController.XIB:

4.

打开 RootViewController.XIB,我们发现少了 View 一项

从左侧的Library库 中拖一个 View 到其中:

选中 File's Owner,按 Command + 4 打开属性窗口,在 Class 一栏选择 RootViewController,这样新创建的 RootViewController.XIB 就和 RootViewController 联系起来了

然后,选中 File's Owner,按 Ctrl 键,鼠标拖动到 View 中,在弹出的 Outlets 中选择 View,这样新创建的 View 就和 RootViewController 的 View 联系起来了

5.

修改 SwitchAppDelegate.m 如下:

//
//  SwitchAppDelegate.m
//  Switch
//
//  Created by HuTao on 8/18/12.
//  Copyright __MyCompanyName__ 2012. All rights reserved.
//#import "SwitchAppDelegate.h"
#import "RootViewController.h"@implementation SwitchAppDelegate@synthesize window;#pragma mark -
#pragma mark Application lifecycle- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    // Override point for customization after application launch.RootViewController  * root = [[RootViewController alloc]init];window.rootViewController = root;[window makeKeyAndVisible];return YES;
}- (void)dealloc
{[window release];[super dealloc];
}@end

运行一下,发现已经成功了,可以在 RootViewController.XIB 放置些控件以示区别

6.

将要显示的图片加入工程中,并且在 FirstViewController.XIB 和 SecondViewController.XIB 中分别铺满 UIImageView 控件,如下图:

7.

修改 FirstViewController.h 如下:

//
//  FirstViewController.h
//  Switch
//
//  Created by HuTao on 8/18/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//#import <UIKit/UIKit.h>@interface FirstViewController : UIViewController
{IBOutlet UIImageView * imageView;
}@property (retain, nonatomic) UIImageView * imageView;-(void)loadImage:(NSString *)imageName;@end

loadImage是提供给外部调用的一个方法,传入要显示的文件名,就会在UIImageView中显示出来



并把 UIImageView 和对应的 IBOutlet 连接起来



然后修改 FirstViewController.m 如下:

//
//  FirstViewController.m
//  Switch
//
//  Created by HuTao on 8/18/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//#import "FirstViewController.h"@implementation FirstViewController@synthesize imageView;-(void)loadImage:(NSString *)imageName
{[imageView setImage:[UIImage imageNamed:imageName]];
}// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{[super viewDidLoad];
}- (void)viewDidUnload
{[super viewDidUnload];imageView = nil;
}- (void)dealloc
{[super dealloc];[imageView release];
}@end

[UIImageView setImage:UIImage] 显示图像

[UIImage imageNamed:NSString *] 将文件名转换成 UIImage

8.

SecondViewController 完全参照 FirstViewController 的修改

9.

修改 RootViewController.h 如下:

//
//  RootViewController.h
//  Switch
//
//  Created by HuTao on 8/18/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//#import <UIKit/UIKit.h>@class FirstViewController;
@class SecondViewController;@interface RootViewController : UIViewController
{FirstViewController * firstViewController;SecondViewController * secondViewController;//图片名称列表NSArray * imageNameList;//当前图片编号int imageIndex;//当前显示的是否是FirstViewControllerBOOL firstUpside;
}@property (retain, nonatomic) FirstViewController * firstViewController;
@property (retain, nonatomic) SecondViewController * secondViewController;
@property (retain, nonatomic) NSArray * imageNameList;-(void)handleSwipeGesture:(UIGestureRecognizer*)sender;
-(void)curlImage:(UISwipeGestureRecognizerDirection)direction;@end

修改 RootViewController.m 如下:

//
//  RootViewController.m
//  Switch
//
//  Created by HuTao on 8/18/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"@implementation RootViewController@synthesize firstViewController;
@synthesize secondViewController;
@synthesize imageNameList;//划动手势后调用的回调函数
-(void)handleSwipeGesture:(UIGestureRecognizer *)sender
{  //划动的方向UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];  //上卷或下卷图片[self curlImage:direction];
}  -(void)curlImage:(UISwipeGestureRecognizerDirection)direction
{UIViewAnimationTransition transition; //判断是上下左右  switch (direction){  case UISwipeGestureRecognizerDirectionUp:  NSLog(@"up");return;case UISwipeGestureRecognizerDirectionDown:  NSLog(@"down");  return;case UISwipeGestureRecognizerDirectionLeft://往左划动,表示向前翻页NSLog(@"left");  imageIndex = ( ([imageNameList count] - 1 + imageIndex) % [imageNameList count]);transition = UIViewAnimationTransitionCurlDown;break;  case UISwipeGestureRecognizerDirectionRight://往右划动,表示向后翻页NSLog(@"right");  imageIndex = ( (++imageIndex) % [imageNameList count] );transition = UIViewAnimationTransitionCurlUp;break;  default:  return;  }//loadImage必须放在insertSubview之后,否则无效?为什么?UIViewController * coming = nil; UIViewController * going = nil;    if(firstUpside == YES)     {//由 FirstViewController 切换到 SecondViewControllercoming = secondViewController;   going = firstViewController;   }else   {   //由 SecondViewController 切换到 FirstViewControllercoming = firstViewController;  going = secondViewController;}[UIView beginAnimations:@"View Flip" context:nil];    [UIView setAnimationDuration:1.25]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:transition forView:self.view cache:YES];   [coming viewWillAppear:YES];    [going viewWillDisappear:YES];  [going.view removeFromSuperview];   [self.view insertSubview:coming.view atIndex:0];    if(firstUpside == YES)     {[secondViewController loadImage:[imageNameList objectAtIndex:imageIndex]];firstUpside = NO;}else {   [firstViewController loadImage:[imageNameList objectAtIndex:imageIndex]];firstUpside = YES;}[coming viewDidAppear:YES];    [going viewDidDisappear:YES];   //提交Animation  [UIView commitAnimations];
}// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{[super viewDidLoad];imageNameList = [[NSArray alloc] initWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg",@"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", nil];imageIndex = 0;firstUpside = YES;FirstViewController * temp1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];    self.firstViewController = temp1;//首先显示 FirstViewController[self.view insertSubview:temp1.view atIndex:0];[temp1 release];[firstViewController loadImage:[imageNameList objectAtIndex:imageIndex]];SecondViewController * temp2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];                   self.secondViewController = temp2; [temp2 release];    //划动手势  //上划  UISwipeGestureRecognizer * swipeGestureUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];  swipeGestureUp.direction = UISwipeGestureRecognizerDirectionUp;[self.view addGestureRecognizer:swipeGestureUp];  [swipeGestureUp release];  //下划  UISwipeGestureRecognizer * swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];  swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown;[self.view addGestureRecognizer:swipeGestureDown];  [swipeGestureDown release];  //左划  UISwipeGestureRecognizer * swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];  swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;[self.view addGestureRecognizer:swipeGestureLeft];  [swipeGestureLeft release];  //右划  UISwipeGestureRecognizer * swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];  swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight;[self.view addGestureRecognizer:swipeGestureRight];  [swipeGestureRight release];
}- (void)viewDidUnload
{[super viewDidUnload];firstViewController = nil;secondViewController = nil;
}- (void)dealloc {[super dealloc];[firstViewController release];[secondViewController release];
}@end

虽然代码较多,但很好理解。

首先初始化了 FirstViewController 和 SecondViewController,然后加入了GestureRecognizer,一旦识别到划动手势,则调用 handleSwipeGesture回调函数。



在 curlImage 中,我采用的切换图片的原理是:定义两个 UIViewController,交替显示,以此达到翻页切换图片的目的。当然,还需要考虑只有0或1张图的情况,不过,这里我省略了。

另外经过我的测试,发现 loadImage 必须放在 insertSubview 之后,否则无效?为什么?

运行结果如下:

原图:

手指向右划动,切换到下一幅图片:

下一幅图片:

最后我把完整代码也上传上来了:

http://download.csdn.net/detail/htttw/4510398

完成!

iPhone之手势切换图片相关推荐

  1. LVGL笔记(6)-电子相册使用手势切换图片(windows仿真)

    文章目录 1.LV_EVENT_GESTURE事件的回调函数 2.较为完整的代码 3.工程源码 今天看了一下lvgl的EVENT枚举,有一个事件 LV_EVENT_GESTURE 是响应手势滑屏的,就 ...

  2. 【转】Android android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)...

    首先我们还是看一些示例:(网易,新浪,百度)      下面我简单的介绍下实现方法:其实就是listview addHeaderView.只不过这个view是一个可以切换图片的view,至于这个vie ...

  3. 在c 语言里如何制作滑动图片大小,[教程]教你在微信小程序中实现手势缩放图片...

    在小程序中,image 组件的 mode 有 12 种,其中只有三种是缩放模式.而在这三种之中,只有 aspectFit 模式可以等比例缩放图片,并显示完整的图片.此模式是保证图片长边完整地显示出来, ...

  4. iPhone模拟器之将图片添加到相册实例

    iPhone模拟器实例 把图片添加到相册是本文要介绍的内容,我们通常可以使用iPhone模拟器完成大多数iPhone开发.模拟器允许在计算机上而不是一个真正的iPhone上构建.运行和测试iPhone ...

  5. java 滑块控制图片渐变切换_Android实现滑动屏幕切换图片

    本文实例为大家分享了Android实现滑动屏幕切换图片的具体代码,供大家参考,具体内容如下 activity_main.xml 文件代码: xmlns:app="http://schemas ...

  6. Android (滑动屏幕切换图片的实现)

    一.首先实现界面部分 代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...

  7. iOS开发——手势识别器(用手势实现图片旋转和缩小放大)

    iOS开发中,除了有关触摸的这组方法来控制用户的手指触控外,还可以用UIGestureRecognize的衍生类来进行判断,方便了开发. UIGestureRecognize的子类类别有以下几种: U ...

  8. javascript设计模式实践之模板方法--具有百叶窗切换图片效果的JQuery插件(二)...

    在上一篇<javascript设计模式实践之迭代器--具有百叶窗切换图片效果的JQuery插件(一)>里,通过采用迭代器模式完成了各初始化函数的定义和调用. 接下来就要完成各个切换效果的编 ...

  9. html简单的图片切换js,一分钟让你学会如何使用js切换图片

    利用js实现简单的动画效果 js简介 JavaScript 是世界上最流行的编程语言. 这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. Java ...

  10. JavaScript基础15-day17【BOM(Navigator、History、Location)、定时器、切换图片练习、轮播图】

    学习地址: 谷粒学院--尚硅谷 哔哩哔哩网站--尚硅谷最新版JavaScript基础全套教程完整版(140集实战教学,JS从入门到精通) JavaScript基础.高级学习笔记汇总表[尚硅谷最新版Ja ...

最新文章

  1. 2018年中国人工智能行业研究报告|附下载
  2. 设计模式--外观模式
  3. 字节跳动技术团队提出「AI渲染」方案,手机端也能实现影视级渲染效果
  4. linux grep和正则表达式
  5. html 地球大气,地球大气层为什么永远不会消失?
  6. JAVA:网络编程总结
  7. php键名相加,php二维数组相同键名相加实例
  8. Qt on Android 蓝牙通信开发
  9. java sax解析器_Java SAX解析器示例
  10. 【光学】基于matlab色散曲线拟合【含Matlab源码 2053期】
  11. winpe装双系统linux_Winpe下安装Ubuntu
  12. bigemap如何下生成CGCS2000坐标系等高线
  13. win10系统如何查询本机的IP地址和外网IP地址
  14. 解决Outlook搜索功能的搜索结果不完整问题
  15. 日志-坑-keng-rz-rizhi-log
  16. 牛客网刷题:Q3禁忌雷炎(一般)
  17. (java)跳台阶:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
  18. 从前后端分离到前后端整合的“退步”(一)项目结构
  19. HC-SR501 人体红外感应模块
  20. DDD(Domain-Driven Design)领域驱动架构介绍

热门文章

  1. 题解 【中学高级本-网络流24题】餐巾计划
  2. Cisco Packet Tracer路由器ip简单配置(网关)
  3. lattice planner 规划详解
  4. kuangbin专题十二 HDU1069 Monkey and Banana
  5. java 显示百分比_Java 数字转百分比%
  6. siebel基础配置
  7. flask中的应用工厂
  8. Ubuntu中挂载使用nas服务器
  9. 计算机功能自定义,设计大师学教学:自定义鼠标右键功能提升CAD绘图效率-鼠标右键菜单设置...
  10. TEM波以及TEM TE TM模的区别