|
import UIKit |
|
|
|
/// ラッパークラス |
|
final class CAButtonWrapper { |
|
private var button: UIButton! |
|
private var bar: UIView! |
|
private let completion: () -> Void |
|
|
|
init(completion: @escaping () -> Void) { |
|
self.completion = completion |
|
} |
|
|
|
func addSubviews(to view: UIView) { |
|
addEvacuationButton(parent: view) |
|
addBar(parent: view) |
|
} |
|
|
|
private func addEvacuationButton(parent: UIView) { |
|
var frame = parent.frame |
|
frame.origin.y = frame.midY |
|
frame.size.height /= 2 |
|
button = Button(frame: frame, completion: completion) |
|
parent.addSubview(button) |
|
} |
|
|
|
private func addBar(parent: UIView) { |
|
var frame = parent.frame |
|
frame.origin.y = frame.height - 20 |
|
frame.size.height = 20 |
|
bar = Bar(frame: frame) |
|
parent.addSubview(bar) |
|
} |
|
} |
|
|
|
fileprivate class Button: UIButton { |
|
private let completion: () -> Void |
|
|
|
init(frame: CGRect, completion: @escaping () -> Void) { |
|
self.completion = completion |
|
super.init(frame: frame) |
|
|
|
let text = """ |
|
手短に申し上げます。この木曜日にウィキペディアの中立性を守るためのご寄付をお願いします。読者の98%は見て見ぬ振りをして、寄付をしてくださいません。もしあなたが今年すでに寄付をしてくださった特別な読者なら、心から感謝いたします |
|
|
|
ほとんどの方はこのメッセージを読んでくださらないでしょう。ただ、私たちが願っていることがあります。オープンな知識の情報源に、何も請求されず、何も売り込まれず、広告も表示されずにアクセスできることが、どれほど貴重なことかを考えていただくことです |
|
""" |
|
setTitle(text, for: .normal) |
|
setTitleColor(.darkGray, for: .normal) |
|
titleLabel?.font = UIFont.boldSystemFont(ofSize: 20) |
|
titleLabel?.lineBreakMode = .byWordWrapping |
|
titleLabel?.numberOfLines = 0 |
|
backgroundColor = .white |
|
|
|
addTarget(self, action: #selector(tapped), for: .touchUpInside) |
|
|
|
addAnimation() |
|
} |
|
|
|
required init?(coder: NSCoder) { |
|
fatalError("init(coder:) has not been implemented") |
|
} |
|
|
|
@objc private func tapped() { |
|
if backgroundColor == .white { |
|
backgroundColor = .black |
|
} else { |
|
backgroundColor = .white |
|
} |
|
} |
|
|
|
/// ボタンが閉じるアニメーションを付与する |
|
private func addAnimation() { |
|
// layer.position = CGPoint(x: 0, y: frame.minY) |
|
// layer.anchorPoint = CGPoint(x: 0, y: 0) |
|
let animation = CABasicAnimation(keyPath: #keyPath(bounds)) |
|
|
|
animation.beginTime = CACurrentMediaTime() + 3.0 |
|
animation.duration = 1.0 |
|
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut) |
|
|
|
animation.fromValue = bounds |
|
var toValue = bounds |
|
toValue.size.height = 10 |
|
animation.toValue = toValue |
|
animation.isRemovedOnCompletion = false |
|
|
|
animation.fillMode = CAMediaTimingFillMode.forwards |
|
layer.add(animation, forKey: nil) |
|
|
|
// DelegateやCATransactionだと別画面に遷移した後のViewで不整合が出たので、タイマー処理にした |
|
Timer.scheduledTimer(withTimeInterval: 4.0, repeats: false) { [weak self] _ in |
|
self?.setTitle(nil, for: .normal) |
|
self?.completion() |
|
} |
|
} |
|
} |
|
|
|
fileprivate class Bar: UIView { |
|
override init(frame: CGRect) { |
|
super.init(frame: frame) |
|
backgroundColor = .red |
|
isUserInteractionEnabled = false |
|
} |
|
|
|
required init?(coder: NSCoder) { |
|
fatalError("init(coder:) has not been implemented") |
|
} |
|
} |