Skip to content

Commit

Permalink
Fix SwiftUI hang
Browse files Browse the repository at this point in the history
  • Loading branch information
wtmoose committed Jan 22, 2024
1 parent e70fb07 commit eb9591a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
17 changes: 6 additions & 11 deletions SwiftMessages/KeyboardTrackingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,15 @@ open class KeyboardTrackingView: UIView {
}

private func animateKeyboardChange(change: Change, height: CGFloat, userInfo: [AnyHashable: Any]) {
self.heightConstraint.constant = height
if let durationNumber = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
let curveNumber = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber {
CATransaction.begin()
CATransaction.setCompletionBlock {
self.didChange(change: change, userInfo: userInfo)
self.delegate?.keyboardTrackingViewDidChange(change: change, userInfo: userInfo)
}
let curve = UIView.AnimationCurve(rawValue: curveNumber.intValue) ?? .easeInOut
let animation = UIViewPropertyAnimator(duration: durationNumber.doubleValue, curve: curve) {
if let durationNumber = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber {
UIView.animate(withDuration: durationNumber.doubleValue, delay: 0, options: .curveEaseInOut, animations: {
self.heightConstraint.constant = height
self.updateConstraintsIfNeeded()
self.superview?.layoutIfNeeded()
}) { completed in
self.didChange(change: change, userInfo: userInfo)
self.delegate?.keyboardTrackingViewDidChange(change: change, userInfo: userInfo)
}
animation.startAnimation()
}
}

Expand Down
15 changes: 14 additions & 1 deletion SwiftMessages/MessageHostingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

/// A rudimentary hosting view for SwiftUI messages.
@available(iOS 14.0, *)
public class MessageHostingView<Content>: BaseView, Identifiable where Content: View {
public class MessageHostingView<Content>: UIView, Identifiable where Content: View {

// MARK: - API

Expand Down Expand Up @@ -50,4 +50,17 @@ public class MessageHostingView<Content>: BaseView, Identifiable where Content:
if view == self || view?.superview == self { return nil }
return view
}

// MARK: - Configuration

private func installContentView(_ contentView: UIView) {
contentView.translatesAutoresizingMaskIntoConstraints = false
addSubview(contentView)
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
contentView.leftAnchor.constraint(equalTo: leftAnchor),
contentView.rightAnchor.constraint(equalTo: rightAnchor),
])
}
}

0 comments on commit eb9591a

Please sign in to comment.