三、圖像移動《蘋果iOS實例編程入門教程》

該app爲應用的功能爲動態移動動畫

現版本 SDK 8.4 Xcode

運行Xcode 選擇 Create a new Xcode project ->Single View Application 命名 movingImage

(1)  在xCode打開 ViewController.m 文件

(紅色爲所添加的代碼)

#import "ViewController.h" 

@interface ViewController () 

@end 

@implementation ViewController 

- (void)viewDidLoad {

    //[super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

 

    //定義圖片的位置和尺寸,位置:x=10.0f, y=10.0f ,尺寸:x=50.0f, y=40.0f

    UIImageView *subView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 50.0f, 40.0f)];

    

    //設定圖片名稱,lovely mouse.png已經存在,拖放添加圖片文件到項目Supporting Files文件夾中

    [subView setImage:[UIImage imageNamed:@"lovely mouse.png"]];

    //啓用動畫移動

    [UIImageView beginAnimations:nil context:NULL];

    //移動時間4秒

    [UIImageView setAnimationDuration:4];

    //圖片持續移動

    [UIImageView setAnimationBeginsFromCurrentState:YES];

   //重新定義圖片的位置和尺寸,位置 

    subView.frame = CGRectMake(100.0, 150.0, 200.0, 160.0);

    //完成動畫移動

    [UIImageView commitAnimations];

    //在 View 中加入圖片 subview 

    [self.view addSubview:subView];    

} 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

} 

@end

(2)模擬器效果圖

   

本文源於網上博客教程,經過本人修改和測試。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100dvx9.html