YYImage
YYImage copied to clipboard
GIF只播放一次
能不能控制YYAnimatedImageView只播放一次某个GIF然后变为第一帧的图?
animatedImageLoopCount
?
you can also create an YYImage
subclass to set that loopCount to 1 😄
This is my extension for playing in special times.(in swift4)
extension YYImage {
public var loopCount: UInt? {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as? UInt
}
set {
if newValue != nil {
DispatchQueue.once(token: "YYImageCountSet", block: {
self.setSwizzlingMethod()
})
}
objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
public func setSwizzlingMethod() {
let originalSelector = #selector(animatedImageLoopCount)
let swizzledSelector = #selector(swizzled_animatedImageLoopCount)
let originalMethod = class_getInstanceMethod(YYImage.self, originalSelector)
let swizzledMethod = class_getInstanceMethod(YYImage.self, swizzledSelector)
guard let original = originalMethod else { return }
guard let swizzled = swizzledMethod else { return }
if class_addMethod(YYImage.self, originalSelector, method_getImplementation(swizzled), method_getTypeEncoding(swizzled)) {
class_replaceMethod(YYImage.self, swizzledSelector, method_getImplementation(original), method_getTypeEncoding(original))
} else {
method_exchangeImplementations(original, swizzled)
}
}
@objc func swizzled_animatedImageLoopCount() -> UInt {
guard let loop = loopCount else {
return 0
}
return loop
}
}