[swift] NotificationBanner 알림창 터치 시 슬라이드

앱 개발 중에 알림창을 사용하면 사용자에게 중요한 정보를 전달할 수 있습니다. NotificationBanner 라이브러리는 알림창을 쉽게 구현할 수 있는 훌륭한 도구 입니다. 이번에는 NotificationBanner 알림창을 터치하고 슬라이드 되는 기능을 추가하는 방법에 대해 알아보겠습니다.

NotificationBanner 설치하기

먼저 NotificationBanner를 설치해야 합니다. CocoaPods를 사용하는 경우, Podfile에 다음과 같이 추가합니다.

pod 'NotificationBannerSwift'

터미널에서 pod install 명령을 실행하여 라이브러리를 설치합니다.

NotificationBanner 생성하기

NotificationBanner를 사용하기 위해 import 문을 추가합니다.

import NotificationBannerSwift

알림창을 생성하고 터치 시 슬라이드 되도록 하려면 NotificationBannerQueue를 사용해야 합니다.

let banner = FloatingNotificationBanner(title: "알림", subtitle: "알림 내용입니다.", style: .info)
banner.autoDismiss = false
banner.show(queuePosition: .front)

NotificationBanner 터치 핸들링하기

알림창을 터치했을 때 어떤 동작을 하도록 하기 위해서는 NotificationBannerDelegate 프로토콜을 준수해야 합니다.

첫째, 해당 클래스에서 NotificationBannerDelegate 프로토콜을 채택합니다.

class ViewController: UIViewController, NotificationBannerDelegate {

둘째, NotificationBanner 객체를 만들 때에 delegate를 설정합니다.

banner.delegate = self

셋째, notificationBannerWasTapped(_ banner: BaseNotificationBanner) 메서드를 구현합니다. 이 메서드는 알림창이 터치되었을 때 호출됩니다.

func notificationBannerWasTapped(_ banner: BaseNotificationBanner) {
    // 슬라이드 동작을 처리하는 코드를 작성합니다.
}

알림창이 터치되었을 때 수행해야 할 동작을 작성합니다.

NotificationBanner 슬라이드 동작 구현하기

알림창을 터치하고 슬라이드 동작을 구현하려면, DelayedNotificationBanner를 사용해야 합니다. 해당 클래스는 NotificationBanner의 서브클래스로써 일정 시간이 지난 후에 알림창이 자동으로 사라지도록 동작하는 기능을 제공합니다.

let banner = DelayedNotificationBanner(title: "알림", subtitle: "알림 내용입니다.", style: .info)
banner.autoDismiss = false
banner.show(queuePosition: .front)

알림창이 터치되면 슬라이드 동작을 하도록 하려면 notificationBannerWillDisappear(_ banner: BaseNotificationBanner) 메서드를 구현합니다.

func notificationBannerWillDisappear(_ banner: BaseNotificationBanner) {
    banner.dismiss()
}

notificationBannerWillDisappear 메서드에서 banner.dismiss()를 호출하여 알림창을 슬라이드 아웃 시킵니다.

마무리

이제 NotificationBanner 알림창을 터치하고 슬라이드 되는 기능을 구현할 수 있게 되었습니다. 이 기능을 적용해서 사용자에게 더 좋은 경험을 제공해보세요.

참고자료