【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址: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
分享到:
相关推荐
IOS书本展示效果,类似于 AppleTree 的产品宝宝爱看书中的效果 http://blog.csdn.net/toss156/article/details/7630518
- [iOS动画和特效(一)UIView动画和CoreAnimation](http://liuyanwei.jumppo.com/2015/10/30/iOS-Animation-UIViewAndCoreAnimation.html) - [iOS动画和特效(二)UIKit力学行为]...
ios源码之3D书本翻页效果.rar
ios实现viewcontroller切换传值的三种方法
CSS3实现ios7计算器 CSS3实现ios7计算器网页特效.zip
iOS 使用tableview实现下拉选项框,可以直接使用
这是一份IOS实现二维码扫描功能的完整代码,简单明了
cocos2d 粒子特效 ios game
iOS 实现了 相册添加图片,多选,功能完善
CSS3+JS实现iOS8风格loading加载进度条特效源码.zip
使用EGOTableViewPullRefresh开源项目在IOS中实现UITableView的下拉刷新功能,对源码进行了一些改动,支持中英文显示,刷新时间格式也做了修改
IOS实现中间凸起圆形TabBar,类似于闲鱼的效果
这是一个ios的重力特效的一个demo,里面包括了不同的效果
iOS 10 in Swift 3 非常好的ios书! 非常好的ios书! 非常好的ios书! 非常好的ios书! 非常好的ios书! 非常好的ios书!
一句代码搞定 轮播特效!非常实用 GitHub:https://github.com/TonyDongDong/CollectionView.git
抽屉效果ios实现
iOS 实现一个类似电商购物车界面示例 主要实现了商品的展示,并且可以对商品进行多选操作,以及改变商品的购买数量。与此同时,计算出,选中的总价格。
iOS实现类似安卓的加锁效果; 欢迎提出更好的建议
jquery苹果IOS滑块banner焦点图特效
这是一个实现侧边栏的Demo,适配了ios7