我正在创建自定义节点,以使用播放和暂停按钮显示gif文件。 以下是我的代码。
import Foundation
import AsyncDisplayKit
class GifNode: ASCellNode {
var gifImageNode:ASNetworkImageNode = {
let node = ASNetworkImageNode()
node.contentMode = .scaleAspectFit
node.shouldRenderProgressImages = true
return node
}()
var playImage: ASImageNode = {
let node = ASImageNode()
node.contentMode = .scaleAspectFit
node.style.height = ASDimensionMakeWithPoints(30)
node.style.height = ASDimensionMakeWithPoints(30)
node.backgroundColor = .gray
node.image = #imageLiteral(resourceName: "play")
return node
}()
init(model:GifContent)
{
super.init()
self.automaticallyManagesSubnodes = true
let width = UIScreen.main.bounds.size.width
let height = (width * model.height) / model.width
gifImageNode.url = URL(string: "https://i.pinimg.com/originals/07/44/38/074438e7c75034df2dcf37ba1057803e.gif")
gifImageNode.style.width = ASDimensionMake(width)
gifImageNode.style.height = ASDimensionMake(height)
gifImageNode.animatedImagePaused = true
gifImageNode.addTarget(self, action: #selector(self.toggleGifPlay), forControlEvents: .touchUpInside)
}
@objc func toggleGifPlay()
{
self.gifImageNode.animatedImagePaused = !self.gifImageNode.animatedImagePaused
self.playImage.isHidden = !self.gifImageNode.animatedImagePaused
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
let playButtonCenterSpec = ASCenterLayoutSpec(centeringOptions: .XY, sizingOptions: .minimumXY, child: self.playImage)
return ASOverlayLayoutSpec(child: gifImageNode, overlay: playButtonCenterSpec)
}
}
当我尝试在另一个ASCellNode中使用gif节点时,它不会呈现。我可以看到播放按钮,但看不到实际的gif文件。如果我尝试使用相同的控件加载jpeg,效果很好。如果我尝试直接加载gif而不使用上面的类,它也可以正常工作。
不确定我是否缺少任何物品。
答案 0 :(得分:0)
我测试了您的应用,但是默认情况下,您的动画禁用了gifImageNode.animatedImagePaused = true
。