iOS-實現簡單的動畫效果

來源:互聯網
上載者:User
文章目錄
  • 左右移動
  • 旋轉
  • 縮放

http://disanji.net/2010/12/18/ios-realize-animation-effect/

由 Fgamers 發表於 2010 年 12 月 18 日    89 次閱讀 評論 (0)

ios中最簡單的動畫效果,是沒有主要畫面格的,比如左右移動、上下跳動、旋轉和縮放。主要畫面格要稍微複雜一點,要設定路徑等。

iOS可以在不藉助OpenGL 2D等重型庫的情況下實現上述簡單的動畫效果。編寫起來十分簡單。

左右移動

上下移動的情況類似。

 

代碼如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
- (void)loadView {    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];    UIImage *image=[UIImage imageNamed:@"1.jpg"];     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();    CGContextRef context = CGBitmapContextCreate(NULL, 768, 1024, 8, 4 * 768, colorSpace, kCGImageAlphaPremultipliedFirst);    CGRect rect = CGRectMake(0, 0, 768, 1024);    CGColorRef fillColor = [[UIColor whiteColor] CGColor];    CGContextSetFillColor(context, CGColorGetComponents(fillColor));     CGContextMoveToPoint(context, 160.0f, 230.0f);    CGContextAddLineToPoint(context, 600.0f, 230.0f);    CGContextAddLineToPoint(context, 600.0f, 100.0f);    CGContextAddLineToPoint(context, 370.0f, 50.0f);    CGContextAddLineToPoint(context, 200.0f, 100.0f);     CGContextClosePath(context);    CGContextClip(context);    CGContextDrawImage(context, rect, image.CGImage);    CGImageRef imageMasked = CGBitmapContextCreateImage(context);    CGContextRelease(context);    UIImage *newImage = [UIImage imageWithCGImage:imageMasked];    CGImageRelease(imageMasked);     UIImageView *backView=[[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    [self.view addSubview:backView];    backView.image=newImage;    backView.alpha=0.3;     CABasicAnimation *theAnimation1;    //定義動畫     //左右搖擺    theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];    theAnimation1.fromValue=[NSNumber numberWithFloat:0];    theAnimation1.toValue=[NSNumber numberWithFloat:-100];     theAnimation1.duration=5.5;//動畫期間    theAnimation1.repeatCount=6;//動畫重複次數    theAnimation1.autoreverses=YES;//是否自動重複     [backView.layer addAnimation:theAnimation1 forKey:@"animateLayer"];     [newImage release];    [image release];}
旋轉

效果類似這樣:

 

這裡,圖做了pi(180°)的旋轉。只需把動畫部分代碼替換為:

12
theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform"];    theAnimation1.toValue = [ NSValue valueWithCATransform3D: CATransform3DMakeRotation(3.1415, 0, 0, 1.0) ];
縮放

縮放類似這樣:

 

代碼可替換為:

12
theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform.scale"];    theAnimation1.toValue = [NSNumber numberWithDouble:1.5];

原文連結:http://marshal.easymorse.com/archives/3727

轉載編輯: Fgamers轉載地址:http://disanji.net/2010/12/18/ios-realize-animation-effect/
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.