iPhone开发动画效果之最简单的动画——动态加载图片 .

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


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

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


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

#import <UIKit/UIKit.h>

@interface ImageViewController : UIViewController

@property(strong,nonatomic)UIImageView *imageView;
@property(strong,nonatomic)UIImage *desktop;
-(void)loadImage;
@end


ImageViewController.m如下:

#import "ImageViewController.h"
@implementation ImageViewController
@synthesize imageView;
@synthesize desktop;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)loadView
{
// If you create your views manually, you MUST override this method and use it to create your views.
// If you use Interface Builder to create your views, then you must NOT override this method.
NSLog(@"loadView");
UIView *view = [[ UIView alloc] initWithFrame:[ UIScreen
mainScreen].applicationFrame] ;
UIColor *bgColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
[view setBackgroundColor:(bgColor)];
self.view = view;
desktop = [UIImage imageNamed:@"desktop.png"];

[self performSelector:@selector(loadImage) withObject:nil afterDelay:0.1];
}

-(void)loadImage{
if(imageView!=nil){
[imageView removeFromSuperview];
}
//初始时,将imageView的宽度设置为0,这样就隐藏起来了
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, desktop.size.height)];
[imageView setImage:desktop];

[self.view addSubview:imageView];
//设置动画效果
CGContextRef context = UIGraphicsGetCurrentContext();
//开始播放动画
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1.5];
//imageView最终的宽度为desktop.size.width
[imageView setFrame:CGRectMake(0, 0, desktop.size.width, desktop.size.height)];
[UIView commitAnimations];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self loadImage];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
imageView = nil;
desktop = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


2、运行效果是图片从左至右慢慢显示出来,如下,
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7347/8ba4b8ed-bae5-3b80-84de-5384dab5be23.png[/img]
[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值