UILabel需要很长时间才能加载swift

时间:2016-08-01 19:02:44

标签: ios swift uilabel

我试图创建一个"匹配的视图"图像和"匹配价格"标记位于匹配图像的右上角。到目前为止一切正常 - 我在容器视图中创建了图像,我为我的价格标签的背景创建了一个UIImageView,但是当我创建实际标签并自定义它时,需要永远加载我的应用程序(即比如,一切都加载 - 匹配图像,价格标签背景图像,而不是详细说明价格的实际标签)。任何人都可以发现我的代码中哪些地方出错了吗?

func setupMiniContentScroll(contentScroll: UIScrollView) {
    let scalar:Double = 6/19
    let contentViewDimension = contentScroll.frame.width * CGFloat(scalar)
    let contentScrollWidth = CGFloat(LocalUser.matches.count) * (contentViewDimension + CGFloat(12)) - CGFloat(12)
    contentScroll.backgroundColor = UIColorFromHex(0x34495e)

    for index in 0..<LocalUser.matches.count {
        let match = LocalUser.matches[index]
        MatchesManager.globalManager.retrieveMatchThumbnail(match) { img, error in

            if let img = img {

                //create the mini matches views
                let xOrigin = index == 0 ? 12 : CGFloat(index) * contentViewDimension + (CGFloat(12) * CGFloat(index) + CGFloat(12))
                let contentFrame = CGRectMake(xOrigin, 10, contentViewDimension, contentViewDimension)
                let contentView = self.makeMiniContentView(contentFrame, image: img, matchedPrice: match.matchedPrice)
                contentView.match = match

                let tap = UITapGestureRecognizer(target: self, action: #selector(BrowseViewController.toggleItemInfo(_:)))
                contentView.addGestureRecognizer(tap)

                //update the contentScrollView
                dispatch_async(dispatch_get_main_queue()) {
                    contentScroll.addSubview(contentView)
                    contentScroll.contentSize = CGSizeMake(contentScrollWidth + CGFloat(16), contentScroll.frame.height)
                }
            }

        }
    }
}


//functions to create labels and imgViews for MiniMyMatches

func makeMiniContentView(frame: CGRect, image: UIImage, matchedPrice: Int) -> ItemContainer {

    let containerView = ItemContainer(frame: frame)

    //create the item image
    let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: containerView.frame.width, height: containerView.frame.height))
    imgView.image = image
    imgView.layer.cornerRadius = 5
    imgView.layer.masksToBounds = true
    imgView.userInteractionEnabled = true

    //create the price label
    dispatch_async(dispatch_get_main_queue()) {
        let priceLabel = self.makeMiniPriceLabel(containerView, matchedPrice: matchedPrice)
        containerView.addSubview(imgView)
        containerView.addSubview(priceLabel)
    }
    return containerView
}

func makeMiniPriceLabel(containerView: ItemContainer, matchedPrice: Int) -> UIView {
    //price label var

    let priceLabelFrame = CGRectMake(containerView.frame.size.width - 35, -7, containerView.frame.size.width * 0.50, containerView.frame.size.height * 0.35)

    //create the price container
    let priceContainer = UIImageView(frame: priceLabelFrame)
    priceContainer.image = UIImage(named: "venn.png")

    //create the price label
    let priceLabel = UILabel(frame: CGRect(x: 3, y:0, width: priceContainer.frame.width, height: priceContainer.frame.height))

    priceLabel.text = "$\(matchedPrice)"
    priceLabel.numberOfLines = 1
    priceLabel.textColor = UIColor.whiteColor()
    priceLabel.font = priceLabel.font.fontWithSize(20)
    priceContainer.addSubview(priceLabel)
    return priceContainer
}

1 个答案:

答案 0 :(得分:1)

我的猜测是你的retrieveMatchThumbnail函数的闭包是在后台线程上调用的。您在该闭包中有代码来操纵UI对象。我会将您调用的所有UI代码移到dispatch_async():

    MatchesManager.globalManager.retrieveMatchThumbnail(match) { img, error in

        if let img = img {

            //create the mini matches views
            let xOrigin = index == 0 ? 12 : CGFloat(index) * contentViewDimension + (CGFloat(12) * CGFloat(index) + CGFloat(12))
            let contentFrame = CGRectMake(xOrigin, 10, contentViewDimension, contentViewDimension)

            //update the contentScrollView
            dispatch_async(dispatch_get_main_queue()) {
              let contentView = self.makeMiniContentView(contentFrame, image: img, matchedPrice: match.matchedPrice)
            contentView.match = match

              let tap = UITapGestureRecognizer(target: self, action: #selector(BrowseViewController.toggleItemInfo(_:)))
            contentView.addGestureRecognizer(tap)

              contentScroll.addSubview(contentView)
              contentScroll.contentSize = CGSizeMake(contentScrollWidth + CGFloat(16), contentScroll.frame.height)
            }
        }

    }
相关问题