2017-08-08 67 views
1

我對swift和Objective C相當陌生,而且我有一個從主視圖控制器到按鈕按下的第二個視圖控制器的應用程序。第二個視圖控制器打開一個攝像頭,每次點擊按鈕時,攝像頭開啓都會有一定的滯後。我認爲這與線程有關,以及進程是如何處理的。iOS當按下按鈕時,希望延遲視圖控制器之間的延遲

我想介紹一下當我的第一個視圖控制器上的按鈕被放置和第二個viewcontroller被顯示在屏幕上之間的延遲。有沒有延遲可以用這種方式來應用?

下面是代碼與按鈕我的主視圖控制器:

import UIKit 

class ViewController: UIViewController { 

    @IBAction func itemAction(_ sender: AnyObject) { 
     performSegue(withIdentifier: "segue", sender: self) 
    } 

    @IBAction func logosAction(_ sender: Any) { 
     performSegue(withIdentifier: "segue2", sender: self) 
    } 

    @IBOutlet var itemsButton: UIButton! 

    @IBOutlet var carsButton: UIButton! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     itemsButton.layer.cornerRadius = 10 
     itemsButton.clipsToBounds = true 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 

謝謝!

+0

我想你也許應該尋找固定的延遲。你能告訴我們你是如何添加攝像機層的嗎?我已經在viewDidAppear中添加了它,並且工作正常 – GoodSp33d

+0

我正在使用第一個屏幕來連接到此處的此開放源代碼。相機初始化發生在這裏的視圖控制器: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/ios/camera/CameraExampleViewController.mm – Veejay

+0

請參閱:https://developer.apple。 com/documentation/objectivec/nsobject#// apple_ref/occ/instm/NSObject/performSelector:withObject:afterDelay: –

回答

3

無論何時用戶performSegue方法則需要這樣的

使用在該例子中,用10秒的延遲

DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: { 
    self.performSegue(withIdentifier: "segue", sender: self) 
}) 

更新

在這種例如5秒延遲

DispatchQueue.main.asyncAfter(deadline:.now() + 5.0, execute: { 
    self.performSegue(withIdentifier:"segue",sender: self) 
}) 

希望這會幫助你的感謝。

+0

謝謝你的回答,不幸的是,在我的代碼中添加這個並不會導致10秒的延遲,我必須做錯事。我有如下代碼,並在按下項目按鈕後預計會有10秒的延遲。 @IBAction FUNC itemAction(_發件人:AnyObject){ DispatchQueue.main.asyncAfter(截止日期:。現在()+ 5.0,執行:{ self.performSegue(withIdentifier: 「SEGUE」,發送方:自) } ) } 我在這裏錯過了什麼嗎? – Veejay

+0

我不認爲你錯過了那件事。我可能會因爲「清潔代碼」或「項目」而發生。但是,如果它工作的很好,那麼請投票回答我的問題,以便有人能夠幫助解決這個問題。我還在你的代碼中添加了代碼更新 謝謝 –

+2

@Viv你可能會在故事板中爲按鈕設置segue。從視圖控制器添加人工賽格瑞這種延遲工作 – GoodSp33d

0
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { 
    self.performSegue(withIdentifier: "segue", sender: self) 
} 

OR

let timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false) { (timer) in 
    self.performSegue(withIdentifier: "segue", sender: self) 
} 
+0

謝謝!我試着按照下面的方式實現這一點,並且在按鈕按下之後沒有任何延遲。我肯定是做了一些不正確的事情,不知道是什麼: @IBAction func itemAction(_ sender:AnyObject){ let timer = Timer.scheduledTimer(withTimeInterval:10.0,repeats:false){(timer)in self。 performSegue(withIdentifier: 「SEGUE」,發件人:個體經營) }} – Veejay

+0

@Viv以下鏈接可能對你有用https://stackoverflow.com/a/39845573 –