Created
November 24, 2010 09:09
-
-
Save marshluca/713367 to your computer and use it in GitHub Desktop.
TurnPageViewController
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
#import "TurnPageViewController.h" | |
@implementation TurnPageViewController | |
@synthesize viewArray; | |
@synthesize startPoint; | |
@synthesize currentpageNumber; | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
viewArray = [[NSMutableArray alloc]init]; | |
NSArray *dataArray = [[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",nil]; | |
for (NSString *i in dataArray) { | |
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:i]]; | |
imageView.userInteractionEnabled=YES; | |
imageView.frame=CGRectMake(0, 0, 768, 1024); | |
UITextView *textView = [[UITextView alloc] init]; | |
textView.text = @"我废旧塑料大量就立刻防腐剂是垃圾发流口水的积分类似的就逻辑电路我废旧塑料大量就立刻防腐剂是垃圾发流口水的积分类似的就逻辑电路我废旧塑料大量就立刻防腐剂是垃圾发流口水的积分类似的就逻辑电路我废旧塑料大量就立刻防腐剂是垃圾发流口水的积分类似的就逻辑电路我废旧塑料大量就立刻防腐剂是垃圾发流口水的积分类似的就逻辑电路"; | |
textView.backgroundColor = [UIColor grayColor]; | |
if ([i isEqual:@"1.jpg"]) { | |
textView.frame = CGRectMake(50, 100, 100, 100); | |
} else if ([i isEqual:@"2.jpg"]) { | |
textView.frame = CGRectMake(100, 200, 100, 100); | |
} else if ([i isEqual:@"3.jpg"]) { | |
textView.frame = CGRectMake(200, 300, 100, 100); | |
} else if ([i isEqual:@"4.jpg"]) { | |
textView.frame = CGRectMake(300, 400, 100, 100); | |
} | |
UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; | |
[pageView addSubview:imageView]; | |
[pageView addSubview:textView]; | |
[imageView release]; | |
[textView release]; | |
[viewArray addObject:pageView]; | |
} | |
self.view.userInteractionEnabled = YES; | |
[self.view insertSubview:[viewArray objectAtIndex:0] atIndex:0]; | |
} | |
-(void) showAlertViewWithMessage:(NSString *)message | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self | |
cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
[alert release]; | |
} | |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
UITouch *touch = [touches anyObject]; | |
CGPoint point = [touch locationInView:self.view]; | |
self.startPoint = point; | |
} | |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
UITouch *touch = [touches anyObject]; | |
CGPoint point = [touch locationInView:self.view]; | |
if (fabs(point.x - self.startPoint.x) > 50) | |
{ | |
if ((point.x - self.startPoint.x) > 0) //向右边滑动,表示得到上一页 | |
{ | |
currentpageNumber = currentpageNumber - 1; | |
if (self.currentpageNumber >= 0) { | |
UIView *nextView = [viewArray objectAtIndex:currentpageNumber]; | |
[self performTransitionRight:nextView]; | |
} else { | |
[self showAlertViewWithMessage:@"已经是第一页"]; | |
currentpageNumber=0; | |
} | |
} | |
else //向左滑动,表示得到下一页 | |
{ | |
currentpageNumber = currentpageNumber + 1; | |
if (self.currentpageNumber<[viewArray count]) { | |
UIView *prevView = [viewArray objectAtIndex:currentpageNumber]; | |
[self performTransitionLeft:prevView]; | |
} else { | |
[self showAlertViewWithMessage:@"已经是最后一页"]; | |
self.currentpageNumber=[viewArray count]-1; | |
} | |
} | |
} | |
} | |
-(void)performTransitionLeft:(UIView *)View{ | |
CATransition *transition = [CATransition animation]; | |
transition.duration = 0.75; | |
transition.type = @"pageCurl"; | |
transition.subtype = kCATransitionFromRight; | |
[[self.view.subviews objectAtIndex:0] removeFromSuperview]; | |
[self.view insertSubview:View atIndex:0]; | |
[self viewWillDisappear:YES]; | |
[self.view.layer addAnimation:transition forKey:nil]; | |
} | |
-(void)performTransitionRight:(UIView *)View{ | |
CATransition *transition = [CATransition animation]; | |
transition.duration = 0.75; | |
transition.type = @"pageCurl"; | |
transition.subtype = kCATransitionFromLeft; | |
[[self.view.subviews objectAtIndex:0] removeFromSuperview]; | |
[self.view insertSubview:View atIndex:0]; | |
[self viewWillDisappear:YES]; | |
[self.view.layer addAnimation:transition forKey:nil]; | |
} | |
// Override to allow orientations other than the default portrait orientation. | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
return YES; | |
} | |
- (void)didReceiveMemoryWarning { | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
- (void)viewDidUnload { | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
self.viewArray = nil; | |
} | |
- (void)dealloc { | |
[viewArray release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment