Skip to content

Instantly share code, notes, and snippets.

@uruly
Last active April 11, 2018 01:46
Show Gist options
  • Save uruly/38afba6561de6d672046cb3bc118eb76 to your computer and use it in GitHub Desktop.
Save uruly/38afba6561de6d672046cb3bc118eb76 to your computer and use it in GitHub Desktop.
import UIKit
@objc protocol UpDownCountButtonDelegate {
func touchDown(sender:UIButton)
func touchCancel(sender:UIButton)
}
class UpDownCountButton: UIButton {
var delegate:UpDownCountButtonDelegate!
var count = 0 {
didSet {
self.setTitle(String(count), for: .normal)
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init(frame:CGRect,delegate:UpDownCountButtonDelegate) {
self.init(frame: frame)
self.delegate = delegate
setup()
}
func setup() {
self.setTitle("0",for:.normal)
self.setTitleColor(UIColor.black,for:.normal)
self.backgroundColor = UIColor.lightGray
//指が触れた時点でのアクション
self.addTarget(delegate,action:#selector(delegate.touchDown(sender:)),for:.touchDown)
//キャンセル処理
self.addTarget(delegate, action: #selector(delegate.touchCancel(sender:)), for: .touchCancel)
self.addTarget(delegate, action: #selector(delegate.touchCancel(sender:)), for: .touchUpInside)
self.addTarget(delegate, action: #selector(delegate.touchCancel(sender:)), for: .touchDragOutside)
//上にスワイプした時の動作
let swipeUp = UISwipeGestureRecognizer()
swipeUp.direction = .up
swipeUp.addTarget(self, action: #selector(swipe(sender:)))
self.addGestureRecognizer(swipeUp)
//下にスワイプした時の動作
let swipeDown = UISwipeGestureRecognizer()
swipeDown.direction = .down
swipeDown.addTarget(self, action:#selector(swipe(sender:)))
self.addGestureRecognizer(swipeDown)
}
@objc func swipe(sender:UISwipeGestureRecognizer){
//countする
switch sender.direction{
case .up: count += 1
case .down: count -= 1
default:break
}
}
}
import UIKit
class UpDownCountView: UIView {
var plusLabel:UILabel!
var minusLabel:UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
let width = self.frame.width
let height = self.frame.height
let itemHeight = height / 3
let btnRect = CGRect(x:0,y:height / 3,width:width,height:itemHeight)
let btn = UpDownCountButton(frame: btnRect, delegate: self)
self.addSubview(btn)
let plusRect = CGRect(x:0,y:0,width:width,height:itemHeight)
plusLabel = UILabel(frame:plusRect)
plusLabel.text = "+"
plusLabel.textAlignment = .center
plusLabel.backgroundColor = UIColor.red
self.addSubview(plusLabel)
let minusRect = CGRect(x:0,y:height * 2 / 3,width:width,height:itemHeight)
minusLabel = UILabel(frame:minusRect)
minusLabel.text = "−"
minusLabel.textAlignment = .center
minusLabel.backgroundColor = UIColor.blue
self.addSubview(minusLabel)
//隠しておく
plusLabel.isHidden = true
minusLabel.isHidden = true
}
}
extension UpDownCountView:UpDownCountButtonDelegate {
func touchDown(sender: UIButton) {
self.plusLabel.isHidden = false
self.minusLabel.isHidden = false
}
func touchCancel(sender: UIButton) {
self.plusLabel.isHidden = true
self.minusLabel.isHidden = true
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let width = self.view.frame.width
let height = self.view.frame.height
let itemWidth:CGFloat = 50
let itemHeight = itemWidth * 3 //3倍にしておく
let upDownView = UpDownCountView(frame:CGRect(x:0,y:0,width:itemWidth,height:itemHeight))
upDownView.center = CGPoint(x:width / 2,y:height / 2)
self.view.addSubview(upDownView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment