手势做的简单的翻页效果 翻书带背影效果

手势做的简单的翻页效果 翻书带背影效果,第1张

概述原文地址:手势做的简单的翻页效果 作者:醉吻花颜 翻页效果,类似下面的样子: 在电子书应用中会很常见。这里需要两个要点: 翻页动画 手势上下轻扫(swipe)的处理 先说一下轻扫(swipe)的实现,可以参考编写简单的手势示例:Tap了解手势种类。 在viewDidLoad方法中注册了对上、下、左、右四个方向轻松的处理方法: - (void)viewDidLoad {        UISwipe 原文地址:手势做的简单的翻页效果 作者:醉吻花颜

翻页效果,类似下面的样子:


在电子书应用中会很常见。这里需要两个要点:

翻页动画 手势上下轻扫(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=[[AVAudioPlayer 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 总结

以上是内存溢出为你收集整理的手势做的简单的翻页效果 翻书带背影效果全部内容,希望文章能够帮你解决手势做的简单的翻页效果 翻书带背影效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1177025.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存