本文實例為大家分享了IOS使用UIImageView控件制作動畫的方法,供大家參考,具體內容如下
先添加40張tomcat的圖片到資源列表中:名稱為cat_eat0000.jpg到cat_eat0039.jpg。
1、定義所需控件
// 定義按鈕,圖片控件、可變數組對象 UIButton *actionbuttom; UIImageView *imageMove; NSMutableArray *imgsarray;
2、初始化各控件
// image動畫// 初始化UIImageView,大小和View的大小相同 imageMove = [[UIImageView alloc]initWithFrame:self.view.frame];// 設置UIImageView的初始化圖片 imageMove.image = [UIImage imageNamed:@"cat_eat0000.jpg"];// 把UIImageView加載到頁面 [self.view addSubview:imageMove];// 設置UIImageView的交互性為yes imageMove.userInteractionEnabled = YES; // 創建功能按鈕// 初始化按鈕 actionbuttom = [[UIButton alloc]initWithFrame:CGRectMake(100, 680, 218, 50)];// 設置按鈕背景色 actionbuttom.backgroundColor = [UIColor yellowColor];// 設置按鈕標題 [actionbuttom setTitle:@"開始播放" forState:UIControlStateNormal];// 設置按鈕文字顏色 [actionbuttom setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// 為按鈕添加觸發事件 [actionbuttom addTarget:self action:@selector(startmove:) forControlEvents:UIControlEventTouchUpInside];// 把按鈕添加到頁面中 [imageMove addSubview:actionbuttom]; // 初始化可變數組,用來存放圖片 imgsarray = [[NSMutableArray alloc]initWithCapacity:40];// 循環從資源中拿到四十張圖片,并添加到imgsarray。 for (int x=0; x<40; x++) { NSString *imgname = [NSString stringWithFormat:@"cat_eat00%.2d.jpg",x]; UIImage *img = [UIImage imageNamed:imgname]; [imgsarray addObject:img];
3、設置按鈕觸發動畫播放
//按鈕的觸發事件-(void)startmove:(id)sender{// 設置動畫時長 imageMove.animationDuration = 2;// 設置動畫圖片來源為圖片數組 imageMove.animationImages = imgsarray;// 設置動畫重復次數,0是無限循環,1為重復1次 imageMove.animationRepeatCount = 1;// 開始播放 [imageMove startAnimating]; }
以上就是本文的全部內容,希望對大家學習使用UIImageView控件制作動畫有所幫助。
新聞熱點
疑難解答