Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

CATransition 应用代码 View Transitions
-(void)applicationDidFinishLaunching: (UIApplication *)application

{


// Create the two views to transition between, and add them to our containerView. We'll hide the second one


// until we are ready to transition to it.


UIImage *image1 = [UIImage
imageWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:@"image1.jpg"
ofType:nil]];


view1 = [[UIImageView alloc] initWithImage:image1];


UIImage *image2 = [UIImage
imageWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:@"image2.jpg"
ofType:nil]];


view2 = [[UIImageView alloc] initWithImage:image2];


view2.hidden = YES;


[containerView
addSubview:view1];


[containerView
addSubview:view2];


transitioning = NO;

}

-(void)dealloc

{


[containerView
release];


[view1
release];


[view2
release];


[super
dealloc];

}

-(void)performTransition

{


// First create a CATransition object to describe the transition


CATransition *transition = [CATransition
animation];


// Animate over 3/4 of a second


transition.duration = 0.75;


// using the ease in/out timing function


transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];


// Now to set the type of transition. Since we need to choose at random, we'll setup a couple of arrays to help us.


NSString *types[4] = {kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};


NSString *subtypes[4] = {kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop,kCATransitionFromBottom};


int rnd = random() % 4;


transition.type = types[rnd];


if(rnd < 3) // if we didn't pick the fade transition, then we need to set a subtype too


{


transition.subtype = subtypes[random() % 4];


}


// Finally, to avoid overlapping transitions we assign ourselves as the delegate for the animation and wait for the


// -animationDidStop:finished: message. When it comes in, we will flag that we are no longer transitioning.


transitioning = YES;


transition.delegate = self;


// Next add it to the containerView's layer. This will perform the transition based on how we change its contents.


[containerView.layer addAnimation:transition forKey:nil];


// Here we hide view1, and show view2, which will cause Core Animation to animate view1 away and view2 in.


view1.hidden = YES;


view2.hidden = NO;


// And so that we will continue to swap between our two images, we swap the instance variables referencing them.


UIImageView *tmp = view2;


view2 = view1;


view1 = tmp;

}

-(void)animationDidStop: (CAAnimation *)theAnimation finished: (BOOL)flag

{


transitioning = NO;

}

-(IBAction)nextTransition: (id)sender

{


if(!transitioning)


{


[self
performTransition];


}

}
<< iphone的动画效果类型及实现方法iPhone中使用NSLocalizedString实现国际化 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.