当前位置: 首页 > news >正文

【IOS】实现一种书本的展示特效

【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。

原文地址:http://blog.csdn.net/toss156/article/details/7630518


最近在做一个电子书的项目,其中有一个书架的功能。看了很多其他应用的书架,有些实现的效果真的很不错,就比如《宝宝爱看书》,于是也仿着自己写了一个,虽然最后没能用在项目中,但是还是觉得很不错,在这里和大家分享一下怎么实现的。

一开始以列表的方式显示向上或者向下滑动,切换布局方式,位置的信息记录在plist中点击其中任意一本书,选中的书本移动到最前面,只有在最前面的书本是可以直接打开的。


// // UIBook.h // BookShelf // // Created by zhouhaifeng on 12-6-1. // Copyright (c) 2012年 zhouhaifeng. All rights reserved. // #import <UIKit/UIKit.h> @interface UIBook : UIButton { id bookdelegate; int downPrecent; UILabel *precent; } @property (nonatomic) UILabel *precent; @property (nonatomic) id bookdelegate; -(void) setDownloadPrecent:(int) value; -(void) OnButtonClicked:(id)sender; @end @protocol UIBookDelegate <NSObject> @optional - (void)UIBook:(UIBook *)book clickedButtonAtIndex:(NSInteger)buttonIndex; @end
// // UIBook.m // BookShelf // // Created by zhouhaifeng on 12-6-1. // Copyright (c) 2012年 zhouhaifeng. All rights reserved. // #import "UIBook.h" @implementation UIBook @synthesize precent,bookdelegate; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { downPrecent = 100; // Initialization code [self setImage:[UIImage imageNamed:@"bookcover_temp.jpg"] forState:UIControlStateNormal]; UIImageView* progressbg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"loading_bg.png"]]; [progressbg setFrame:CGRectMake(100, 200, 96, 96)]; [self addSubview:progressbg]; UIImageView* loading_ring = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"loading_ring.png"]]; [loading_ring setFrame:CGRectMake(0, 0, 96, 96)]; [progressbg addSubview:loading_ring]; [UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationOptionRepeat|UIViewAnimationCurveLinear animations:^{ loading_ring.transform = CGAffineTransformMakeRotation(M_PI); } completion:nil]; precent = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 96, 96)]; [precent setText:[NSString stringWithFormat:@"%d%%",downPrecent]]; [precent setTextColor:[UIColor whiteColor]]; [precent setBackgroundColor:[UIColor clearColor]]; [precent setTextAlignment:UITextAlignmentCenter]; [progressbg addSubview:precent]; [self addTarget:self action:@selector(OnButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; } return self; } -(void) OnButtonClicked:(id)sender { if (bookdelegate) { if ([bookdelegate respondsToSelector:@selector(UIBook:clickedButtonAtIndex:)]) { [bookdelegate UIBook:self clickedButtonAtIndex:self.tag]; } }else { NSLog(@"these is no delegate"); } } -(void) setDownloadPrecent:(int) value { downPrecent = value; [precent setText:[NSString stringWithFormat:@"%d%%",downPrecent]]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
// // ViewController.h // BookShelf // // Created by zhouhaifeng on 12-6-1. // Copyright (c) 2012年 zhouhaifeng. All rights reserved. // #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> #import "UIBook.h" typedef enum { LayoutStateLine = 0, LayoutStateRandom }LayoutState; @interface ViewController : UIViewController<UIGestureRecognizerDelegate,UIBookDelegate> { NSMutableArray *bookArray; LayoutState layoutState; } -(void) swapBooks:(NSInteger) bookA TwoBooks:(NSInteger) bookB; @property (nonatomic) NSMutableArray *bookArray; @end
// // ViewController.m // BookShelf // // Created by zhouhaifeng on 12-6-1. // Copyright (c) 2012年 zhouhaifeng. All rights reserved. // #define NUMBER 8 #import "ViewController.h" @interface ViewController () -(void) didSwipe:(UISwipeGestureRecognizer *) recognizer; -(void) layoutBooks; @end @implementation ViewController @synthesize bookArray; - (void)viewDidLoad { [super viewDidLoad]; [self.view setUserInteractionEnabled:YES]; bookArray = [NSMutableArray arrayWithCapacity:10]; layoutState = LayoutStateLine; //添加书本 for (int i = 0; i<NUMBER; i++) { UIBook *book = [UIBook buttonWithType:UIButtonTypeCustom]; [book setBookdelegate:self]; [self.view addSubview:book]; [bookArray addObject:book]; [book setDownloadPrecent:i*10]; book.layer.shadowOffset = CGSizeMake(1,3); book.layer.shadowColor = [UIColor blackColor].CGColor; book.layer.shadowOpacity = 1.0f; } [self layoutBooks]; //添加手势 UISwipeGestureRecognizer *pinTouches = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(didSwipe:)]; pinTouches.direction= UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:pinTouches]; } -(void) didSwipe:(UISwipeGestureRecognizer *) recognizer; { if (recognizer.state == UIGestureRecognizerStateEnded) { if (layoutState == LayoutStateLine) { layoutState = LayoutStateRandom; NSLog(@"layoutState----------LayoutStateLine:%d",layoutState); [self layoutBooks]; }else if (layoutState == LayoutStateRandom){ layoutState = LayoutStateLine; NSLog(@"layoutState---------LayoutStateRandom:%d",layoutState); [self layoutBooks]; } } } -(void) layoutBooks { if (layoutState == LayoutStateRandom) { NSString *imagePath= [[NSBundle mainBundle] pathForResource:@"bookshelf" ofType:@"plist"]; NSArray *imageDiction= [NSArray arrayWithContentsOfFile:imagePath]; NSArray *dataFromPlist=[NSArray arrayWithArray:[imageDiction objectAtIndex:NUMBER-1]]; for (int j=0; j<dataFromPlist.count; j++) { NSDictionary *bookdata=[NSDictionary dictionaryWithDictionary:[dataFromPlist objectAtIndex:j]]; UIBook *book = [bookArray objectAtIndex:j]; [book setTag:j]; NSString *str = [bookdata objectForKey:@"center"]; CGPoint centerPoint = CGPointFromString(str); CGFloat angle = [[bookdata objectForKey:@"angle"]floatValue]; // CGFloat scale = [[bookdata objectForKey:@"scale"]floatValue]; [UIView animateWithDuration:0.5 animations:^{ [book setCenter:centerPoint]; [book setTransform:CGAffineTransformMakeRotation(angle/180*M_PI)]; [self.view sendSubviewToBack:book]; //[book setTransform:CGAffineTransformMakeScale(scale, scale)]; }]; } }else if (layoutState == LayoutStateLine) { for (int j=0; j<NUMBER; j++) { [UIView animateWithDuration:0.5 animations:^{ UIBook *book = [bookArray objectAtIndex:j]; [book setTag:j]; [book setTransform:CGAffineTransformMakeRotation(0)]; [book setFrame:CGRectMake(40+250*(j%4), 30+350*(j/4), 200, 300)]; [self.view sendSubviewToBack:book]; }]; } } } -(void) swapBooks:(NSInteger) bookA TwoBooks:(NSInteger)bookB { for (int n=0; n<bookA-bookB;n++) { UIBook *first = [bookArray objectAtIndex:0]; for(int i=0; i<NUMBER-1; i++) { UIBook *book2 = [bookArray objectAtIndex:i+1]; [bookArray replaceObjectAtIndex:i withObject:book2]; } [bookArray replaceObjectAtIndex:(NUMBER-1) withObject:first]; } [self layoutBooks]; } - (void)UIBook:(UIBook *)book clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex!=0) { [self swapBooks:buttonIndex TwoBooks:0]; }else { NSLog(@"Open the books"); } } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } @end
Demo的下载地址:http://download.csdn.net/detail/toss156/4350495


相关文章:

  • asp.net webform设计思路的思考
  • 给自己的应用添加iAd广告之一
  • virsh查看迁移信息的两个命令
  • 【iOS-Cocos2d游戏开发】触屏事件处理机制
  • 迷宫里的动态规划应用
  • Django学习手册 - cookie / session
  • We are unable to complete the review of your app since one or more of your In App Purchases have not
  • IOS内存管理
  • gerrit + ldap + phpldapadmin docker部署
  • 【编程之美】2.1 - 求二进制数中1的个数
  • JavaScript中数组的排序方法:1.冒泡排序 2.选择排序
  • js计算页面加载时间
  • Solium代码测试框架
  • 迎接第五次工业革命浪潮,不当纳米知识文盲
  • 12-单表查询
  • [PHP内核探索]PHP中的哈希表
  • 5、React组件事件详解
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • co模块的前端实现
  • DOM的那些事
  • Java 网络编程(2):UDP 的使用
  • Java知识点总结(JavaIO-打印流)
  • Less 日常用法
  • Quartz初级教程
  • Redis学习笔记 - pipline(流水线、管道)
  • SpiderData 2019年2月16日 DApp数据排行榜
  • Web设计流程优化:网页效果图设计新思路
  • 产品三维模型在线预览
  • 从零开始在ubuntu上搭建node开发环境
  • 读懂package.json -- 依赖管理
  • 多线程 start 和 run 方法到底有什么区别?
  • 分布式熔断降级平台aegis
  • 浮现式设计
  • 技术胖1-4季视频复习— (看视频笔记)
  • 日剧·日综资源集合(建议收藏)
  • 我的业余项目总结
  • 【干货分享】dos命令大全
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • # Apache SeaTunnel 究竟是什么?
  • (1)(1.13) SiK无线电高级配置(五)
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (Java)【深基9.例1】选举学生会
  • (java)关于Thread的挂起和恢复
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (动态规划)5. 最长回文子串 java解决
  • (附源码)计算机毕业设计大学生兼职系统
  • (力扣题库)跳跃游戏II(c++)
  • (十三)Maven插件解析运行机制
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • .Net 4.0并行库实用性演练
  • .NET Core 中的路径问题
  • .NET Core中Emit的使用
  • .Net(C#)自定义WinForm控件之小结篇
  • .net的socket示例