简体   繁体   中英

Completion Block from Objective-C to Swift

I am trying to translate this completionBlock located in the viewWillAppear function in Swift:

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

I have the animation part:

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

But not the adjustCenterup definition. Thank you!

Simply:

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

but you should probably capture self weakly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM