diff --git a/Example/SCLAlertViewExample/ViewController.swift b/Example/SCLAlertViewExample/ViewController.swift index 045a9d4..1c0bc18 100644 --- a/Example/SCLAlertViewExample/ViewController.swift +++ b/Example/SCLAlertViewExample/ViewController.swift @@ -34,7 +34,7 @@ class ViewController: UIViewController { @IBAction func showSuccess(sender: AnyObject) { let alert = SCLAlertView() - alert.addButton("First Button", target:self, selector:Selector("firstButton")) + alert.addButton("First Button", target:self, selector:#selector(ViewController.firstButton)) alert.addButton("Second Button") { print("Second button tapped") } diff --git a/SCLAlertView.xcodeproj/project.pbxproj b/SCLAlertView.xcodeproj/project.pbxproj index efa9de2..536406f 100644 --- a/SCLAlertView.xcodeproj/project.pbxproj +++ b/SCLAlertView.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 9E124F1B1C9EA303001A4972 /* SCLAlertViewInitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E124F1A1C9EA303001A4972 /* SCLAlertViewInitTests.swift */; }; 9E124F1D1C9EA6B5001A4972 /* SCLTextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E124F1C1C9EA6B5001A4972 /* SCLTextFieldTests.swift */; }; 9E47417F1CA1DABE00F95B05 /* SCLButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E47417E1CA1DABE00F95B05 /* SCLButtonTests.swift */; }; + 9EAEFBC51CC2AA6600BA87FB /* SCLExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */; }; 9EE073151C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE073131C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift */; }; 9EE073161C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE073141C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift */; }; 9EE073171C9D7F6B002B43FD /* SCLAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70264F171B0F592500B32B18 /* SCLAlertView.swift */; }; @@ -41,6 +42,7 @@ 9E124F1A1C9EA303001A4972 /* SCLAlertViewInitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewInitTests.swift; sourceTree = ""; }; 9E124F1C1C9EA6B5001A4972 /* SCLTextFieldTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLTextFieldTests.swift; sourceTree = ""; }; 9E47417E1CA1DABE00F95B05 /* SCLButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLButtonTests.swift; sourceTree = ""; }; + 9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLExtensions.swift; sourceTree = ""; }; 9EE073131C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewPropertiesTests.swift; sourceTree = ""; }; 9EE073141C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewStyleTests.swift; sourceTree = ""; }; 9EE99F901CA6B2320090F845 /* SCLPublicConstructorsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLPublicConstructorsTest.swift; sourceTree = ""; }; @@ -91,6 +93,7 @@ isa = PBXGroup; children = ( 70264F171B0F592500B32B18 /* SCLAlertView.swift */, + 9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */, 70264EFA1B0F588700B32B18 /* Supporting Files */, ); path = SCLAlertView; @@ -234,6 +237,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9EAEFBC51CC2AA6600BA87FB /* SCLExtensions.swift in Sources */, 70264F181B0F592500B32B18 /* SCLAlertView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SCLAlertView/SCLAlertView.swift b/SCLAlertView/SCLAlertView.swift index e8cc763..12fb562 100644 --- a/SCLAlertView/SCLAlertView.swift +++ b/SCLAlertView/SCLAlertView.swift @@ -179,12 +179,12 @@ public class SCLAlertView: UIViewController { viewText.textContainer.lineFragmentPadding = 0; viewText.font = UIFont(name: kDefaultFont, size:14) // Colours - contentView.backgroundColor = UIColorFromRGB(0xFFFFFF) - labelTitle.textColor = UIColorFromRGB(0x4D4D4D) - viewText.textColor = UIColorFromRGB(0x4D4D4D) - contentView.layer.borderColor = UIColorFromRGB(0xCCCCCC).CGColor + contentView.backgroundColor = 0xFFFFFF.toUIColor() + labelTitle.textColor = 0x4D4D4D.toUIColor() + viewText.textColor = 0x4D4D4D.toUIColor() + contentView.layer.borderColor = 0xCCCCCC.toCGColor() //Gesture Recognizer for tapping outside the textinput - let tapGesture = UITapGestureRecognizer(target: self, action: Selector("tapped:")) + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SCLAlertView.tapped(_:))) tapGesture.numberOfTapsRequired = 1 self.view.addGestureRecognizer(tapGesture) } @@ -255,8 +255,8 @@ public class SCLAlertView: UIViewController { override public func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) - NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); - NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); + NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SCLAlertView.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil); + NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SCLAlertView.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil); } override public func viewDidDisappear(animated: Bool) { @@ -294,9 +294,9 @@ public class SCLAlertView: UIViewController { let btn = addButton(title) btn.actionType = SCLActionType.Closure btn.action = action - btn.addTarget(self, action:Selector("buttonTapped:"), forControlEvents:.TouchUpInside) - btn.addTarget(self, action:Selector("buttonTapDown:"), forControlEvents:[.TouchDown, .TouchDragEnter]) - btn.addTarget(self, action:Selector("buttonRelease:"), forControlEvents:[.TouchUpInside, .TouchUpOutside, .TouchCancel, .TouchDragOutside] ) + btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), forControlEvents:.TouchUpInside) + btn.addTarget(self, action:#selector(SCLAlertView.buttonTapDown(_:)), forControlEvents:[.TouchDown, .TouchDragEnter]) + btn.addTarget(self, action:#selector(SCLAlertView.buttonRelease(_:)), forControlEvents:[.TouchUpInside, .TouchUpOutside, .TouchCancel, .TouchDragOutside] ) return btn } @@ -305,9 +305,9 @@ public class SCLAlertView: UIViewController { btn.actionType = SCLActionType.Selector btn.target = target btn.selector = selector - btn.addTarget(self, action:Selector("buttonTapped:"), forControlEvents:.TouchUpInside) - btn.addTarget(self, action:Selector("buttonTapDown:"), forControlEvents:[.TouchDown, .TouchDragEnter]) - btn.addTarget(self, action:Selector("buttonRelease:"), forControlEvents:[.TouchUpInside, .TouchUpOutside, .TouchCancel, .TouchDragOutside] ) + btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), forControlEvents:.TouchUpInside) + btn.addTarget(self, action:#selector(SCLAlertView.buttonTapDown(_:)), forControlEvents:[.TouchDown, .TouchDragEnter]) + btn.addTarget(self, action:#selector(SCLAlertView.buttonRelease(_:)), forControlEvents:[.TouchUpInside, .TouchUpOutside, .TouchCancel, .TouchDragOutside] ) return btn } @@ -358,18 +358,17 @@ public class SCLAlertView: UIViewController { func keyboardWillShow(notification: NSNotification) { keyboardHasBeenShown = true - if let userInfo = notification.userInfo { - if let beginKeyBoardFrame = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.origin.y { - if let endKeyBoardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.origin.y { - tmpContentViewFrameOrigin = self.contentView.frame.origin - tmpCircleViewFrameOrigin = self.circleBG.frame.origin - let newContentViewFrameY = beginKeyBoardFrame - endKeyBoardFrame - self.contentView.frame.origin.y - let newBallViewFrameY = self.circleBG.frame.origin.y - newContentViewFrameY - self.contentView.frame.origin.y -= newContentViewFrameY - self.circleBG.frame.origin.y = newBallViewFrameY - } - } - } + + guard let userInfo = notification.userInfo else {return} + guard let beginKeyBoardFrame = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.origin.y else {return} + guard let endKeyBoardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.origin.y else {return} + + tmpContentViewFrameOrigin = self.contentView.frame.origin + tmpCircleViewFrameOrigin = self.circleBG.frame.origin + let newContentViewFrameY = beginKeyBoardFrame - endKeyBoardFrame - self.contentView.frame.origin.y + let newBallViewFrameY = self.circleBG.frame.origin.y - newContentViewFrameY + self.contentView.frame.origin.y -= newContentViewFrameY + self.circleBG.frame.origin.y = newBallViewFrameY } func keyboardWillHide(notification: NSNotification) { @@ -447,7 +446,7 @@ public class SCLAlertView: UIViewController { viewColor = UIColor() var iconImage: UIImage? let colorInt = colorStyle ?? style.defaultColorInt - viewColor = UIColorFromRGB(colorInt) + viewColor = colorInt.toUIColor() // Icon style switch style { case .Success: @@ -501,7 +500,7 @@ public class SCLAlertView: UIViewController { // Done button if showCloseButton { let txt = completeText != nil ? completeText! : "Done" - addButton(txt, target:self, selector:Selector("hideView")) + addButton(txt, target:self, selector:#selector(SCLAlertView.hideView)) } //hidden/show circular view based on the ui option @@ -528,13 +527,13 @@ public class SCLAlertView: UIViewController { } for btn in buttons { btn.backgroundColor = viewColor - btn.setTitleColor(UIColorFromRGB(colorTextButton ?? 0xFFFFFF), forState:UIControlState.Normal) + btn.setTitleColor(colorTextButton?.toUIColor() ?? 0xFFFFFF.toUIColor(), forState:UIControlState.Normal) } // Adding duration if duration > 0 { durationTimer?.invalidate() - durationTimer = NSTimer.scheduledTimerWithTimeInterval(duration!, target: self, selector: Selector("hideView"), userInfo: nil, repeats: false) + durationTimer = NSTimer.scheduledTimerWithTimeInterval(duration!, target: self, selector: #selector(SCLAlertView.hideView), userInfo: nil, repeats: false) } // Animate in the alert view @@ -581,16 +580,6 @@ public class SCLAlertView: UIViewController { return defaultImage } } - - // Helper function to convert from RGB to UIColor - func UIColorFromRGB(rgbValue: UInt) -> UIColor { - return UIColor( - red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, - green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, - blue: CGFloat(rgbValue & 0x0000FF) / 255.0, - alpha: CGFloat(1.0) - ) - } } // ------------------------------------ diff --git a/SCLAlertView/SCLExtensions.swift b/SCLAlertView/SCLExtensions.swift new file mode 100644 index 0000000..a999e2d --- /dev/null +++ b/SCLAlertView/SCLExtensions.swift @@ -0,0 +1,41 @@ +// +// SCLExtensions.swift +// SCLAlertView +// +// Created by Christian Cabarrocas on 16/04/16. +// Copyright © 2016 Alexey Poimtsev. All rights reserved. +// + +import UIKit + +extension Int { + + func toUIColor() -> UIColor { + return UIColor( + red: CGFloat((self & 0xFF0000) >> 16) / 255.0, + green: CGFloat((self & 0x00FF00) >> 8) / 255.0, + blue: CGFloat(self & 0x0000FF) / 255.0, + alpha: CGFloat(1.0) + ) + } + + func toCGColor() -> CGColor { + return self.toUIColor().CGColor + } +} + +extension UInt { + + func toUIColor() -> UIColor { + return UIColor( + red: CGFloat((self & 0xFF0000) >> 16) / 255.0, + green: CGFloat((self & 0x00FF00) >> 8) / 255.0, + blue: CGFloat(self & 0x0000FF) / 255.0, + alpha: CGFloat(1.0) + ) + } + + func toCGColor() -> CGColor { + return self.toUIColor().CGColor + } +} \ No newline at end of file