#Swift 4.2
押すとクニュっとするボタンのサンプルコードです。
iOSでよくあるマイクロインタラクションです。
僕はマイクロインタラクション大好きなんですが、エンジニアやデザイナーの自己満なんでしょうかね?
わからんとです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
var button: CustomButton! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
let width = view.frame.width | |
let height = view.frame.height | |
let buttonWidth = width * 0.8 | |
let buttonHeight: CGFloat = 80 | |
button = CustomButton() | |
button.addTarget(self, action: #selector(touchUpInside(_:)), for: UIControl.Event.touchUpInside) | |
button.addTarget(self, action: #selector(touchDown(_:)), for: UIControl.Event.touchDown) | |
button.addTarget(self, action: #selector(touchDragExit(_:)), for: UIControl.Event.touchDragExit) | |
button.setTitle("ボタンのテキスト", for: UIControl.State.normal) | |
button.setTitleColor(.red, for: UIControl.State.normal) | |
button.frame = CGRect(x: (width - buttonWidth) / 2, y: (height - buttonHeight) / 2, width: buttonWidth, height: buttonHeight) | |
view.addSubview(button) | |
} | |
@objc func touchDown(_ sender: UIButton) { | |
print("touchDown") | |
(sender as! CustomButton).onTap() | |
} | |
@objc func touchUpInside(_ sender: UIButton) { | |
print("touchUpInside") | |
(sender as! CustomButton).unTap() | |
} | |
@objc func touchDragExit(_ sender: UIButton) { | |
print("touchDragExit") | |
(sender as! CustomButton).unTap() | |
} | |
} |