从Objective-C到Swift的完井区块

从Objective-C到Swift的完井区块,objective-c,swift,Objective C,Swift,我正在尝试在Swift中转换ViewWillAspect函数中的此completionBlock: KeyboardHelperCompletionBlock adjustCenterUp = ^(NSNotification *notification) { [UIView animateWithDuration:0.35f animations:^{ self.popupVC.view.centerY = self.view.centerY - [KeyboardHe

我正在尝试在Swift中转换ViewWillAspect函数中的此completionBlock:

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
        })
但不是调整CenterUp定义。 谢谢大家!

简单地说:

let adjustCenterUp: KeyboardHelperCompletionBlock = { notification in
   UIView.animate(withDuration: 0.35) {
        self.popupVC?.view.center.y = self.view.centerY() - KeyboardHelper.keyboardHeight() / 2
   }
}
但您可能应该捕获
自我

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
   }
}