2016-08-01 93 views
0

我試圖在匹配圖像的右上角創建「匹配」圖像和「匹配價格」標籤的視圖。到目前爲止,一切工作正常 - 我在容器視圖中創建圖像,我創建了一個UIImageView作爲我的價格標籤的背景,但是當我創建實際標籤並對其進行自定義時,需要永久加載我的應用程序(即比如說,一切都會加載 - 匹配圖片,價格標籤背景圖片,但不包括詳細說明價格的實際標籤)。任何人都可以發現我的代碼中哪裏會出錯嗎?UILabel花費很長時間來加載swift

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

我的猜測是你的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) 
      } 
     } 

    } 
+0

一個很好的猜測,但不幸的是它沒有工作。 –