天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

实现iPhone漂亮的动画效果主要有两种方法,

   一种是UIView层面的,

  一种是使用CATransition进行更低层次的控制,

 

      第一种是UIView,UIView方式可能在低层也是使用CATransition进行了封装,它只能用于一些简单的、常用的效果展现,这里写一个常用的示例代码,供大家参考。

 

Cpp代码 
  1.  [UIView beginAnimations:@"Curl"context:nil];//动画开始   
  2.  [UIView setAnimationDuration:0.75];   
  3.  [UIView setAnimationDelegate:self];  
  4.  [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES];   
  5. [myview removeFromSuperview];   
  6. [UIView commitAnimations];  

 

 

       第二种方式相对复杂一些,但如果更好的进行控制,还是使用这种方法吧,

基本使用方法可以看一下如下例子:

 

Cpp代码 
  1. CATransition *animation = [CATransition animation];  
  2. [animation setDuration:1.25f];   
  3. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];   
  4. [animation setType:kCATransitionReveal];  
  5. [animation setSubtype: kCATransitionFromBottom];  
  6. [self.view.layer addAnimation:animation forKey:@"Reveal"];  

 

这里使用了setType与setSubtype组合,这使用个比较保险,因为他的参数就是官方API里定义的,他们的参数说明可以参考如下:

 

[animation setType:@"suckEffect"];

这里的suckEffect就是效果名称,可以用的效果主要有:

Cpp代码 
  1. pageCurl 向上翻一页   
  2.   
  3. pageUnCurl 向下翻一页   
  4.   
  5. rippleEffect 滴水效果   
  6.   
  7. suckEffect 收缩效果,如一块布被抽走   
  8.   
  9. cube 立方体效果   
  10.   
  11. oglFlip 上下翻转效果   

 I have been using the latter for a lot of nice lightweight animations. You can use it crossfade two views, or fade one in in front of another, or fade it out. You can shoot a view over another like a banner, you can make a view stretch or shrink... I'm getting a lot of mileage out of beginAnimation/commitAnimations.

Don't think that all you can do is:

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];

Here is a sample:

[UIView beginAnimations:nil context:NULL]; {
   
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
   
[UIView setAnimationDuration:1.0];
       
[UIView setAnimationDelegate:self];
       
if (movingViewIn) {
// after the animation is over, call afterAnimationProceedWithGame
//  to start the game
               
[UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];

//              [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation
//              [UIView setAnimationDelay:0.50];
//              [UIView setAnimationRepeatAutoreverses:YES];

                gameView
.alpha = 1.0;
                topGameView
.alpha = 1.0;
                viewrect1
.origin.y = selfrect.size.height - (viewrect1.size.height);
                viewrect2
.origin.y = -20;

                topGameView
.alpha = 1.0;
       
}
       
else {
       
// call putBackStatusBar after animation to restore the state after this animation
               
[UIView setAnimationDidStopSelector:@selector(putBackStatusBar)];
                gameView
.alpha = 0.0;
                topGameView
.alpha = 0.0;
       
}
       
[gameView setFrame:viewrect1];
       
[topGameView setFrame:viewrect2];

} [UIView commitAnimations];

As you can see, you can play with alpha, frames, and even sizes of a view. Play around. You may be surprised with its capabilities.

 

 ==================

Dashed & solid line when using CGContext drawing

To get dashed line:

CGContextRef context = UIGraphicsGetCurrentContext();

float dash[2]={6 ,5}; // pattern 6 times “solid”, 5 times “empty”

CGContextSetLineDash(context,0,dash,2);

To get back to solid line:

float normal[1]={1};

CGContextSetLineDash(context,0,normal,0); 

posted on 2010-09-14 13:57  老舟  阅读(2313)  评论(0编辑  收藏  举报