簡體   English   中英

Swift / iOS子視圖未立即顯示在主線程上

[英]Swift/iOS Subview Not Showing Immediately, on Main Thread

因此,我要在循環中向ViewController的視圖中添加幾個子視圖; 問題是,從我添加子視圖開始,它的顯示會延遲約5秒鍾。 添加后, 確保在子視圖和視圖上都調用setNeedsDisplay ,沒有骰子。 我什至使用NSThread.isMainThread()再次檢查了代碼是否在主線程上執行 ,並且按預期返回了true。 這是我的確切代碼:

    dispatch_async(dispatch_get_main_queue()) {
        var nib = UINib(nibName: "ProfileCard", bundle: NSBundle.mainBundle())
        var profiles = self.delegate.user!.profilesToShow

        //loop to add ProfileCard subviews
        while profiles.count > 0 && self.deck.count < self.MAX_DECK_SIZE {
            println("Adding Card")

            //setup subview
            var card = nib.instantiateWithOwner(self, options: nil).first! as ProfileCard
            card.delegate = self
            card.frame = CGRect(x: (self.view.bounds.width - 300)/2, y: 100, width: 300, height: 200)
            card.displayProfile(profiles.first!)

            //add subview
            self.view.addSubview(card)
            self.view.sendSubviewToBack(card)
            card.setNeedsDisplay()
            self.view.setNeedsDisplay()

            //add card to deck, mark as shown
            self.deck.append(card)
            profiles.removeAtIndex(0)
        }
    }

此循環中的所有打印語句在實際顯示子視圖之前大約運行5秒鍾,我不確定這是什么原因。 我什至在子視圖的init方法中設置了打印語句,甚至是按預期打印的語句。

代碼不能在后台線程上運行,但事實上,你必須要他到主線程建議有在后台線程上運行其他代碼。 該其他代碼是問題的根源。 它正在嘗試與后台線程上的接口通信。 反過來又使整個繪圖系統陷入困境。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM