Skip to content

Commit

Permalink
Merge pull request #175 from hibento/feature/DurationStatus
Browse files Browse the repository at this point in the history
Buttons: Custom Color, Duration Status and some Warnings
  • Loading branch information
vikmeup committed May 2, 2016
2 parents 8d20a72 + e08939e commit 9af7b07
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Example/SCLAlertViewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
INFOPLIST_FILE = SCLAlertViewExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "SIX-DAYS-LLC.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -425,6 +426,7 @@
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
INFOPLIST_FILE = SCLAlertViewExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "SIX-DAYS-LLC.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
7 changes: 6 additions & 1 deletion Example/SCLAlertViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ class ViewController: UIViewController {
print("Logged in")
}

alert.showInfo("Login", subTitle: "")
// Add Button with Duration Status and custom Colors
alert.addButton("Duration Button", backgroundColor: UIColor.brownColor(), textColor: UIColor.yellowColor(), showDurationStatus: true) {
print("Duration Button tapped")
}

alert.showInfo("Login", subTitle: "", duration: 10)
}

func firstButton() {
Expand Down
22 changes: 8 additions & 14 deletions SCLAlertView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@
ENABLE_TESTABILITY = YES;
INFOPLIST_FILE = SCLAlertView/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
};
Expand All @@ -381,8 +381,8 @@
ENABLE_TESTABILITY = YES;
INFOPLIST_FILE = SCLAlertView/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
};
Expand All @@ -391,29 +391,23 @@
70264F111B0F588800B32B18 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
);
FRAMEWORK_SEARCH_PATHS = "";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
INFOPLIST_FILE = SCLAlertViewTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
70264F121B0F588800B32B18 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
);
FRAMEWORK_SEARCH_PATHS = "";
INFOPLIST_FILE = SCLAlertViewTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
56 changes: 46 additions & 10 deletions SCLAlertView/SCLAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class SCLButton: UIButton {
var target:AnyObject!
var selector:Selector!
var action:(()->Void)!
var customBackgroundColor:UIColor?
var customTextColor:UIColor?
var initialTitle:String!
var showDurationStatus:Bool=false

public init() {
super.init(frame: CGRectZero)
Expand Down Expand Up @@ -136,6 +140,8 @@ public class SCLAlertView: UIViewController {
var circleBG = UIView(frame:CGRect(x:0, y:0, width:kCircleHeightBackground, height:kCircleHeightBackground))
var circleView = UIView()
var circleIconView : UIView?
var duration: NSTimeInterval!
var durationStatusTimer: NSTimer!
var durationTimer: NSTimer!
var dismissBlock : DismissBlock?
private var inputs = [UITextField]()
Expand Down Expand Up @@ -327,8 +333,8 @@ public class SCLAlertView: UIViewController {
return txt
}

public func addButton(title:String, action:()->Void)->SCLButton {
let btn = addButton(title)
public func addButton(title:String, backgroundColor:UIColor? = nil, textColor:UIColor? = nil, showDurationStatus:Bool=false, action:()->Void)->SCLButton {
let btn = addButton(title, backgroundColor: backgroundColor, textColor: textColor, showDurationStatus: showDurationStatus)
btn.actionType = SCLActionType.Closure
btn.action = action
btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), forControlEvents:.TouchUpInside)
Expand All @@ -337,8 +343,8 @@ public class SCLAlertView: UIViewController {
return btn
}

public func addButton(title:String, target:AnyObject, selector:Selector)->SCLButton {
let btn = addButton(title)
public func addButton(title:String, backgroundColor:UIColor? = nil, textColor:UIColor? = nil, showDurationStatus:Bool = false, target:AnyObject, selector:Selector)->SCLButton {
let btn = addButton(title, backgroundColor: backgroundColor, textColor: textColor, showDurationStatus: showDurationStatus)
btn.actionType = SCLActionType.Selector
btn.target = target
btn.selector = selector
Expand All @@ -348,14 +354,18 @@ public class SCLAlertView: UIViewController {
return btn
}

private func addButton(title:String)->SCLButton {
private func addButton(title:String, backgroundColor:UIColor? = nil, textColor:UIColor? = nil, showDurationStatus:Bool=false)->SCLButton {
// Update view height
kWindowHeight += kButtonHeight
// Add button
let btn = SCLButton()
btn.layer.masksToBounds = true
btn.setTitle(title, forState: .Normal)
btn.titleLabel?.font = UIFont(name:kButtonFont, size: 14)
btn.customBackgroundColor = backgroundColor
btn.customTextColor = textColor
btn.initialTitle = title
btn.showDurationStatus = showDurationStatus
contentView.addSubview(btn)
buttons.append(btn)
return btn
Expand Down Expand Up @@ -536,8 +546,7 @@ public class SCLAlertView: UIViewController {

// Done button
if showCloseButton {
let txt = completeText != nil ? completeText! : "Done"
addButton(txt, target:self, selector:#selector(SCLAlertView.hideView))
addButton(completeText ?? "Done", target:self, selector:#selector(SCLAlertView.hideView))
}

//hidden/show circular view based on the ui option
Expand Down Expand Up @@ -574,14 +583,30 @@ public class SCLAlertView: UIViewController {
}

for btn in buttons {
btn.backgroundColor = viewColor
btn.setTitleColor(UIColorFromRGB(colorTextButton ?? 0xFFFFFF), forState:UIControlState.Normal)
if let customBackgroundColor = btn.customBackgroundColor {
// Custom BackgroundColor set
btn.backgroundColor = customBackgroundColor
} else {
// Use default BackgroundColor derived from AlertStyle
btn.backgroundColor = viewColor
}

if let customTextColor = btn.customTextColor {
// Custom TextColor set
btn.setTitleColor(customTextColor, forState:UIControlState.Normal)
} else {
// Use default BackgroundColor derived from AlertStyle
btn.setTitleColor(UIColorFromRGB(colorTextButton ?? 0xFFFFFF), forState:UIControlState.Normal)
}
}

// Adding duration
if duration > 0 {
self.duration = duration
durationTimer?.invalidate()
durationTimer = NSTimer.scheduledTimerWithTimeInterval(duration!, target: self, selector: #selector(SCLAlertView.hideView), userInfo: nil, repeats: false)
durationTimer = NSTimer.scheduledTimerWithTimeInterval(self.duration, target: self, selector: #selector(SCLAlertView.hideView), userInfo: nil, repeats: false)
durationStatusTimer?.invalidate()
durationStatusTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(SCLAlertView.updateDurationStatus), userInfo: nil, repeats: true)
}

// Animate in the alert view
Expand All @@ -598,12 +623,23 @@ public class SCLAlertView: UIViewController {
return SCLAlertViewResponder(alertview: self)
}

public func updateDurationStatus() {
duration = duration.advancedBy(-1)
for btn in buttons.filter({$0.showDurationStatus}) {
let txt = "\(btn.initialTitle) (\(duration))"
btn.setTitle(txt, forState: .Normal)
}
}

// Close SCLAlertView
public func hideView() {
UIView.animateWithDuration(0.2, animations: {
self.view.alpha = 0
}, completion: { finished in

// Stop StatusTimer
self.durationStatusTimer?.invalidate()

if(self.dismissBlock != nil) {
// Call completion handler when the alert is dismissed
self.dismissBlock!()
Expand Down

0 comments on commit 9af7b07

Please sign in to comment.