翻页效果,类似下面的样子:
在电子书应用中会很常见。这里需要两个要点:
翻页动画 手势上下轻扫(swipe)的处理
先说一下轻扫(swipe)的实现,可以参考编写简单的手势示例:Tap了解手势种类。
在vIEwDIDLoad方法中注册了对上、下、左、右四个方向轻松的处理方法:
- (voID)vIEwDIDLoad {
UISwipeGestureRecognizer *recognizer;
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizer DirectionRight)];
[[self vIEw]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizer DirectionUp)];
[[self vIEw]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizer DirectionDown)];
[[self vIEw]addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizersetDirection:(UISwipeGestureRecognizer Directionleft)];
[[self vIEw]addGestureRecognizer:recognizer];
[recognizerrelease];
[supervIEwDIDLoad];
可以看到,都是同一个方法,handleSwipeFrom。
在该方法中,再识别具体是哪个方向的轻扫手势,比如判断是向下的轻扫:
-(voID)handleSwipeFrom:(UISwipeGestureRecognizer
*)recognizer{
NSLog(@"Swipe received.");
if(recognizer.direction==UISwipeGestureRecognizer DirectionDown){
NSLog(@"swipe down");
判断是向上的轻扫:
if (recognizer.direction==UISwipeGestureRecognizer
DirectionUp){
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:UIVIEwAnimationTransitionCurlUp forVIEw: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: UIStatusBaranimationSlID e];
UIImageVIEw*contentVIEw = [[UIImageVIEw alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];
[contentVIEwsetimage:[UIImage imagenamed:[pagesobjectAtIndex:(currentTag-1)]]];
[contentVIEwsetUserInteractionEnable d:YES];
contentVIEw.tag=currentTag;
在翻页时的处理:
if (currentTag<[pages count]) {
以上是内存溢出为你收集整理的手势做的简单的翻页效果 翻书带背影效果全部内容,希望文章能够帮你解决手势做的简单的翻页效果 翻书带背影效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)