2011-09-11 22:14:31| 分类: iOS&Mac|字号 订阅
卷曲和翻转动画
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
//[self.blueViewController.view removeFromSuperview];
//[self.view insertSubview:yellowViewController.view atIndex:0];
[yellowViewController viewDidDisappear:YES];
[blueViewController viewDidAppear:YES];
[UIView commitAnimations];//提交动画
-------------------------------------------------
给一般属性变化应用动画
UIImage* tImage2=[[UIImage imageNamed:@"t.jpg"]autorelease];
UIImageView* t2=[[[UIImageViewalloc]initWithFrame:CGRectMake(tImage.size.width,100,tImage.size.width,tImage.size.height)]autorelease];
t2.image=tImage2;
[tscrollView addSubview:t2];
[UIView beginAnimations:@"animation2" context:t2];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationStopped:finished:context:)];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:2];
// t2.transform=CGAffineTransformMakeScale(1.25, 1.25);//一系列动画函数,可以实现二维变换
t2.transform=CGAffineTransformMakeRotation(3.14149/2);
//合并多个动画
//CGAffineTransformConcat(<#CGAffineTransform t1#>, <#CGAffineTransform t2#>)
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView commitAnimations];
---------------------------------------------------------------------------
使用UIImageView播放动画图片
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.gif"],
[UIImage imageNamed:@"image2.gif"],
[UIImage imageNamed:@"image3.gif"],
[UIImage imageNamed:@"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
// 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;
//ios5
CATransition* animation=[CATransition animation];
animation.type = @"rippleEffect";
animation.duration=1.2;
[self.containerView.layer addAnimation:animation forKey:@"ripple"];
// < ios 5
[UIView beginAnimations:@"ripper" context:NULL];
[UIView setAnimationTransition:110 forView:self.contentView cache:YES];
[UIView setAnimationDuration:2.0];
[UIView commitAnimations];
//flip 绕y轴翻转动画
#import <QuartzCore/QuartzCore.h>
UIViewController* current=self.window.rootViewController;
//set perspective
CATransform3D sublayerTransform = CATransform3DIdentity;
sublayerTransform.m34 = 1.0 / -1000;
[self.window.layer setSublayerTransform:sublayerTransform];
//set anchorPoint
current.view.layer.anchorPoint = CGPointMake(0.0f, 0.5f);
current.view.center = CGPointMake(current.view.center.x - current.view.bounds.size.width/2.0f, current.view.center.y);
//apply animation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.duration=0.55f;
animation.repeatCount=1;
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0, 0, 1, 0)];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI/2, 0, 1,0)];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[current.view.layer addAnimation:animation forKey:@"flip"];
[[DownloadManager sharedInstance]checkAndUpdate];
- (void) doCurl
{
//创建CATransition对象
CATransition *animation = [CATransition animation];
//相关参数设置
[animation setDelegate:self];
[animation setDuration:1.0f];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
//向上卷的参数
//卷曲程度,0为不卷曲,1为完全卷曲
float curlDegree=0.8;
if(!isCurl)
{
//设置动画类型为pageCurl,并只卷一半
[animation setType:@"pageCurl"];
animation.endProgress=curlDegree;
}
//向下卷的参数
else
{
//设置动画类型为pageUnCurl,并从一半开始向下卷
[animation setType:@"pageUnCurl"];
animation.startProgress=1-curlDegree;
}
//卷的过程完成后停止,并且不从层中移除动画
[animation setFillMode:kCAFillModeForwards];
[animation setSubtype:kCATransitionFromBottom];
[animation setRemovedOnCompletion:NO];
isCurl=!isCurl;
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[self.view layer] addAnimation:animation forKey:@"pageCurlAnimation"];
if([switchView isOn])
{
switchStatusLabel.text=@"ON";
}
else
{
switchStatusLabel.text=@"OFF";
}
}
/* self.myView.layer.position=CGPointMake(self.myView.layer.position.x, self.myView.layer.position.y+50);
CABasicAnimation* drop=[CABasicAnimation animationWithKeyPath:@"position.y"];
drop.fromValue=[NSNumber numberWithFloat:self.myView.layer.position.y-50];
drop.toValue=[NSNumber numberWithFloat:self.myView.layer.position.y];
drop.duration=3;
[self.myView.layer addAnimation:drop forKey:nil];*/
float y=self.myView.layer.position.y;
CAKeyframeAnimation* bounce=[CAKeyframeAnimation animationWithKeyPath:@"position.y"];
bounce.duration=4;
self.myView.layer.position=CGPointMake(self.myView.layer.position.x, y+200);
bounce.keyTimes=[NSArray arrayWithObjects:[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:0.75],
[NSNumber numberWithFloat:1], nil ];
bounce.values=[NSArray arrayWithObjects:[NSNumber numberWithFloat:y],
[NSNumber numberWithFloat:y+50],
[NSNumber numberWithFloat:y-100],
[NSNumber numberWithFloat:y+100],
[NSNumber numberWithFloat:y+200],nil ];
bounce.calculationMode=kCAAnimationCubic;
CAKeyframeAnimation* bgcolor=[CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
bgcolor.duration=4;
bgcolor.keyTimes=[NSArray arrayWithObjects:[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:0.75],
[NSNumber numberWithFloat:1], nil ];
bgcolor.values=[NSArray arrayWithObjects:(id)[UIColor blueColor].CGColor,
(id)[UIColor redColor].CGColor,
(id)[UIColor yellowColor].CGColor,
(id)[UIColor blackColor].CGColor,
(id)[UIColor greenColor].CGColor,nil ];
//bgcolor.calculationMode=kCAAnimationCubic;
CAAnimationGroup* animations=[CAAnimationGroup animation];
animations.animations=[NSArray arrayWithObjects:bounce,bgcolor, nil];
animations.duration=4;
[self.myView.layer addAnimation:animations forKey:@"bounce"];