gpt4 book ai didi

swift - 如何显示 ASNetworkImageNode 的事件指示器?

转载 作者:行者123 更新时间:2023-11-30 12:31:06 24 4
gpt4 key购买 nike

这是我尝试过的代码,但是当图像尚未加载时,什么也没有显示(应该显示一个动画指示器)。显示事件指示器的最佳实践是什么?我连接到了 ASNetworkImageNodeDelegate。

import AsyncDisplayKit

class WideImageFeedNode : ASCellNode, ASNetworkImageNodeDelegate {


var imageNode = ASNetworkImageNode()
var activityIndicator:UIActivityIndicatorView?

init(itemid:Int) {
super.init()

imageNode.backgroundColor = ASDisplayNodeDefaultPlaceholderColor()
let imgURL = URL(string:"http://...somelargeimage.jpg")

imageNode.url = imgURL
imageNode.delegate = self

self.addSubnode(imageNode)

self.automaticallyManagesSubnodes = true



}

override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {


var finalStackArr:[ASLayoutElement] = [self.imageNode]


let finalSpec = ASStackLayoutSpec(direction: .vertical, spacing: 10.0, justifyContent: .start, alignItems: .start, children: finalStackArr)

return finalSpec

}

func imageNode(_ imageNode: ASNetworkImageNode, didLoad image: UIImage) {
if let activityIndicator = self.activityIndicator {
activityIndicator.removeFromSuperview()
self.activityIndicator = nil
}
self.setNeedsLayout()

}

// helper functions
func setupActivityIndicator(bounds:CGSize) -> UIActivityIndicatorView {
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

var refreshRect = activityIndicator.frame
refreshRect.origin = CGPoint(x: (bounds.width - activityIndicator.frame.size.width) / 2.0, y: (bounds.height - activityIndicator.frame.size.height) / 2.0)
activityIndicator.frame = refreshRect
return activityIndicator
}

func imageNodeDidStartFetchingData(_ imageNode: ASNetworkImageNode) {
self.activityIndicator = setupActivityIndicator(bounds: imageNode.style.preferredSize)
imageNode.view.addSubview(self.activityIndicator!)
}

func imageNode(_ imageNode: ASNetworkImageNode, didFailWithError error: Error) {
if let activityIndicator = self.activityIndicator {
activityIndicator.removeFromSuperview()
self.activityIndicator = nil
}
}



}

最佳答案

没关系,它有效。我错过了一个电话:

func imageNodeDidStartFetchingData(_ imageNode: ASNetworkImageNode) {
self.activityIndicator = setupActivityIndicator(bounds: imageNode.style.preferredSize)
imageNode.view.addSubview(self.activityIndicator!)
self.activityIndicator!.startAnimating()
}

关于swift - 如何显示 ASNetworkImageNode 的事件指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com