Created
December 17, 2010 07:11
-
-
Save marshluca/744598 to your computer and use it in GitHub Desktop.
PageView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MPageView.h | |
// Magazine | |
// | |
// Created by wang xuefeng on 10-12-9. | |
// Copyright 2010 www.5yi.com. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface MPageView : UIScrollView { | |
UIImageView *imageView; | |
UILabel *textLabel; | |
CGPoint startTouchPosition; | |
NSString *swipType; | |
} | |
@property (nonatomic, retain) UIImageView *imageView; | |
@property (nonatomic, retain) UILabel *textLabel; | |
- (id)initWithPageIndex:(int)index PageDict:(NSDictionary *)dict1 MagazineDict:(NSDictionary *)dict2; | |
- (void)loadContentWithIndex:(int)index pageDict:(NSDictionary *)pageDict magazineDict:(NSDictionary *)magazineDict; | |
- (CGFloat)getStringHeight:(NSString *)string font:(UIFont *)font; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MPageView.m | |
// Magazine | |
// | |
// Created by wang xuefeng on 10-12-9. | |
// Copyright 2010 www.5yi.com. All rights reserved. | |
// | |
#import "MPageView.h" | |
#import "MUtility.h" | |
#import "MFoundation.h" | |
#import "RootViewController.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation MPageView | |
@synthesize imageView; | |
@synthesize textLabel; | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
if ((self = [super initWithFrame:frame])) | |
{ | |
} | |
return self; | |
} | |
- (id)initWithPageIndex:(int)index PageDict:(NSDictionary *)dict1 MagazineDict:(NSDictionary *)dict2; | |
{ | |
self = [super init]; | |
self.scrollEnabled = YES; | |
self.multipleTouchEnabled = YES; | |
[self loadContentWithIndex:index pageDict:dict1 magazineDict:dict2]; | |
return self; | |
} | |
- (void)loadContentWithIndex:(int)index pageDict:(NSDictionary *)pageDict magazineDict:(NSDictionary *)magazineDict; | |
{ | |
// init image view here | |
imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
imageView.backgroundColor = [MUtility randomColor]; | |
NSString *bookDirectory = [magazineDict objectForKey:@"book_directory"]; | |
NSString *imageName = [pageDict objectForKey:@"image_name"]; | |
NSString *imageFile = [NSString stringWithFormat:@"%@/%@",bookDirectory,imageName]; | |
NSString *imagePath = [MUtility documentPathForFile:imageFile]; | |
imageView.image = [UIImage imageWithContentsOfFile:imagePath]; | |
[self addSubview:imageView]; | |
// init text label here | |
textLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
textLabel.backgroundColor = [MUtility randomColor]; | |
textLabel.textColor = [UIColor whiteColor]; | |
textLabel.lineBreakMode = UILineBreakModeWordWrap; | |
textLabel.font = [UIFont systemFontOfSize:15.0f]; | |
textLabel.text = [pageDict objectForKey:@"text_content"]; | |
textLabel.numberOfLines = 0; | |
[self addSubview:textLabel]; | |
CGFloat imageWidth = PAGE_WIDTH - PADDING*2 ; | |
CGFloat imageHeight = PAGE_HEIGHT; | |
CGFloat textHeight = [self getStringHeight:textLabel.text font:textLabel.font]; | |
self.imageView.frame = CGRectMake(PADDING, PADDING, imageWidth, imageHeight); | |
self.textLabel.frame = CGRectMake(PADDING, 20+self.imageView.frame.size.height+PADDING, PAGE_WIDTH-PADDING*2, textHeight); | |
CGFloat pageHeight = self.textLabel.frame.origin.y + self.textLabel.frame.size.height + 20; | |
self.frame = CGRectMake(0.0f, 0.0f, PAGE_WIDTH,pageHeight); | |
self.contentSize = CGSizeMake(self.frame.size.width , pageHeight); | |
[imageView release]; | |
[textLabel release]; | |
} | |
/* | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
- (void)drawRect:(CGRect)rect { | |
// Drawing code. | |
} | |
*/ | |
- (void)dealloc { | |
[super dealloc]; | |
} | |
#pragma mark - | |
#pragma mark custom methods | |
- (CGFloat)getStringHeight:(NSString *)string font:(UIFont *)font { | |
CGSize size = [string sizeWithFont:font | |
constrainedToSize:CGSizeMake(768-PADDING*2, 1000000.0f) | |
lineBreakMode:UILineBreakModeWordWrap]; | |
return size.height; | |
} | |
#pragma mark - | |
#pragma mark touches methods | |
- (void)setToolbarHidden | |
{ | |
RootViewController *rootController = (RootViewController *)[self.superview.superview nextResponder]; | |
BOOL isVisible = rootController.toolbarIsHidden; | |
[rootController.topToolbar setVisible:!isVisible]; | |
[rootController.bottomToolbar setVisible:!isVisible]; | |
rootController.toolbarIsHidden = !isVisible; | |
} | |
#define kAnimationKey @"transitionViewAnimation" | |
#define HORIZ_SWIPE_DRAG_MIN 12 | |
#define VERT_SWIPE_DRAG_MAX 4 | |
// The following swipe code derives from Apple Sample Code | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
if ([touches count] == 1) { | |
[self setToolbarHidden]; | |
} | |
UITouch *touch = [touches anyObject]; | |
startTouchPosition = [touch locationInView:self]; | |
swipType = NULL; | |
} | |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
UITouch *touch = touches.anyObject; | |
CGPoint currentTouchPosition = [touch locationInView:self]; | |
if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN && | |
fabsf(startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX) | |
{ | |
// Horizontal Swipe | |
if (startTouchPosition.x < currentTouchPosition.x) | |
swipType = kCATransitionFromLeft; | |
else | |
swipType = kCATransitionFromRight; | |
} else { | |
// Process a non-swipe event. | |
swipType = NULL; | |
} | |
} | |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
if (swipType) | |
{ | |
CATransition *animation = [CATransition animation]; | |
[animation setDelegate:self]; | |
[animation setType:kCATransitionPush]; | |
// [animation setType:@"oglFlip"]; | |
[animation setSubtype:swipType]; | |
[animation setDuration:0.5f]; | |
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; | |
// CATransition *animation = [self getAnimation:swipType]; | |
// self.superview is helloController's view | |
if (swipType == kCATransitionFromLeft) { | |
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; | |
} else { | |
[[self superview] exchangeSubviewAtIndex:2 withSubviewAtIndex:1]; | |
} | |
[[[self superview] layer] addAnimation:animation forKey:kAnimationKey]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment