Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions VKEBExample/VKEBExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
10 changes: 5 additions & 5 deletions VKEBExample/VKEBExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ViewController: UIViewController

// Add manually VKExpandableButton with text content via code
// Direction: RIGHT (from left to right)
self.buttonRight = VKExpandableButton(frame: CGRectMake(16, 28, 80, 44))
self.buttonRight.direction = .Right
self.buttonRight = VKExpandableButton(frame: CGRect(x: 16, y: 28, width: 80, height: 44))
self.buttonRight.direction = .right
self.buttonRight.options = ["Auto", "On", "Off"]
self.buttonRight.currentValue = self.buttonRight.options[0]
self.buttonRight.cornerRadius = self.buttonRight.frame.size.height / 2
Expand All @@ -38,7 +38,7 @@ class ViewController: UIViewController

// Customized VKExpandableButton with UIImage content
// Direction: LEFT (from right to left)
self.buttonLeft.direction = .Left
self.buttonLeft.direction = .left
self.buttonLeft.options = [UIImage(named: "icon1")!, UIImage(named: "icon2")!, UIImage(named: "icon3")!]
self.buttonLeft.currentValue = self.buttonLeft.options[2]
self.buttonLeft.cornerRadius = self.buttonLeft.frame.size.height / 2
Expand All @@ -56,7 +56,7 @@ class ViewController: UIViewController

// VKExpandableButton with text content
// Direction: DOWN (from top to bottom)
self.buttonDown.direction = .Down
self.buttonDown.direction = .down
self.buttonDown.options = ["Auto", "On", "Off"]
self.buttonDown.currentValue = self.buttonRight.options[1]

Expand All @@ -67,7 +67,7 @@ class ViewController: UIViewController

// VKExpandableButton with mixed content
// Direction: UP (from bottom to top)
self.buttonUp.direction = .Up
self.buttonUp.direction = .up
self.buttonUp.options = ["Dog", UIImage(named: "icon2")!, "Mouse"]
self.buttonUp.currentValue = self.buttonUp.options[0]
self.buttonUp.optionSelectionBlock = {
Expand Down
108 changes: 54 additions & 54 deletions VKExpandableButton/VKExpandableButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ typealias OptionSelectionBlock = (index: Int) -> Void

enum VKExpandableButtonDirection
{
case Up
case Right
case Down
case Left
case up
case right
case down
case left
}

// MARK: - Init
Expand All @@ -24,13 +24,13 @@ class VKExpandableButton: UIView
{
var options: [AnyObject]!

var direction: VKExpandableButtonDirection = .Right
var direction: VKExpandableButtonDirection = .right

var textFont = UIFont.systemFontOfSize(15.0)
var textColor = UIColor.whiteColor()
var textFont = UIFont.systemFont(ofSize: 15.0)
var textColor = UIColor.white()

var autoHideOptions = true
var animationDuration: NSTimeInterval = 0.275
var animationDuration: TimeInterval = 0.275
private var minOptionSize: CGFloat = 60

var imageInsets: UIEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6)
Expand All @@ -40,19 +40,19 @@ class VKExpandableButton: UIView
}
}

var buttonBackgroundColor = UIColor.lightGrayColor()
var buttonBackgroundColor = UIColor.lightGray()
{
didSet {
self.button.backgroundColor = buttonBackgroundColor
}
}

var expandedButtonBackgroundColor = UIColor.lightGrayColor()
var expandedButtonBackgroundColor = UIColor.lightGray()

var selectionColor = UIColor.grayColor()
var selectionColor = UIColor.gray()
{
didSet {
self.button.setBackgroundImage(VKExpandableButton.imageWithColor(selectionColor), forState: .Highlighted)
self.button.setBackgroundImage(VKExpandableButton.imageWithColor(selectionColor), for: .highlighted)
}
}

Expand All @@ -70,11 +70,11 @@ class VKExpandableButton: UIView
{
if currentValue is String
{
self.button.setTitle(currentValue as? String, forState: .Normal)
self.button.setTitle(currentValue as? String, for: UIControlState())
}
else if currentValue is UIImage
{
self.button.setImage(currentValue as? UIImage, forState: .Normal)
self.button.setImage(currentValue as? UIImage, for: UIControlState())
}
}
}
Expand Down Expand Up @@ -110,20 +110,20 @@ class VKExpandableButton: UIView
self.button.layer.cornerRadius = self.cornerRadius
self.button.backgroundColor = self.buttonBackgroundColor

self.button.imageView?.contentMode = .ScaleAspectFit
self.button.imageView?.contentMode = .scaleAspectFit
self.button.imageEdgeInsets = self.imageInsets

self.button.titleLabel?.textAlignment = .Center
self.button.setTitleColor(self.textColor, forState: .Normal)
self.button.titleLabel?.textAlignment = .center
self.button.setTitleColor(self.textColor, for: UIControlState())

self.button.titleLabel?.adjustsFontSizeToFitWidth = true

self.button.setBackgroundImage(VKExpandableButton.imageWithColor(selectionColor), forState: .Highlighted)
self.button.addTarget(self, action: #selector(VKExpandableButton.showOptions), forControlEvents: .TouchUpInside)
self.button.setBackgroundImage(VKExpandableButton.imageWithColor(selectionColor), for: .highlighted)
self.button.addTarget(self, action: #selector(VKExpandableButton.showOptions), for: .touchUpInside)

self.addSubview(self.button)

self.backgroundColor = UIColor.clearColor()
self.backgroundColor = UIColor.clear()
self.button.layer.masksToBounds = true
self.optionsView.layer.masksToBounds = true
}
Expand All @@ -145,60 +145,60 @@ extension VKExpandableButton
// Create buttons for each option
for option in self.options
{
var frame = CGRectMake(desiredOrigin, 0, initialWidth, optionsView.frame.size.height)
var frame = CGRect(x: desiredOrigin, y: 0, width: initialWidth, height: optionsView.frame.size.height)

if self.direction == .Up || self.direction == .Down
if self.direction == .up || self.direction == .down
{
frame = CGRectMake(0, desiredOrigin, initialWidth, optionsView.frame.size.height)
frame = CGRect(x: 0, y: desiredOrigin, width: initialWidth, height: optionsView.frame.size.height)
}

let button = UIButton(frame: frame)

if option is UIImage
{
button.setImage(option as? UIImage, forState: .Normal)
button.imageView?.contentMode = .ScaleAspectFit
button.setImage(option as? UIImage, for: UIControlState())
button.imageView?.contentMode = .scaleAspectFit
button.imageEdgeInsets = self.imageInsets
}
else if option is String
{
let textLength = (option as! NSString).sizeWithAttributes([NSFontAttributeName : self.textFont]).width
let textLength = (option as! NSString).size(attributes: [NSFontAttributeName : self.textFont]).width

if textLength > self.minOptionSize && textLength > self.button.bounds.size.width
{
button.frame.size.width = textLength
}

button.setTitle(option as? String, forState: .Normal)
button.setTitleColor(self.textColor, forState: .Normal)
button.setTitle(option as? String, for: UIControlState())
button.setTitleColor(self.textColor, for: UIControlState())
}
else
{
// Unknown type
button.setTitle("UNKNOWN_TYPE", forState: .Normal)
button.setTitleColor(UIColor.redColor(), forState: .Normal)
button.setTitle("UNKNOWN_TYPE", for: UIControlState())
button.setTitleColor(UIColor.red(), for: UIControlState())
}

button.titleLabel?.font = self.textFont
button.titleLabel?.textAlignment = .Center
button.backgroundColor = UIColor.clearColor()
button.titleLabel?.textAlignment = .center
button.backgroundColor = UIColor.clear()
button.titleLabel?.adjustsFontSizeToFitWidth = true

button.setBackgroundImage(VKExpandableButton.imageWithColor(self.selectionColor), forState: .Highlighted)
button.addTarget(self, action: #selector(VKExpandableButton.onOptionButtonAction), forControlEvents: .TouchUpInside)
button.setBackgroundImage(VKExpandableButton.imageWithColor(self.selectionColor), for: .highlighted)
button.addTarget(self, action: #selector(VKExpandableButton.onOptionButtonAction), for: .touchUpInside)
button.tag = i

self.optionsView.addSubview(button)

desiredOrigin += self.direction == .Up || self.direction == .Down ? button.frame.size.height : button.frame.size.width
desiredOrigin += self.direction == .up || self.direction == .down ? button.frame.size.height : button.frame.size.width
i += 1
}

self.superview?.addSubview(optionsView)

let size = desiredOrigin

UIView.animateWithDuration(self.animationDuration)
UIView.animate(withDuration: self.animationDuration)
{
self.optionsView.backgroundColor = self.expandedButtonBackgroundColor
self.optionsView.frame = self.frame(size: size, direction: self.direction)
Expand All @@ -207,7 +207,7 @@ extension VKExpandableButton

func hideOptions()
{
UIView.animateWithDuration(self.animationDuration, animations:
UIView.animate(withDuration: self.animationDuration, animations:
{
self.optionsView.backgroundColor = self.buttonBackgroundColor
self.optionsView.frame = self.frame
Expand All @@ -228,30 +228,30 @@ extension VKExpandableButton
// MARK: - Private
extension VKExpandableButton
{
private func frame(size size: CGFloat, direction: VKExpandableButtonDirection) -> CGRect
private func frame(size: CGFloat, direction: VKExpandableButtonDirection) -> CGRect
{
switch direction
{
case .Up:
return CGRectMake(self.frame.origin.x, self.frame.maxY - size, self.frame.size.width, size)
case .up:
return CGRect(x: self.frame.origin.x, y: self.frame.maxY - size, width: self.frame.size.width, height: size)

case .Right:
return CGRectMake(self.frame.minX, self.frame.origin.y, size, self.frame.size.height)
case .right:
return CGRect(x: self.frame.minX, y: self.frame.origin.y, width: size, height: self.frame.size.height)

case .Down:
return CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, size)
case .down:
return CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: size)

case .Left:
return CGRectMake(self.frame.maxX - size, self.frame.origin.y, size, self.frame.size.height)
case .left:
return CGRect(x: self.frame.maxX - size, y: self.frame.origin.y, width: size, height: self.frame.size.height)
}
}

@objc private func onButtonAction(sender: UIButton)
@objc private func onButtonAction(_ sender: UIButton)
{
self.showOptions()
}

@objc private func onOptionButtonAction(sender: UIButton)
@objc private func onOptionButtonAction(_ sender: UIButton)
{
self.currentValue = self.options[sender.tag]

Expand All @@ -262,9 +262,9 @@ extension VKExpandableButton

if self.autoHideOptions
{
UIView.animateWithDuration(self.animationDuration, animations:
UIView.animate(withDuration: self.animationDuration, animations:
{
sender.frame = CGRectMake(0, 0, self.button.frame.size.width, self.button.frame.size.height)
sender.frame = CGRect(x: 0, y: 0, width: self.button.frame.size.width, height: self.button.frame.size.height)
})

self.hideOptions()
Expand All @@ -275,15 +275,15 @@ extension VKExpandableButton
// MARK: - Utils
extension VKExpandableButton
{
private class func imageWithColor(color: UIColor) -> UIImage
private class func imageWithColor(_ color: UIColor) -> UIImage
{
let size = CGSize(width: 64, height: 64)
let rect = CGRectMake(0, 0, size.width, size.height)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
}