iPhone开发【十三】动画效果之最简单的动画——动态加载图片
发布日期:2021-09-28 18:46:29 浏览次数:8 分类:技术文章

本文共 2664 字,大约阅读时间需要 8 分钟。

转载请注明出处,原文网址:作者:张燕广

从本篇开始不再详细介绍每一步操作,而只介绍一些关键操作及展示核心代码和代码解释。

实现的功能:1)演示一个简单的动画效果,动态加载图片。2)点击屏幕时重新加载动画。

关键词:Animation 动画 动态加载图片

1、新建视图控制器ImageViewController(不带xib),作为根视图控制器,ImageViewController.h如下:

[cpp] 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ImageViewController : UIViewController  
  4.   
  5. @property(strong,nonatomic)UIImageView *imageView;  
  6. @property(strong,nonatomic)UIImage *desktop;  
  7. -(void)loadImage;  
  8. @end  

ImageViewController.m如下:

[cpp] 
  1. <span style="font-size:18px;">#import "ImageViewController.h"  
  2. @implementation ImageViewController  
  3. @synthesize imageView;  
  4. @synthesize desktop;  
  5.   
  6. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  7. {  
  8.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  9.     if (self) {  
  10.         // Custom initialization  
  11.     }  
  12.     return self;  
  13. }  
  14.   
  15. - (void)loadView  
  16. {  
  17.     // If you create your views manually, you MUST override this method and use it to create your views.  
  18.     // If you use Interface Builder to create your views, then you must NOT override this method.  
  19.     NSLog(@"loadView");  
  20.     UIView *view = [[ UIView alloc] initWithFrame:[ UIScreen  
  21.                                                    mainScreen].applicationFrame] ;  
  22.     UIColor *bgColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];  
  23.     [view setBackgroundColor:(bgColor)];  
  24.     self.view = view;  
  25.     desktop = [UIImage imageNamed:@"desktop.png"];  
  26.       
  27.     [self performSelector:@selector(loadImage) withObject:nil afterDelay:0.1];  
  28. }  
  29.   
  30. -(void)loadImage{  
  31.     if(imageView!=nil){  
  32.         [imageView removeFromSuperview];  
  33.     }  
  34.     //初始时,将imageView的宽度设置为0,这样就隐藏起来了  
  35.     imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, desktop.size.height)];  
  36.     [imageView setImage:desktop];  
  37.       
  38.     [self.view addSubview:imageView];  
  39.     //设置动画效果  
  40.     CGContextRef context = UIGraphicsGetCurrentContext();  
  41.     //开始播放动画  
  42.     [UIView beginAnimations:nil context:context];  
  43.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  44.     //[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];  
  45.     //[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];  
  46.     //[UIView setAnimationCurve:UIViewAnimationCurveLinear];  
  47.     [UIView setAnimationDuration:1.5];  
  48.     //imageView最终的宽度为desktop.size.width  
  49.     [imageView setFrame:CGRectMake(0, 0, desktop.size.width, desktop.size.height)];     
  50.     [UIView commitAnimations];  
  51. }  
  52.   
  53. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
  54.     [self loadImage];  
  55. }  
  56.   
  57. - (void)viewDidLoad  
  58. {  
  59.     [super viewDidLoad];  
  60.     // Do any additional setup after loading the view, typically from a nib.  
  61. }  
  62.   
  63. - (void)viewDidUnload  
  64. {  
  65.     [super viewDidUnload];  
  66.     // Release any retained subviews of the main view.  
  67.     imageView = nil;  
  68.     desktop = nil;  
  69. }  
  70.   
  71. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  72. {  
  73.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  74. }  
  75.   
  76. @end  
  77. </span>  
2、运行效果是图片从左至右慢慢显示出来,如下,

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8563722 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
下一篇:iPhone开发【十二】多视图技术总结之四:Segmented Control

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月06日 15时14分58秒