From 430a3e11b73a66f1cfeb89e9651d4bca14e75b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Aslan?= Date: Mon, 28 Mar 2016 12:48:24 +0300 Subject: [PATCH] Added touch handler based notification --- Classes/NotificationView.swift | 34 ++++++++++++++++++++++++++++------ README.md | 11 ++++++++++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Classes/NotificationView.swift b/Classes/NotificationView.swift index 8dc3c8f..b63667c 100644 --- a/Classes/NotificationView.swift +++ b/Classes/NotificationView.swift @@ -9,7 +9,7 @@ import Foundation import UIKit -class NotificationView : UIView { +class NotificationView : UIView{ let bundle = NSBundle(identifier: "com.omer.OEANotification") var notificationTimer: NSTimer! var title: String? @@ -23,20 +23,27 @@ class NotificationView : UIView { init(frame: CGRect, title: String, subTitle: String?, type: NotificationType?, isDismissable: Bool) { super.init(frame: frame) - self.initVariables(title, subTitle: subTitle, image: nil, type: type, completionHandler: {}, touchHandler: {}, isDismissable: isDismissable) + self.initVariables(title, subTitle: subTitle, image: nil, type: type, completionHandler: nil, touchHandler: nil, isDismissable: isDismissable) self.setupNotification() } init(frame: CGRect, title: String, subTitle: String?, image: UIImage?, type: NotificationType?, isDismissable: Bool) { super.init(frame: frame) - self.initVariables(title, subTitle: subTitle, image: image, type: type, completionHandler: {}, touchHandler: {}, isDismissable: isDismissable) + self.initVariables(title, subTitle: subTitle, image: image, type: type, completionHandler: nil, touchHandler: nil, isDismissable: isDismissable) self.setupNotification() } init(frame: CGRect, title: String, subTitle: String?, image: UIImage?, type: NotificationType?, completionHandler: (() -> Void)?, isDismissable: Bool) { super.init(frame: frame) - self.initVariables(title, subTitle: subTitle, image: image, type: type, completionHandler: completionHandler, touchHandler: {}, isDismissable: isDismissable) + self.initVariables(title, subTitle: subTitle, image: image, type: type, completionHandler: completionHandler, touchHandler: nil, isDismissable: isDismissable) + self.setupNotification() + } + + init(frame: CGRect, title: String, subTitle: String?, image: UIImage?, type: NotificationType?, completionHandler: (() -> Void)?, touchHandler: (() -> Void)?, isDismissable: Bool) { + super.init(frame: frame) + + self.initVariables(title, subTitle: subTitle, image: image, type: type, completionHandler: completionHandler, touchHandler: touchHandler, isDismissable: isDismissable) self.setupNotification() } @@ -52,6 +59,7 @@ class NotificationView : UIView { } private func setupNotification() { + self.createBackground() if self.isDismissable { constants.nvPaddingRight += constants.nvdWidth @@ -70,9 +78,22 @@ class NotificationView : UIView { if self.isDismissable { constants.nvPaddingRight -= constants.nvdPaddingLeft } + + if self.touchHandler != nil { + self.createTouchEvent() + } self.addNotificationView() } + private func createTouchEvent() { + let tap = UITapGestureRecognizer(target: self, action: "handleTap:") + self.addGestureRecognizer(tap) + } + + func handleTap(recognizer: UITapGestureRecognizer) { + self.touchHandler!() + } + private func addNotificationView() { NotificationView.animateWithDuration(constants.nvaTimer, animations: { () -> Void in self.frame.origin.y = self.constants.nvMarginTop @@ -83,6 +104,7 @@ class NotificationView : UIView { self.completionHandler!() } } + } self.addTimer(constants.nvaShownTimer) @@ -161,15 +183,15 @@ class NotificationView : UIView { func close() { self.notificationTimer.invalidate() + NotificationView.animateWithDuration(constants.nvaTimer, animations: { () -> Void in - self.frame.origin.y = self.constants.nvStartYPoint + self.frame.origin.y = self.constants.nvStartYPoint }) { (Bool) -> Void in if OEANotification.notificationCount == 1 { UIApplication.sharedApplication().delegate?.window??.windowLevel = UIWindowLevelNormal OEANotification.removeOldNotifications() } } - } required init?(coder aDecoder: NSCoder) { diff --git a/README.md b/README.md index 0d494f7..04b3899 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,15 @@ class ViewController: UIViewController { //OEANotification.notify("Test Title", subTitle: "Test SubTitle", image: nil, type: NotificationType.Info, isDismissable: true) // to create warning notification //OEANotification.notify("Test Title", subTitle: "Test SubTitle", image: nil, type: NotificationType.Warning, isDismissable: true) + // to create completion handler based notification + OEANotification.notify("Test Title", subTitle: "Test Subtitle", image: nil, type: .Success, isDismissable: true, completion: { () -> Void in + print("completed") + }, touchHandler: nil) + // to create touchHandler based notificaiton + OEANotification.notify("Test Title", subTitle: "Test Subtitle", image: nil, type: .Success, isDismissable: true, completion: { () -> Void in + print("completed") + }) { () -> Void in + print("touched event") } } ``` @@ -45,7 +54,7 @@ pod "OEANotification" - [ ] Custom Notification Type - [X] Device rotation handling - [X] Completion Handler based notification -- [ ] View Tapped Handler based notification +- [X] View Tapped Handler based notification - [ ] Creating big example of OEANotification - [ ] UI Tests - [ ] Setup Travis