Skip to content

Commit

Permalink
Merge branch 'SmallRefactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
christiancabarrocas committed Apr 16, 2016
2 parents 3d0ffef + fe86bfa commit 0cbf0b6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Example/SCLAlertViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 4 additions & 0 deletions SCLAlertView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -41,6 +42,7 @@
9E124F1A1C9EA303001A4972 /* SCLAlertViewInitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewInitTests.swift; sourceTree = "<group>"; };
9E124F1C1C9EA6B5001A4972 /* SCLTextFieldTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLTextFieldTests.swift; sourceTree = "<group>"; };
9E47417E1CA1DABE00F95B05 /* SCLButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLButtonTests.swift; sourceTree = "<group>"; };
9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLExtensions.swift; sourceTree = "<group>"; };
9EE073131C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewPropertiesTests.swift; sourceTree = "<group>"; };
9EE073141C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewStyleTests.swift; sourceTree = "<group>"; };
9EE99F901CA6B2320090F845 /* SCLPublicConstructorsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLPublicConstructorsTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -91,6 +93,7 @@
isa = PBXGroup;
children = (
70264F171B0F592500B32B18 /* SCLAlertView.swift */,
9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */,
70264EFA1B0F588700B32B18 /* Supporting Files */,
);
path = SCLAlertView;
Expand Down Expand Up @@ -234,6 +237,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9EAEFBC51CC2AA6600BA87FB /* SCLExtensions.swift in Sources */,
70264F181B0F592500B32B18 /* SCLAlertView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
67 changes: 28 additions & 39 deletions SCLAlertView/SCLAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
)
}
}

// ------------------------------------
Expand Down
41 changes: 41 additions & 0 deletions SCLAlertView/SCLExtensions.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 0cbf0b6

Please sign in to comment.