Skip to content

Commit 22d7b39

Browse files
author
蔡龙君
committed
feat |> 完成基本功能
1 parent fd936e8 commit 22d7b39

File tree

5 files changed

+623
-31
lines changed

5 files changed

+623
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SFAlertController
22

3-
[![CI Status](https://img.shields.io/travis/蔡龙君/SFAlertController.svg?style=flat)](https://travis-ci.org/蔡龙君/SFAlertController)
3+
[![CI Status](https://img.shields.io/travis/coderflower/SFAlertController.svg?style=flat)](https://travis-ci.org/coderflower/SFAlertController)
44
[![Version](https://img.shields.io/cocoapods/v/SFAlertController.svg?style=flat)](https://cocoapods.org/pods/SFAlertController)
55
[![License](https://img.shields.io/cocoapods/l/SFAlertController.svg?style=flat)](https://cocoapods.org/pods/SFAlertController)
66
[![Platform](https://img.shields.io/cocoapods/p/SFAlertController.svg?style=flat)](https://cocoapods.org/pods/SFAlertController)
@@ -22,7 +22,7 @@ pod 'SFAlertController'
2222

2323
## Author
2424

25-
蔡龙君, cailongjun@huami.com
25+
花菜, developer.swifter@gmail.com
2626

2727
## License
2828

SFAlertController/Classes/Extensions.swift

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,54 @@
22
// Extensions.swift
33
// SFAlertController
44
//
5-
// Created by 蔡龙君 on 2019/8/28.
5+
// Created by 花菜 on 2019/8/28.
66
// Copyright © 2019 花菜. All rights reserved.
77
//
88

99
import Foundation
10+
public extension SFAlertController {
11+
/// 显示样式
12+
enum Style: Int {
13+
case sheet
14+
case alert
15+
}
16+
17+
/// 按钮样式
18+
enum AlertButton {
19+
case `default`(String)
20+
case cancel(String)
21+
case disabled(String)
22+
case destructive(String)
23+
var style: UIAlertAction.Style {
24+
switch self {
25+
case .default, .disabled:
26+
return .default
27+
case .cancel:
28+
return .cancel
29+
case .destructive:
30+
return .destructive
31+
}
32+
}
33+
34+
var title: String {
35+
switch self {
36+
case .default(let title):
37+
return title
38+
case .cancel(let title):
39+
return title
40+
case .disabled(let title):
41+
return title
42+
case .destructive(let title):
43+
return title
44+
}
45+
}
46+
var isDsiabled: Bool {
47+
switch self {
48+
case .disabled:
49+
return true
50+
default:
51+
return false
52+
}
53+
}
54+
}
55+
}

SFAlertController/Classes/SFAlertCell.swift

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,122 @@
22
// SFAlertCell.swift
33
// SFAlertController
44
//
5-
// Created by 蔡龙君 on 2019/8/28.
5+
// Created by 花菜 on 2019/8/28.
66
// Copyright © 2019 花菜. All rights reserved.
77
//
88

99
import UIKit
1010

1111
class SFAlertCell: UITableViewCell {
12+
static var identifier = "SFAlertCell"
13+
/// 分割线
14+
private(set) lazy var separatorView: UIView = {
15+
$0.translatesAutoresizingMaskIntoConstraints = false
16+
$0.backgroundColor = UIColor(red: 0.783922, green: 0.780392, blue: 0.8, alpha: 1)
17+
return $0
18+
}(UIView())
19+
20+
private lazy var leftButton: UIButton = {
21+
$0.setTitleColor(UIColor(red: 0, green: 0.48, blue: 1, alpha: 1), for: .selected)
22+
$0.setTitleColor(UIColor.black, for: .normal)
23+
$0.backgroundColor = UIColor.white
24+
$0.setTitleColor(UIColor.black.withAlphaComponent(0.25), for: .disabled)
25+
$0.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
26+
$0.addTarget(self, action: #selector(leftButtonTapped), for: .touchUpInside)
27+
return $0
28+
}(UIButton(type: .custom))
29+
30+
private lazy var rightButton: UIButton = {
31+
$0.isHidden = true
32+
$0.setTitleColor(UIColor(red: 0, green: 0.48, blue: 1, alpha: 1), for: .selected)
33+
$0.setTitleColor(UIColor.black, for: .normal)
34+
$0.backgroundColor = UIColor.white
35+
$0.setTitleColor(UIColor.black.withAlphaComponent(0.25), for: .disabled)
36+
$0.addTarget(self, action: #selector(rightButtonTapped), for: .touchUpInside)
37+
return $0
38+
}(UIButton(type: .custom))
39+
40+
var leftCompletion: (() -> Void)?
41+
var rightCompletion: (() -> Void)?
42+
43+
/// 内容视图
44+
private lazy var stackView: UIStackView = {
45+
$0.translatesAutoresizingMaskIntoConstraints = false
46+
$0.alignment = .fill
47+
$0.distribution = .fillProportionally
48+
$0.axis = .horizontal
49+
return $0
50+
}(UIStackView(arrangedSubviews: [leftButton, rightButton]))
51+
52+
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
53+
super.init(style: style, reuseIdentifier: reuseIdentifier)
54+
configSubviews()
55+
}
56+
57+
required init?(coder aDecoder: NSCoder) {
58+
super.init(coder: aDecoder)
59+
configSubviews()
60+
}
61+
}
62+
63+
/// public
64+
extension SFAlertCell {
65+
func config(using leftButtonType: SFAlertController.AlertButton, rightButtonType: SFAlertController.AlertButton? = nil) {
66+
67+
configButton(leftButton, using: leftButtonType)
68+
guard let rightButtonType = rightButtonType else {
69+
separatorView.isHidden = true
70+
rightButton.isHidden = true
71+
return
72+
}
73+
separatorView.isHidden = false
74+
configButton(rightButton, using: rightButtonType)
75+
}
76+
}
1277

13-
override func awakeFromNib() {
14-
super.awakeFromNib()
15-
// Initialization code
78+
/// private
79+
extension SFAlertCell {
80+
81+
private func configSubviews() {
82+
contentView.addSubview(stackView)
83+
contentView.addSubview(separatorView)
84+
selectionStyle = .none
85+
contentView.backgroundColor = UIColor(red: 0.783922, green: 0.780392, blue: 0.8, alpha: 1)
86+
stackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0.5).isActive = true
87+
stackView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
88+
stackView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
89+
stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
90+
91+
separatorView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
92+
separatorView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
93+
separatorView.widthAnchor.constraint(equalToConstant: 0.5).isActive = true
94+
separatorView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
1695
}
1796

18-
override func setSelected(_ selected: Bool, animated: Bool) {
19-
super.setSelected(selected, animated: animated)
97+
private func configButton(_ button: UIButton, using type: SFAlertController.AlertButton) {
98+
switch type {
99+
case .cancel:
100+
button.isEnabled = true
101+
button.isSelected = false
102+
case .disabled:
103+
button.isEnabled = false
104+
button.isSelected = false
105+
case .default:
106+
button.isEnabled = true
107+
button.isSelected = false
108+
case .destructive:
109+
button.isEnabled = true
110+
button.isSelected = true
111+
}
112+
button.setTitle(type.title, for: .normal)
113+
button.isHidden = false
114+
}
20115

21-
// Configure the view for the selected state
116+
@objc private func leftButtonTapped() {
117+
leftCompletion?()
22118
}
23119

120+
@objc private func rightButtonTapped() {
121+
rightCompletion?()
122+
}
24123
}

0 commit comments

Comments
 (0)