序列幀:肉眼可以接受最高每秒20幀以內(nèi)的動(dòng)畫
屬性:
- 設(shè)置 animation 圖片
self.imageView.animationImages = self.array; - 設(shè)置播放時(shí)長
self.imageView.animationDuration = 0.1 * self.array.count; - 設(shè)置播放次數(shù)
self.imageView.animationRepeatCount = 1; - 開始播放動(dòng)畫
[self.imageView startAnimating]; - 停止播放動(dòng)畫
[self.imageView stopAnimating]; - 判斷imageView的狀態(tài)
if (self.imageView.isAnimating == NO) { }
一凛俱、OC 寫法
方法一:使用Assets 資源常駐內(nèi)存
//
// ViewController.m
// 7-序列幀動(dòng)畫
//
// Created by 千山我獨(dú)行 on 16/10/29.
// Copyright ? 2016年 千山我獨(dú)行. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
// moxing
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.設(shè)置最后一幀為imageView的圖片
self.imageView.image = [UIImage imageNamed:@"0017"];
// 2.獲取 image 數(shù)組
NSMutableArray *imgArray = [NSMutableArray array];
for (int i=0; i<18; i++) {
NSString *str = [NSString stringWithFormat:@"%04d",i];
UIImage *image = [UIImage imageNamed:str];
[imgArray addObject:image];
}
// 3.設(shè)置 animation 圖片數(shù)組
self.imageView.animationImages = imgArray;
// 設(shè)置播放時(shí)長
self.imageView.animationDuration = 0.1 * imgArray.count;
// 設(shè)置播放次數(shù)
self.imageView.animationRepeatCount = 1;
// 開始播放動(dòng)畫
[self.imageView startAnimating];
}
方法二:資源存儲(chǔ)在 Bundle中映挂,播放完成后,animationImages 指向 nil
//
// ViewController.m
// 7-序列幀動(dòng)畫
//
// Created by 千山我獨(dú)行 on 16/10/29.
// Copyright ? 2016年 千山我獨(dú)行. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
// moxing
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:@"0017"];
NSMutableArray * imgArray = [NSMutableArray array];
for (int i=0; i<18; i++) {
NSString *str = [NSString stringWithFormat:@"%04d",i];
// 1.將資源放到 bundle 中
NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:@".jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[imgArray addObject:image];
}
self.imageView.animationImages = imgArray;
self.imageView.animationDuration = 0.1 * imgArray.count;
self.imageView.animationRepeatCount = 1;
[self.imageView startAnimating];
// 2.動(dòng)畫結(jié)束后冶忱、調(diào)用釋放內(nèi)存(在動(dòng)畫執(zhí)行結(jié)束后殷蛇,調(diào)用 ImageView 的 animationImages set方法倔监,并指向nil批旺,來釋放內(nèi)存)
[self performSelector:@selector(releaseMem) withObject:nil afterDelay:0.1 * imgArray.count];
}
// 3.釋放內(nèi)存
- (void)releaseMem {
self.imageView.animationImages = nil;
}
代碼簡化:
// 2.動(dòng)畫結(jié)束后销睁、調(diào)用釋放內(nèi)存(在動(dòng)畫執(zhí)行結(jié)束后供璧,調(diào)用 ImageView 的 animationImages set方法,并指向nil冻记,來釋放內(nèi)存)
[self performSelector:@selector(releaseMem) withObject:nil afterDelay:0.1 * imgArray.count];
}
// 3.釋放內(nèi)存
- (void)releaseMem {
self.imageView.animationImages = nil;
}
替換為:
點(diǎn)語法調(diào)用的是 set 方法睡毒,所以可以簡化為 imageView 調(diào)用 performSelector 方法,并傳入 nil 參數(shù)
[self.imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:0.1 * imgArray.count];
二冗栗、Swift
//
// ViewController.swift
// 11-序列幀動(dòng)畫-Swift
//
// Created by 千山我獨(dú)行 on 16/10/30.
// Copyright ? 2016年 千山我獨(dú)行. All rights reserved.
//
import UIKit
let xCenter = UIScreen.mainScreen().bounds.origin.x / 2
class ViewController: UIViewController {
// MARK: - 懶加載
lazy var imageView: UIImageView = {
let frame = CGRect(x: 91, y: 80, width: 192, height: 284)
let imageView = UIImageView(frame: frame)
return imageView
}()
var btn = UIButton(type: UIButtonType.System)
// 動(dòng)畫資源數(shù)組
var dataArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
// 添加播放按鈕
btn.frame = CGRect(x: 50, y: 400, width: 275, height: 40)
btn.setTitle("播放動(dòng)畫", forState: UIControlState.Normal)
btn.addTarget(self, action: "playAnimation1DidClick", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(imageView)
view.addSubview(btn)
}
// MARK: - 定義方法
// private: 私有方法演顾、swift 比較嚴(yán)謹(jǐn)、button事件的調(diào)用是在編譯時(shí)已經(jīng)決定的贞瞒,但是事件的觸發(fā)來自于 runloop偶房,編譯器不會(huì)將它編譯進(jìn)來,只有運(yùn)行時(shí)來調(diào)用军浆,這是屬于 oc 的調(diào)用方式棕洋,所以要使用 @objc 修飾
@objc private func playAnimation1DidClick() {
// 獲取圖片路徑、存儲(chǔ)到資源數(shù)組中
for i in 0...17 {
let name = NSString(format: "%04d", i);
let path = NSBundle.mainBundle().pathForResource(name as String, ofType: ".jpg");
let image = UIImage(contentsOfFile: path!)
guard let img = image else {
print("nil")
return
}
dataArray.append(img)
}
// 播放動(dòng)畫
self.playAnimation()
// 動(dòng)畫結(jié)束后乒融、清空內(nèi)存中的資源
imageView.performSelector("animationImages", withObject: nil, afterDelay: 0.1 * 17)
}
@objc private func playAnimation() {
imageView.animationImages = dataArray
imageView.animationDuration = 0.1 * 17
imageView.animationRepeatCount = 1
imageView.startAnimating()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}