標籤:
UIImageView:映像視圖控制項:
它是UIView的子類,因此也是視圖控制項,可以用來顯示映像。因為它具有幀動畫屬性和操作方法,因此可以用來製作動畫,其實動畫就是很短的時間內,執行顯示連續的很多張圖片,人肉眼無法處分,使人看起來彷彿映像在動似的。例如典型的執行個體:湯姆貓執行個體
@interface UIImageView : UIView {
@property(nonatomic,retain) UIImage *image; //映像
@property(nonatomic,retain) UIImage *highlightedImage ; //高亮映像
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; //視圖能否互動
@property(nonatomic,getter=isHighlighted) BOOL highlighted; //是否高亮
@property(nonatomic,copy) NSArray *animationImages; //幀動畫映像數組(全部的映像)
@property(nonatomic,copy) NSArray *highlightedAnimationImages ; //高亮的幀動畫映像數組 (全部的映像)
@property(nonatomic) NSTimeInterval animationDuration; //執行一次全程的幀動畫時間
@property(nonatomic) NSInteger animationRepeatCount; //幀動畫重複次數
@property (nonatomic, retain) UIColor *tintColor;//控制項顏色
}
- (instancetype)initWithImage:(UIImage *)image; //初始化
- (instancetype)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage;//初始化
- (void)startAnimating;//開始幀動畫
- (void)stopAnimating;//停止幀動畫
- (BOOL)isAnimating;//是否正在執行幀動畫
@end
執行個體如下:湯姆貓
只要映像素材充足,其實代碼很簡單,素材和代碼如下:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgviewCat;
//每一個按鈕都對應著自己的一個事件
- (IBAction)drinkBtnClicked;
- (IBAction)clawBtnClicked;
- (IBAction)birdBtnClicked;
- (IBAction)breadBtnClicked;
- (IBAction)CymbalBtnClicked;
- (IBAction)stomachBtnClicked;
- (IBAction)fartBtnClicked;
- (IBAction)knockoutBtnClicked;
- (IBAction)angryBtnClicked;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//喝牛奶
- (IBAction)drinkBtnClicked
{
[self action:@"drink" andNums:81];
}
//劃玻璃
- (IBAction)clawBtnClicked
{
[self action:@"scratch" andNums:56];
}
//吃小鳥
- (IBAction)birdBtnClicked
{
[self action:@"eat" andNums:40];
}
//附蛋糕
- (IBAction)breadBtnClicked
{
[self action:@"pie" andNums:24];
}
//敲鑔
- (IBAction)CymbalBtnClicked
{
[self action:@"cymbal" andNums:13];
}
//肚子痛
- (IBAction)stomachBtnClicked
{
[self action:@"stomach" andNums:34];
}
//放屁
- (IBAction)fartBtnClicked
{
[self action:@"fart" andNums:28];
}
//敲頭頭暈
- (IBAction)knockoutBtnClicked
{
[self action:@"knockout" andNums:81];
}
//打臉生氣
- (IBAction)angryBtnClicked
{
[self action:@"angry" andNums:26];
}
//所有的動作事件
-(void)action:(NSString *)actionName andNums:(NSInteger) num
{
//如果正在執行當前的動畫,對新觸發的事件不做處理,直到當前的動作執行結束為止
if(self.imgviewCat.isAnimating)
{
return;
}
//1、載入圖片資源到數組中
NSMutableArray *arrayM = [NSMutableArray array];
for(int i=0; i<num; i++)
{
NSString *catName = [NSString stringWithFormat:@"%@_%02d.jpg",actionName,i];
//通過imagNamed:這種方式載入圖片,載入好的圖片會一直儲存在記憶體中,不會釋放,這樣下次在使用的時候就不需要再載入了,提高了運行速度;但
是,缺點是由於緩衝的緣故,如果資源過大,那麼應用程式會一直佔用太多的記憶體,這就是緩衝。
//即使沒有強型別指標引用,對象在方法結束後也不會被銷毀,這就是緩衝造成的。
//UIImage *imgcat = [UIImage imageNamed:catName];
//解決辦法,換一種載入方式,只要沒有強型別指標引用,載入的圖片對象在方法執行完後就會被銷毀
NSString *path = [[NSBundle mainBundle] pathForResource:catName ofType:nil];
//不能再傳入圖片名稱,而是要傳入放在根目錄下的完整的圖片路徑,即絕對路徑
UIImage *imgcat = [UIImage imageWithContentsOfFile:path];
[arrayM addObject:imgcat];
}
//2、設定UIImageView(圖片框)的animationImages屬性,這個屬性包含的就是所有的幀動畫
self.imgviewCat.animationImages = arrayM;
//3、設定動畫期間
self.imgviewCat.animationDuration = self.imgviewCat.animationImages.count * 0.08;
//4、設定動畫重複次數
self.imgviewCat.animationRepeatCount = 1;
//5、開始動畫
[self.imgviewCat startAnimating];
//6.在動畫執行完後,再清空圖片集合,設定圖片框在調用performSelector方法時,讓其順延強制
[self.imgviewCat performSelector:@selector(setAnimationImages:) withObject:nil
afterDelay:self.imgviewCat.animationImages.count*0.1];
}
@end
iOS:UIImageView映像視圖控制項