Created
August 24, 2024 11:36
-
-
Save vurgunmert/edfc558ee58fd971dbd5c91fa0881685 to your computer and use it in GitHub Desktop.
UIKit tvOS Custom InApp Notification Storyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIKitCustomNotificationStoryboard.swift | |
// TemplateAppTvOS | |
// | |
// Created by Mert Vurgun on 24.08.2024. | |
// | |
import UIKit | |
class UIKitCustomNotificationStoryboard: UIViewController { | |
// MARK: 1 - View Lifecycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .black | |
setupButtons() | |
} | |
// MARK: 2 - Button Setup | |
private func setupButtons() { | |
let buttonTitles = ["Show Notification 1", "Show Notification 2", "Show Notification 3"] | |
let actions: [Selector] = [ | |
#selector(showNotification1), | |
#selector(showNotification2), | |
#selector(showNotification3) | |
] | |
let stackView = UIStackView() | |
stackView.axis = .vertical | |
stackView.alignment = .center | |
stackView.distribution = .equalSpacing | |
stackView.spacing = 20 | |
for (title, action) in zip(buttonTitles, actions) { | |
let button = createButton(title: title, action: action) | |
stackView.addArrangedSubview(button) | |
} | |
stackView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(stackView) | |
NSLayoutConstraint.activate([ | |
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor) | |
]) | |
} | |
private func createButton(title: String, action: Selector) -> UIButton { | |
let button = UIButton(type: .system) | |
button.setTitle(title, for: .normal) | |
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium) | |
button.addTarget(self, action: action, for: .primaryActionTriggered) | |
return button | |
} | |
// MARK: 3 - Notification Handlers | |
@objc private func showNotification1() { | |
showNotification(title: "Notification 1", message: "This is the first custom notification.", animationType: .topToBottom) | |
} | |
@objc private func showNotification2() { | |
showNotification(title: "Notification 2", message: "This is the second custom notification.", animationType: .rightToLeft) | |
} | |
@objc private func showNotification3() { | |
showNotification(title: "Notification 3", message: "This is the third custom notification.", animationType: .leftToRight) | |
} | |
// MARK: 4 - Custom In-App Notification | |
private enum AnimationType { | |
case topToBottom | |
case rightToLeft | |
case leftToRight | |
} | |
private func showNotification(title: String, message: String, animationType: AnimationType) { | |
let notificationView = UIView(frame: CGRect(x: 100, y: -100, width: view.frame.width-200, height: 100)) | |
notificationView.backgroundColor = UIColor.darkGray | |
notificationView.layer.cornerRadius = 10 | |
let titleLabel = UILabel(frame: CGRect(x: 20, y: 10, width: notificationView.frame.width - 40, height: 30)) | |
titleLabel.text = title | |
titleLabel.textColor = .white | |
titleLabel.font = UIFont.boldSystemFont(ofSize: 20) | |
let messageLabel = UILabel(frame: CGRect(x: 20, y: 40, width: notificationView.frame.width - 40, height: 50)) | |
messageLabel.text = message | |
messageLabel.textColor = .lightGray | |
messageLabel.font = UIFont.systemFont(ofSize: 16) | |
messageLabel.numberOfLines = 0 | |
notificationView.addSubview(titleLabel) | |
notificationView.addSubview(messageLabel) | |
view.addSubview(notificationView) | |
switch animationType { | |
case .topToBottom: | |
notificationView.frame.origin.y = -100 | |
UIView.animate(withDuration: 0.5, animations: { | |
notificationView.frame.origin.y = 50 | |
}) { _ in | |
UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: { | |
notificationView.frame.origin.y = -100 | |
}) { _ in | |
notificationView.removeFromSuperview() | |
} | |
} | |
case .rightToLeft: | |
notificationView.frame = CGRect(x: 1920, y: 50, width: 420, height: 100) | |
UIView.animate(withDuration: 0.5, animations: { | |
notificationView.frame.origin.x = 1460 | |
}) { _ in | |
UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: { | |
notificationView.frame.origin.x = 2500 | |
}) { _ in | |
notificationView.removeFromSuperview() | |
} | |
} | |
case .leftToRight: | |
notificationView.frame = CGRect(x: -1920, y: 50, width: view.frame.width - 200, height: 100) | |
UIView.animate(withDuration: 0.5, animations: { | |
notificationView.frame.origin.x = 100 | |
}) { _ in | |
UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: { | |
notificationView.frame.origin.x = 2500 | |
}) { _ in | |
notificationView.removeFromSuperview() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment