Last active
April 11, 2018 01:46
-
-
Save uruly/38afba6561de6d672046cb3bc118eb76 to your computer and use it in GitHub Desktop.
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 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 | |
} | |
} |
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 { | |
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