//
// ViewController.swift
// yywebimagetest
//
// Created by targetcloud on 2017/3/29.
// Copyright © 2017年 targetcloud. All rights reserved.
//
import UIKit
import YYWebImage
class ViewController: UIViewController {
@IBOutlet weak var imageView: YYAnimatedImageView!
override func viewDidLoad() {
super.viewDidLoad()
YYImageWebPAvailable() ? print("支持") : print("不支持")
imageView.yy_imageURL = URL(string: "http://file4.qf.56.itc.cn/style/static/gift/m/v2/webp/menghuanhunli.webp")
imageView.addObserver(self, forKeyPath: "currentAnimatedImageIndex", options: [.new], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let newValue = change![NSKeyValueChangeKey.newKey] as! UInt
newValue == 0 ? imageView.stopAnimating() : ()
}
}
注意:
1、imageView类型改为YYAnimatedImageView(sb、XIB)
2、加ATS
3、cocoapod上加入 pod 'YYImage/WebP',完整的podfile如下
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'yywebimagetest' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for yywebimagetest
pod 'YYImage/WebP'
pod 'YYWebImage'
end
4、 YYImageWebPAvailable () ? print ( " 支持 " ) : print ( " 不支持 " ) 用来先判断一下是否支持webp
5、若要GIF只播放一次用KVO方式实现,跟踪属性currentAnimatedImageIndex,第一次播放从1开始,接下来重复播放时从0开始,所以第二次从0开始就让动画停止