簡體   English   中英

從Objective-C到Swift的完成模塊

[英]Completion Block from Objective-C to Swift

我正在嘗試翻譯位於Swift的viewWillAppear函數中的completeBlock:

KeyboardHelperCompletionBlock adjustCenterUp = ^(NSNotification *notification) {
    [UIView animateWithDuration:0.35f animations:^{
        self.popupVC.view.centerY = self.view.centerY - [KeyboardHelper keyboardHeight] / 2;
    }];
};

我有動畫部分:

UIView.animate(withDuration: 0.35, animations: {
            self.popupVC?.view.center.y = self.view.centerY() - KeyboardHelper.keyboardHeight()/2
        })

但不是AdjustCenterup定義。 謝謝!

只是:

let adjustCenterUp: KeyboardHelperCompletionBlock = { notification in
   UIView.animate(withDuration: 0.35) {
        self.popupVC?.view.center.y = self.view.centerY() - KeyboardHelper.keyboardHeight() / 2
   }
}

但您可能應該虛弱地捕捉self

let adjustCenterUp: KeyboardHelperCompletionBlock = { [weak self] _ in
   guard let `self` = self else { return }

   UIView.animate(withDuration: 0.35) {
        self.popupVC?.view.center.y = self.view.centerY() - KeyboardHelper.keyboardHeight() / 2
   }
}

暫無
暫無

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

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