HardBirch

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

时间:12-06-04 栏目:iOS游戏开发技术 作者:张飞不张,文采横飞 评论:3 点击: 2,216 次


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

  一开始以列表的方式显示向上或者向下滑动,切换布局方式,位置的信息记录在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】实现一种书本的展示特效

【IOS】实现一种书本的展示特效:目前有3 条留言

  1. 地板
    khwei2000:

    h

    2012-06-04 15:24 [回复]
  2. 板凳
    tge7618291:

    marK

    2012-07-24 23:01 [回复]
  3. 沙发
    dayuqi:

    我有点不解的是有必要用两个循环去维持数组中的书本的顺序么?(可能是特色来的么,呵呵)而且好像只调用了 第一本 和所点的那本书 进行交换(希望我是看疏忽了。。。)

    2012-10-27 00:54 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐