Skip to content

Instantly share code, notes, and snippets.

@0si43
Last active August 26, 2021 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0si43/039e40aaa5b72db9c1903cd9adf3de6d to your computer and use it in GitHub Desktop.
Save 0si43/039e40aaa5b72db9c1903cd9adf3de6d to your computer and use it in GitHub Desktop.
A sample code using Core Animation
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")
}
}
//
// ViewController.swift
// AnimationSample
//
// Created by nak-ts on 2021/08/26.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .lightGray
let label = UILabel(frame: view.frame)
label.text = """
こんにちは。このアプリを開いていただきありがとうございます。
これがメインコンテンツです。
いかがでしたか?
"""
label.numberOfLines = 0
view.addSubview(label)
let button = ButtonWrapper(completion: setWhiteBackground)
button.addSubviews(to: view)
}
private func setWhiteBackground() {
view.backgroundColor = .white
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment