你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS--UIImageView--幀動畫

IOS--UIImageView--幀動畫

編輯:IOS開發綜合

幀動畫

由於沒有學習gif的制作,所以只能截幾張圖來看一看咯<求mac上怎麼制作gif>
這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

play方法

加載所有動畫圖片 設置動畫圖片 設置播放次數 設置圖片 設置動畫時間 開始動畫 播放完畢後執行的方法
- (void)play:(NSString *)filenamePrefix count:(int)count
{
    // 加載所有的動畫圖片
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 1; i<=count; i++) {
        NSString *filename = [NSString stringWithFormat:@%@_%d, filenamePrefix, i];
        NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:@png];
        UIImage *image = [UIImage imageWithContentsOfFile:file];
        [images addObject:image];
    }

    // 設置動畫圖片
    self.imageView.animationImages = images;

    // 設置播放次數
    self.imageView.animationRepeatCount = [filenamePrefix isEqualToString:@stand] ? 0 :1;

    // 設置圖片
    self.imageView.image = [UIImage imageNamed:@stand_1];

    // 設置動畫的時間
    self.imageView.animationDuration = count * 0.04;

    // 開始動畫
    [self.imageView startAnimating];

    if ([filenamePrefix isEqualToString:@stand]) return;
    // 播放完動畫以後站著(延遲self.imageView.animationDuration時間後調用self的stand方法)
    [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
}

簡單加載音頻

步驟
導入音頻頭文件 創建音頻屬性 創建一個音頻文件的URL 創建播放器 開始播放
#import 
@property (strong, nonatomic) AVPlayer *player;
// 創建一個音頻文件的URL(URL就是文件路徑對象)
NSURL *url = [[NSBundle mainBundle] URLForResource:@dazhao withExtension:@mp3];
// 創建播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];

源碼

//
//  ViewController.m
//  拳皇幀動畫
//
//  Created by MacBookPro on 15/8/9.
//  Copyright © 2015年 sky5156. All rights reserved.
//

#import ViewController.h

#import 

@interface ViewController ()

//拿到storyboard中的imageView
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

//放點音樂
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加個邊框
    CALayer *layer = [self.imageView layer];
    layer.borderColor = [UIColor redColor].CGColor;
    layer.borderWidth = 5.0f;

    [self stand];
}
- (IBAction)stand {
    [self play:@stand count:10];
}

- (IBAction)dead {
    [self play:@dead count:23];
}

- (IBAction)run {
    [self play:@run count:6];
}

- (IBAction)xiaozhao {
    [self play:@xiaozhao1 count:20];

    [self playMusic:@xiaozhao1];
    [self performSelector:@selector(xiaozhao2) withObject:nil afterDelay:20 * 0.06];

   // [self play:@xiaozhao3 count:39];
}

- (IBAction)dazhao {
    [self play:@dazhao count:87];
    [self playMusic:@dazhao];
}

- (IBAction)install_b {
    [self play:@install_b count:29];
}


- (void)xiaozhao2
{
    [self play:@xiaozhao2 count:35];
    [self playMusic:@xiaozhao2];
    [self performSelector:@selector(xiaozhao3) withObject:nil afterDelay:35 * 0.06];
}

-(void)xiaozhao3
{
    [self play:@xiaozhao3 count:39];
    [self playMusic:@xiaozhao3];

}


- (void)playMusic:(NSString *)music
{

    NSString *urlString = [NSString stringWithFormat:@%@,music];
  //  NSString *urlName = [[NSBundle mainBundle] pathForResource:urlString ofType:@mp3];

    NSURL *url = [[NSBundle mainBundle] URLForResource:urlString withExtension:@mp3];
    self.player= [AVPlayer playerWithURL:url];
    [self.player play];
}


- (void)play:(NSString *)preFixName count:(int)count
{
    //加載動畫
    NSMutableArray *images = [NSMutableArray array];

    for (int i = 1; i<=count; i++) {

        NSString *fileName = [NSString stringWithFormat:@%@_%d,preFixName,i];

        NSString *name = [[NSBundle mainBundle] pathForResource:fileName ofType:@png];

        UIImage *image = [UIImage imageWithContentsOfFile:name];

        [images addObject:image];
    }
    //設置動畫的
    self.imageView.animationImages = images;

    self.imageView.image = [UIImage imageNamed:@stand_1];

    self.imageView.animationRepeatCount = [preFixName isEqualToString:@stand]?0 : 1;

    self.imageView.animationDuration = count*0.06;

    [self.imageView startAnimating];

    //播放完以後站著
    if ([preFixName isEqualToString:@stand])  return;

    [self performSelector:@selector(stand) withObject:nil afterDelay:count *0.06];
}
@end
 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved