天天看點

iOS學習筆記-011.UIImageView的基本介紹和幀動畫UIImageView的基本介紹和幀動畫

  • UIImageView的基本介紹和幀動畫
    • 一執行個體化UIImageView
      • - initWithImage
      • - initWithImage highlightedImage
    • 二設定圖像
    • 三設定顯示模式
    • 四序列幀動畫基礎
      • 屬性說明
      • 相關方法
    • 五幀動畫的代碼
    • 六幀動畫的效果

UIImageView的基本介紹和幀動畫

一、執行個體化UIImageView

建立一個UIImageView可以使用以下的方法

1. - initWithImage:

// swift
init(image image:UIImage?)
           
// objective-c
- (instancetype nonnull)initWithImage:(UIImage * nullable)image
           

2.- initWithImage: highlightedImage:

// swift
init(image image: UIImage?,highlightedImage highlightedImage: UIImage?)
           
// objective-c
- (instancetype nonnull)initWithImage:(UIImage * nullable)image
                     highlightedImage:(UIImage * nullable)highlightedImage
           

二、設定圖像

[imageView setImage:[UIImage imageNamed:@"xm.png"]];
           

三、設定顯示模式

四、序列幀動畫基礎

UIImageView可以讓一系列的圖檔在特定的時間内按順序顯示

1. 屬性說明:

animationImages:要顯示的一組圖檔序列
animationDuration:完整地顯示所有圖檔所需的時間
animationRepeatCount:動畫的執行次數(預設為0,代表無限循環)
           

2. 相關方法:

- (void)startAnimating; 開始動畫
- (void)stopAnimating;  停止動畫
- (BOOL)isAnimating;  是否正在運作動畫
           

五、幀動畫的代碼

//
//  ViewController.m
//  03_UIView03_幀動畫
//
//  Created by 杞文明 on 15/12/22.
//  Copyright © 2015年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *testIv;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setXmAnimotion: withImageView:_testIv];
}

//設定動畫
-(void)setXmAnimotion:(int) count withImageView:(UIImageView*) imageView{
    //步驟
    //1.建立一個集合存儲圖檔
    NSMutableArray *imageList = [NSMutableArray array];
    //2.循環添加圖檔
    for (NSInteger i=; i<; i++) {
        [imageList addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",i]]];
    }

    //3.把圖檔集合添加到imageview中
    [imageView setAnimationImages:imageList];
    //4.動畫時長
    [imageView setAnimationDuration:];
    //5.循環次數
    [imageView setAnimationRepeatCount:];
    //6.開始動畫
    [imageView startAnimating];

}

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

@end

           

六、幀動畫的效果

iOS學習筆記-011.UIImageView的基本介紹和幀動畫UIImageView的基本介紹和幀動畫