文章目錄
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/