[转载]手势做的简单的翻页效果 翻书带背影效果
2011-08-01 13:54
232 查看
原文地址:手势做的简单的翻页效果作者:醉吻花颜
翻页效果,类似下面的样子:
在电子书应用中会很常见。这里需要两个要点:
翻页动画
手势上下轻扫(swipe)的处理
先说一下轻扫(swipe)的实现,可以参考编写简单的手势示例:Tap了解手势种类。
在viewDidLoad方法中注册了对上、下、左、右四个方向轻松的处理方法:
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
[superviewDidLoad];
可以看到,都是同一个方法,handleSwipeFrom。
在该方法中,再识别具体是哪个方向的轻扫手势,比如判断是向下的轻扫:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
NSLog(@"Swipe received.");
if(recognizer.direction==UISwipeGestureRecognizerDirectionDown){
NSLog(@"swipe down");
判断是向上的轻扫:
if (recognizer.direction==UISwipeGestureRecognizerDirectionUp){
NSLog(@"swipe up");
有关动画的处理,比如向下(往回)翻页,类似这样:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDownforView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
向上(向前)翻页,只需改为:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
如果是电子书,还需要考虑一个问题,就是有多个页面(图形),比如50页。那么需要有一个数据结构来保存这些页面的图片路径:
objc数据结构,比如数组
sqlite数据库表
这样,写一套翻页代码和加载什么图形之间就可以解耦。
本文示例使用的是数组,类似这样:
pages=[[NSArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",
nil];
图片保存在resources下。
为了能让上页下页翻页的时候找到关联的页面,采用了如下机制:
将图片封装为UIImageView显示
可以为UIImageView设置一个tag值,值为数组下标+1
这样,上级view有方法能根据tag查询到UIImageView,比如:UIView*currentView=[self.view viewWithTag:currentTag];
设置一个成员变量currentTag保存当前的tag值
比如这样,当应用加载的时候显示第一页:
currentTag=1;
NSString*path = [[NSBundle mainBundle] pathForResource:@"pageflip1"ofType:@"mp3"];
player=[[***AudioPlayer alloc] initWithContentsOfURL:[NSURLfileURLWithPath:path] error:NULL];
//[[UIApplication sharedApplication] setStatusBarHidden:YESanimated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:YESwithAnimation: UIStatusBarAnimationSlide];
UIImageView*contentView = [[UIImageView alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];
[contentViewsetImage:[UIImage imageNamed:[pagesobjectAtIndex:(currentTag-1)]]];
[contentViewsetUserInteractionEnabled:YES];
contentView.tag=currentTag;
在翻页时的处理:
if (currentTag<[pages count]) {
UIView*currentView=[self.view viewWithTag:currentTag];
currentTag++;
UIImageView*contentView = [[UIImageView alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];
[contentViewsetImage:[UIImage imageNamed:[pagesobjectAtIndex:(currentTag-1)]]];
[contentViewsetUserInteractionEnabled:YES];
contentView.tag=currentTag;
[UIViewbeginAnimations:@"animationID" context:nil];
[UIViewsetAnimationDuration:0.7f];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIViewsetAnimationRepeatAutoreverses:NO];
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.view cache:YES];
[currentViewremoveFromSuperview];
[self.viewaddSubview:contentView];
[UIViewcommitAnimations];
原文来自:http://marshal.easymorse.com/archives/3760
翻页效果,类似下面的样子:
在电子书应用中会很常见。这里需要两个要点:
翻页动画
手势上下轻扫(swipe)的处理
先说一下轻扫(swipe)的实现,可以参考编写简单的手势示例:Tap了解手势种类。
在viewDidLoad方法中注册了对上、下、左、右四个方向轻松的处理方法:
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view]addGestureRecognizer:recognizer];
[recognizerrelease];
[superviewDidLoad];
可以看到,都是同一个方法,handleSwipeFrom。
在该方法中,再识别具体是哪个方向的轻扫手势,比如判断是向下的轻扫:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
NSLog(@"Swipe received.");
if(recognizer.direction==UISwipeGestureRecognizerDirectionDown){
NSLog(@"swipe down");
判断是向上的轻扫:
if (recognizer.direction==UISwipeGestureRecognizerDirectionUp){
NSLog(@"swipe up");
有关动画的处理,比如向下(往回)翻页,类似这样:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDownforView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
向上(向前)翻页,只需改为:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
如果是电子书,还需要考虑一个问题,就是有多个页面(图形),比如50页。那么需要有一个数据结构来保存这些页面的图片路径:
objc数据结构,比如数组
sqlite数据库表
这样,写一套翻页代码和加载什么图形之间就可以解耦。
本文示例使用的是数组,类似这样:
pages=[[NSArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",
nil];
图片保存在resources下。
为了能让上页下页翻页的时候找到关联的页面,采用了如下机制:
将图片封装为UIImageView显示
可以为UIImageView设置一个tag值,值为数组下标+1
这样,上级view有方法能根据tag查询到UIImageView,比如:UIView*currentView=[self.view viewWithTag:currentTag];
设置一个成员变量currentTag保存当前的tag值
比如这样,当应用加载的时候显示第一页:
currentTag=1;
NSString*path = [[NSBundle mainBundle] pathForResource:@"pageflip1"ofType:@"mp3"];
player=[[***AudioPlayer alloc] initWithContentsOfURL:[NSURLfileURLWithPath:path] error:NULL];
//[[UIApplication sharedApplication] setStatusBarHidden:YESanimated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:YESwithAnimation: UIStatusBarAnimationSlide];
UIImageView*contentView = [[UIImageView alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];
[contentViewsetImage:[UIImage imageNamed:[pagesobjectAtIndex:(currentTag-1)]]];
[contentViewsetUserInteractionEnabled:YES];
contentView.tag=currentTag;
在翻页时的处理:
if (currentTag<[pages count]) {
UIView*currentView=[self.view viewWithTag:currentTag];
currentTag++;
UIImageView*contentView = [[UIImageView alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];
[contentViewsetImage:[UIImage imageNamed:[pagesobjectAtIndex:(currentTag-1)]]];
[contentViewsetUserInteractionEnabled:YES];
contentView.tag=currentTag;
[UIViewbeginAnimations:@"animationID" context:nil];
[UIViewsetAnimationDuration:0.7f];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIViewsetAnimationRepeatAutoreverses:NO];
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.view cache:YES];
[currentViewremoveFromSuperview];
[self.viewaddSubview:contentView];
[UIViewcommitAnimations];
原文来自:http://marshal.easymorse.com/archives/3760
相关文章推荐
- 【转载】android手势翻页效果
- 在android中实现手势翻页效果
- android中简单的图片翻页效果
- Android 手势检测及通过手势实现翻页效果
- 简单的书本翻页效果
- android开发教程:android手势翻页效果
- Android阅读器翻页效果附加代码【转载】
- Android使用手势实现翻页效果
- [简单分页]C#+JQUERY+ORACLE分页效果 ----转载
- Android手势翻页效果
- 一个JS的翻页效果(翻书效果)
- 使用手势实现翻页效果
- 麦子学院android开发教程:android手势翻页效果
- Gesture 通过手势实现翻书效果
- Android实现手势滑动和简单动画效果
- Android移动开发-通过手势实现翻页效果
- android中简单的图片翻页效果
- jQuery实现图片3D折叠,翻页,翻书效果
- 动画:UIKitAnimation 简单动画学习 iOS (一) 渐变 、 移动 、翻页、来回翻转 ——转载
- 简单做出HTML5翻页效果文字特效