L.Vl
L.Vl

Reputation: 113

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!

Upvotes: 0

Views: 44

Answers (1)

Sulthan
Sulthan

Reputation: 130072

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

Upvotes: 1

Related Questions