IOS UIImageView & 湯姆貓實例

一 UIImageView 簡介

UIImageView是顯示圖片的控件,顯示圖片時,首先須要把圖片加載到UIImage控件中,而後根據UIImage控件,設置到UIImageView相對應的屬性中數組


1 建立UIImage對象
//使用應用程序中圖片文件建立,IOS4後能夠省略圖片擴展名;
+ (UIImage *)imageNamed:(NSString *)name;


//根據指定的路徑建立UIImage
+ (UIImage *)imageWithContentsOfFile:(NSString *)path;
- (instancetype)initWithContentsOfFile:(NSString *)path;


//使用NSData建立
+ (UIImage *)imageWithData:(NSData *)data;
- (instancetype)initWithData:(NSData *)data;


//使用Quartz 2D對象建立UIImage
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage;
- (instancetype)initWithCGImage:(CGImageRef)cgImage;


注意:緩存

UIImage imageNamed 會自動緩存圖標,會佔用大量內存,推薦使用 IImage imageWithContentOfFile:(NSString *);動畫


2設置UIImage對象到UIImageView中
//單個圖片對象
@property(nonatomic,retain) UIImage *image;


//用數組存儲的多個圖片對象
@property(nonatomic,copy) NSArray *animationImages;


3 建立UIImageView
//建立image對象
UIImage *image = [UIImage imageNamed:@"imageName"];
UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"imageName" ofType:nil]]


//添加image對象到UIImageView中
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];


二 UIImageView 動畫功能

查看UIImageView頭文件的相關屬性atom

//設置圖片,animationImages 是NSArray數組中包含的UIImage
@property (nonatomic,copy) NSArray *animationImages;

//設置播放次數,不指定爲無限循環播放
@property (nonatomic)NSInteger animationRepeatCount;

//設置播放速度,動畫持續的時間
@property (nonatomic)animationDuration;

//啓動動畫
- (void)startAnimating;

//中止動畫
- (void)stopAnimating;

//判斷動畫是否在運行
- (BOOL)isAnimating;


注意spa

在動畫執行完畢後,須要清理緩存,把UIImageView賦值爲nilcode


三 湯姆貓代碼

@interface ViewController ()

/**
 *  屬性
 */
@property (weak, nonatomic) IBOutlet UIImageView *tomImageView;


/**
 *  喝牛奶
 */
-(IBAction)drink;
-(IBAction)cymbal;
-(IBAction)fart;
-(IBAction)pie;
-(IBAction)scratch;
-(IBAction)eat;

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/**
 *  喝牛奶
 */
-(IBAction)drink
{
    [self runAnimationImagesWithFilePrefix: @"drink" andFileCount:80];
}

-(IBAction)cymbal
{
    [self runAnimationImagesWithFilePrefix: @"cymbal" andFileCount:12];
}

-(IBAction)fart
{
    [self runAnimationImagesWithFilePrefix: @"fart" andFileCount:27];
}

-(IBAction)pie
{
    [self runAnimationImagesWithFilePrefix: @"pie" andFileCount:23];
}

-(IBAction)scratch
{
    [self runAnimationImagesWithFilePrefix: @"scratch" andFileCount:55];
}

-(IBAction)eat
{
    [self runAnimationImagesWithFilePrefix: @"eat" andFileCount:39];
}





/**
 *  播放動畫
 *
 *  @param filePrefix 動畫圖片前綴
 *  @param count      動畫圖片張數
 */

-(void)runAnimationImagesWithFilePrefix:(NSString*)filePrefix andFileCount:(NSInteger)count
{
    
    NSInteger imageTotal = count;
    //    1.在動畫播放時,不能點擊
    if([self.tomImageView isAnimating]){
        return;
    }
    
    //    2.保存圖片到數組
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:imageTotal];
    for (int i=0; i<=imageTotal; i++) {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",filePrefix,i];
        [images addObject:[UIImage imageWithContentsOfFile:[bundle pathForResource:imageName ofType:nil]]];
    }
    
    //    3.把數組賦值給tomImageView
    self.tomImageView.animationImages=images;
    
    
    //    4.播放動畫
    NSInteger delay = images.count*0.09;
    //    4.1設置播放次數
    self.tomImageView.animationRepeatCount=1;
    //    4.2設置播放持續時間
    self.tomImageView.animationDuration=delay;
    //    4.3開始播放
    [self.tomImageView startAnimating];
    
    //    5調用清空緩存
    [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}


/**
 *  清空緩存
 */
-(void)clearCache
{
    self.tomImageView.animationImages=nil;
}
@end
相關文章
相關標籤/搜索