原文链接: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
http://download.csdn.net/detail/toss156/4350495