[swift] NotificationBanner 알림창 배경 동영상 전체화면 설정

NotificationBanner는 iOS 프로젝트에서 알림창을 표시하는 데 사용되는 인기 있는 라이브러리입니다. 이 라이브러리로 사용자에게 알림을 보여줄 때, 배경에 동영상을 삽입하여 좀 더 동적인 효과를 줄 수 있습니다. 이번에는 NotificationBanner 알림창의 배경에 동영상을 전체화면으로 설정하는 방법을 알아보겠습니다.

단계 1: AVPlayerViewController 추가하기

NotificationBanner의 배경으로 동영상을 전체화면으로 나타내기 위해 AVPlayerViewController를 사용할 것입니다. 먼저, 프로젝트에 AVFoundation 프레임워크를 추가해야 합니다.

  1. Xcode에서 프로젝트 네비게이터에서 프로젝트 파일을 선택합니다.
  2. 탭 바에서 “General” 탭으로 이동합니다.
  3. “Frameworks, Libraries, and Embedded Content” 섹션에서 “+” 버튼을 클릭합니다.
  4. “AVFoundation.framework”을 선택하고 “Add” 버튼을 클릭하여 추가합니다.

단계 2: NotificationBanner 커스터마이징

NotificationBanner의 배경에 동영상을 추가하기 위해 커스터마이징된 NotificationBanner를 만들어야 합니다. 다음은 NotificationBanner를 커스터마이징하는 예시 코드입니다.

import NotificationBannerSwift
import AVKit

class CustomNotificationBanner: BaseNotificationBanner {

    private var playerViewController: AVPlayerViewController?

    func showCustomBanner(view: UIView, videoURL: URL) {
        let banner = CustomNotificationBanner(customView: view)
        banner.bannerHeight = .statusBar
        banner.autoDismiss = false

        // AVPlayerViewController 생성
        playerViewController = AVPlayerViewController()
        playerViewController?.player = AVPlayer(url: videoURL)

        // 동영상 전체화면 설정
        playerViewController?.view.frame = UIScreen.main.bounds
        playerViewController?.showsPlaybackControls = false
        playerViewController?.videoGravity = .resizeAspectFill

        // NotificationBanner에 AVPlayerViewController 삽입
        banner.onDidAppear {
            if let playerViewController = self.playerViewController {
                UIApplication.shared.keyWindow?.rootViewController?.present(playerViewController, animated: true) {
                    playerViewController.player?.play()
                }
            }
        }

        banner.onDismiss {
            self.playerViewController?.player?.pause()
            self.playerViewController?.dismiss(animated: true, completion: nil)
        }

        banner.show()
    }
}

단계 3: 사용법

이제 작성한 CustomNotificationBanner 클래스로 NotificationBanner를 표시해보겠습니다. 다음은 사용 예시 코드입니다.

let customView = UIView() // 사용자 정의 뷰
let videoURL = URL(fileURLWithPath: "video.mp4") // 동영상 파일 경로

let banner = CustomNotificationBanner()
banner.showCustomBanner(view: customView, videoURL: videoURL)

위의 코드에서 customView는 NotificationBanner의 내용을 담은 사용자 정의 뷰이며, videoURL은 NotificationBanner 배경에 사용될 동영상 파일 경로입니다. 사용자 정의 뷰와 동영상 파일 경로를 설정한 후 showCustomBanner(view:videoURL:) 메소드를 호출하여 NotificationBanner를 표시합니다.


이제 NotificationBanner 알림창의 배경에 동영상을 전체화면으로 설정하는 방법에 대해 알아보았습니다. 사용자의 화려하고 동적인 알림 경험을 제공하기 위해 동영상 배경을 사용할 수 있습니다.

참고 자료: