完成从Objective-C到Swift的阻止

时间:2017-04-12 17:52:12

标签: objective-c swift

我正在尝试翻译位于Swift中viewWillAppear函数中的这个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
        })

但不是adjustCenterup定义。 谢谢!

1 个答案:

答案 0 :(得分:1)

简单地说:

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