2010/12/16 朱中華. 這個程式會先在 iPhone 螢幕上顯示 This is the Front View 及箭頭, click 任何地方後, 畫面會翻轉 (transition) 至下個畫面 ---This

Embed Size (px)

DESCRIPTION

 新產生 FlipView class 的 touchesEnded : 產生 flip 效果的方式由 14 、 19 行達成,  14 行, [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];  15 行, [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1 ];

Citation preview

2010/12/16 iPhone This is the Front View click (transition) ---This is the Back View click FlipView class touchesEnded flip 14 19 14 [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES]; 15 [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1 ]; #import // Note: although Apple promised UIViewAnimationTransitionCurlUp as a transition, it's not live FlipViewFlipView - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { // Start Animation Block CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; // Animations [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; // Commit Animation Block [UIView commitAnimations]; loadView 38~44 This is the Front View This is the Back View load 47~54 "This is the Front View" label label "This is the Back View" 66~67 addSubview:backView addSubview:frantView frontView front HelloControllerHelloController - (void)loadView { // Create the main view UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor blackColor]; self.view = contentView; [contentView release]; FlipView *frontView = [[FlipView alloc] initWithFrame:self.view.bounds]; frontView.image = [UIImage frontView.userInteractionEnabled = YES; FlipView *backView = [[FlipView alloc] initWithFrame:self.view.bounds]; backView.image = [UIImage backView.userInteractionEnabled = YES; // Add an identifying label for front CGRect labelFrame = CGRectMake(40.0f, 200.0f, 240.0f, 60.0f); UILabel *frontLabel = [[UILabel alloc] initWithFrame:labelFrame]; frontLabel.text is the Front View"; frontLabel.font = [UIFont size:24.0f]; frontLabel.textColor = [UIColor colorWithRed:0.82f green:1.0f blue:0.286f alpha:1.0f]; frontLabel.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]; [frontView addSubview:frontLabel]; [frontLabel release]; // Add an identifying label for back UILabel *backLabel = [[UILabel alloc] initWithFrame:labelFrame]; backLabel.text is the Back View"; backLabel.font = [UIFont size:24.0f]; backLabel.textColor = [UIColor colorWithRed:0.82f green:1.0f blue:0.286f alpha:1.0f]; backLabel.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]; [backView addSubview:backLabel]; [backLabel release]; // add the views [self.view addSubview:backView]; [self.view addSubview:frontView]; [backView release]; [frontView release]; SampleAppDelegateSampleAppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; HelloController *hello = [[HelloController alloc] init]; [window addSubview:hello.view]; [window makeKeyAndVisible]; int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, [pool release]; return retVal; } click swipe FlipView class instance variable startTouchPosition dirString click swipe init setMultipleTouchEnabled getAnimation: direction animation type flip #import #importFlipView : UIImageView { CGPoint startTouchPosition; NSString *dirString; FlipView - (FlipView *) init { self = [super init]; [self setMultipleTouchEnabled:YES]; return self; } - (CATransition *) getAnimation:(NSString *) direction { CATransition *animation = [CATransition animation]; [animation setDelegate:self]; // [animation [animation setType:kCATransitionPush]; [animation setSubtype:direction]; [animation setDuration:1.0f]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; return animation; } touchesBegan startTouchPosition touchesMoved ( startTouchesPosition.x - currentTouchPosition.x ) ( startTouchesPosition.y - currentTouchPosition.y ) ( startTouchesPosition.y - currentTouchPosition.y ) ( startTouchesPosition.x - currentTouchPosition.x ) touchesEnded dirString animation #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 { UITouch *touch = [touches anyObject]; startTouchPosition = [touch locationInView:self]; dirString = 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) = HORIZ_SWIPE_DRAG_MIN && fabsf(startTouchPosition.x - currentTouchPosition.x)