Skip to content

Commit

Permalink
migrate to swift3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DanisFabric committed Oct 14, 2016
1 parent fc0d988 commit e721768
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 259 deletions.
206 changes: 103 additions & 103 deletions Flea/Flea.swift

Large diffs are not rendered by default.

70 changes: 45 additions & 25 deletions Flea/FleaActionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}


struct FleaActionItem {
var title = ""
var color = UIColor.blueColor()
var color = UIColor.blue
var action: (() -> Void)?
}

Expand Down Expand Up @@ -39,26 +59,26 @@ class FleaActionView: UIView {
var titleLabel = { () -> UILabel in
let label = UILabel()
label.textColor = FleaPalette.DarkGray
label.textAlignment = .Center
label.font = UIFont.boldSystemFontOfSize(17)
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 17)
label.numberOfLines = 0

return label
}()
var subTitleLabel = { () -> UILabel in
let label = UILabel()
label.textColor = FleaPalette.LightGray
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(13)
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 13)
label.numberOfLines = 0

return label
}()
private var buttons = [FleaActionButton]()
fileprivate var buttons = [FleaActionButton]()
}

extension FleaActionView: FleaContentView {
func prepareInView(view: UIView) {
func prepareInView(_ view: UIView) {
addSubview(titleLabel)
addSubview(subTitleLabel)

Expand All @@ -72,11 +92,11 @@ extension FleaActionView: FleaContentView {
subTitleLabel.frame = CGRect(x: 0, y: 0, width: textWidth, height: 0)
subTitleLabel.sizeToFit()

if titleLabel.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 {
if titleLabel.text?.lengthOfBytes(using: String.Encoding.utf8) > 0 {
titleLabel.frame = CGRect(x: textMargin, y: textMargin, width: textWidth, height: titleLabel.frame.height)
maxY = titleLabel.frame.maxY
}
if subTitleLabel.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 {
if subTitleLabel.text?.lengthOfBytes(using: String.Encoding.utf8) > 0 {
subTitleLabel.frame = CGRect(x: textMargin, y: maxY + textMargin, width: textWidth, height: subTitleLabel.frame.height)
maxY = subTitleLabel.frame.maxY
}
Expand All @@ -86,29 +106,29 @@ extension FleaActionView: FleaContentView {
self.addGestureRecognizer(tap)

for item in actionItems {
let button = FleaActionButton(type: .Custom)
let button = FleaActionButton(type: .custom)

button.titleLabel?.font = UIFont.systemFontOfSize(15)
button.setTitle(item.title, forState: .Normal)
button.setTitleColor(item.color, forState: .Normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
button.setTitle(item.title, for: UIControlState())
button.setTitleColor(item.color, for: UIControlState())
button.frame = CGRect(x: 0, y: maxY, width: view.bounds.width, height: 44)
maxY += 44

print("Add Action")
button.addTarget(self, action: #selector(onTapAction(_:)), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(onTapAction(_:)), for: .touchUpInside)
addSubview(button)
buttons.append(button)
}
self.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: maxY)
}
func onTap(sender: AnyObject) {
func onTap(_ sender: AnyObject) {
print("点我")
}
func onTapAction(sender: AnyObject) {
func onTapAction(_ sender: AnyObject) {
print("点Button")

let button = sender as! FleaActionButton
let index = buttons.indexOf(button)!
let index = buttons.index(of: button)!
let item = actionItems[index]

item.action?()
Expand Down Expand Up @@ -143,19 +163,19 @@ class FleaActionButton: UIButton {
line.frame = CGRect(x: margin, y: 0, width: bounds.width - margin * 2, height: 0.5)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)

backgroundColor = FleaPalette.DarkWhite
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)

backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.white
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)

backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.white
}
}
94 changes: 57 additions & 37 deletions Flea/FleaAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}


class FleaAlertView: UIView {
weak var flea: Flea?
Expand All @@ -33,23 +53,23 @@ class FleaAlertView: UIView {
var titleLabel = { () -> UILabel in
let label = UILabel()
label.textColor = FleaPalette.DarkGray
label.textAlignment = .Center
label.font = UIFont.boldSystemFontOfSize(17)
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 17)
label.numberOfLines = 0

return label
}()
var subTitleLabel = { () -> UILabel in
let label = UILabel()
label.textColor = FleaPalette.LightGray
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(13)
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 13)
label.numberOfLines = 0

return label
}()

private var buttons = [FleaAlertButton]()
fileprivate var buttons = [FleaAlertButton]()

let contentLine = { () -> UIView in
let line = UIView()
Expand All @@ -68,7 +88,7 @@ class FleaAlertView: UIView {
}

extension FleaAlertView: FleaContentView {
func prepareInView(view: UIView) {
func prepareInView(_ view: UIView) {
addSubview(titleLabel)
addSubview(subTitleLabel)

Expand All @@ -83,65 +103,65 @@ extension FleaAlertView: FleaContentView {
subTitleLabel.frame = CGRect(x: 0, y: 0, width: textWidth, height: 0)
subTitleLabel.sizeToFit()

if titleLabel.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 {
if titleLabel.text?.lengthOfBytes(using: String.Encoding.utf8) > 0 {
titleLabel.frame = CGRect(x: textMargin, y: textMargin, width: textWidth, height: titleLabel.frame.height)
maxY = titleLabel.frame.maxY
}
if subTitleLabel.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 {
if subTitleLabel.text?.lengthOfBytes(using: String.Encoding.utf8) > 0 {
subTitleLabel.frame = CGRect(x: textMargin, y: maxY + textMargin, width: textWidth, height: subTitleLabel.frame.height)
maxY = subTitleLabel.frame.maxY
}
maxY += textMargin

if actionItems.count == 2 {
let button1 = FleaAlertButton(type: .Custom)
let button2 = FleaAlertButton(type: .Custom)
button1.titleLabel?.font = UIFont.systemFontOfSize(15)
button2.titleLabel?.font = UIFont.systemFontOfSize(15)
let button1 = FleaAlertButton(type: .custom)
let button2 = FleaAlertButton(type: .custom)
button1.titleLabel?.font = UIFont.systemFont(ofSize: 15)
button2.titleLabel?.font = UIFont.systemFont(ofSize: 15)

button1.setTitle(actionItems[0].title, forState: .Normal)
button1.setTitleColor(actionItems[0].color, forState: .Normal)
button2.setTitle(actionItems[1].title, forState: .Normal)
button2.setTitleColor(actionItems[1].color, forState: .Normal)
button1.setTitle(actionItems[0].title, for: UIControlState())
button1.setTitleColor(actionItems[0].color, for: UIControlState())
button2.setTitle(actionItems[1].title, for: UIControlState())
button2.setTitleColor(actionItems[1].color, for: UIControlState())

contentLine.frame = CGRect(x: 0, y: maxY, width: contentWidth, height: 0.5)
buttonLine.frame = CGRect(x: contentWidth / 2, y: maxY, width: 0.5, height: 44)
button1.line.hidden = true
button2.line.hidden = true
button1.line.isHidden = true
button2.line.isHidden = true

button1.frame = CGRect(x: 0, y: maxY, width: contentWidth/2, height: 44)
button2.frame = CGRect(x: contentWidth/2, y: maxY, width: contentWidth/2, height: 44)
maxY += 44

button1.addTarget(self, action: #selector(onTapButton(_:)), forControlEvents: .TouchUpInside)
button2.addTarget(self, action: #selector(onTapButton(_:)), forControlEvents: .TouchUpInside)
button1.addTarget(self, action: #selector(onTapButton(_:)), for: .touchUpInside)
button2.addTarget(self, action: #selector(onTapButton(_:)), for: .touchUpInside)

addSubview(button1)
addSubview(button2)
addSubview(contentLine)
addSubview(buttonLine)

buttons.appendContentsOf([button1,button2])
buttons.append(contentsOf: [button1,button2])
}else {
for item in actionItems {
let button = FleaAlertButton(type: .Custom)
button.titleLabel?.font = UIFont.systemFontOfSize(15)
let button = FleaAlertButton(type: .custom)
button.titleLabel?.font = UIFont.systemFont(ofSize: 15)

button.setTitle(item.title, forState: .Normal)
button.setTitleColor(item.color, forState: .Normal)
button.setTitle(item.title, for: UIControlState())
button.setTitleColor(item.color, for: UIControlState())
button.frame = CGRect(x: 0, y: maxY, width: contentWidth, height: 44)
maxY += 44

button.addTarget(self, action: #selector(onTapButton(_:)), forControlEvents: .TouchUpInside)
button.addTarget(self, action: #selector(onTapButton(_:)), for: .touchUpInside)
addSubview(button)
buttons.append(button)
}
}

self.frame = CGRect(x: 0, y: 0, width: contentWidth, height: maxY)
}
@objc private func onTapButton(sender: FleaAlertButton) {
let index = buttons.indexOf(sender)!
@objc fileprivate func onTapButton(_ sender: FleaAlertButton) {
let index = buttons.index(of: sender)!
let item = actionItems[index]

item.action?()
Expand Down Expand Up @@ -175,20 +195,20 @@ class FleaAlertButton: UIButton {
line.frame = CGRect(x: margin, y: 0, width: bounds.width - margin * 2, height: 0.5)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)

backgroundColor = FleaPalette.DarkWhite
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)

backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.white
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)

backgroundColor = UIColor.whiteColor()
backgroundColor = UIColor.white
}

}
}
26 changes: 13 additions & 13 deletions Flea/FleaNotificationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,46 @@ class FleaNotificationView: UIView {

var actionItem: FleaActionItem? {
didSet {
actionButton.setTitle(actionItem?.title, forState: .Normal)
actionButton.setTitleColor(actionItem?.color, forState: .Normal)
actionButton.setTitle(actionItem?.title, for: UIControlState())
actionButton.setTitleColor(actionItem?.color, for: UIControlState())
}
}

var titleLabel = { () -> UILabel in
let label = UILabel()
label.textColor = UIColor.whiteColor()
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(15)
label.textColor = UIColor.white
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 15)
label.numberOfLines = 0

return label
}()
var closeButton = { () -> UIButton in
let button = UIButton(type: .System)
let button = UIButton(type: .system)

let icon = UIImage(named: "flea-close", inBundle: NSBundle(forClass: FleaNotificationView.self), compatibleWithTraitCollection: nil)
button.tintColor = UIColor.whiteColor()
button.setImage(icon, forState: .Normal)
let icon = UIImage(named: "flea-close", in: Bundle(for: FleaNotificationView.self), compatibleWith: nil)
button.tintColor = UIColor.white
button.setImage(icon, for: UIControlState())

return button
}()
var actionButton = { () -> UIButton in
let button = UIButton(type: .System)
let button = UIButton(type: .system)

return button
}()

@objc private func onClose(sender: UIButton) {
@objc fileprivate func onClose(_ sender: UIButton) {
flea?.dismiss()
}
}

extension FleaNotificationView: FleaContentView {
func prepareInView(view: UIView) {
func prepareInView(_ view: UIView) {
addSubview(closeButton)
addSubview(titleLabel)
addSubview(actionButton)
closeButton.addTarget(self, action: #selector(onClose(_:)), forControlEvents: .TouchUpInside)
closeButton.addTarget(self, action: #selector(onClose(_:)), for: .touchUpInside)

let size = CGSize(width: view.bounds.width, height: 44)

Expand Down
Loading

0 comments on commit e721768

Please sign in to comment.