Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS View Class Design Basic

iOS View Class Design Basic

Swift愛好会 vol.40

darquro

May 10, 2019
Tweet

More Decks by darquro

Other Decks in Programming

Transcript

  1. Re:valua t ion https://fril.jp/ L ead & Delight User F

    irst Ownership Fail Smart J047JFXΫϥεઃܭجຊ .BZ BU4XJGUѪ޷ձWPM
  2.   "UPNJD%FTJHO 1BHFT ʢϖʔδʣ 5FNQMBUFT ʢςϯϓϨʔτʣ YYYY YYYY YYYY

    YYYY YYYY YYYY YYYY YYYY =  YYY YYYYYYYYYYYYYYY YYY YYY YYYYYYYYYYYYYYY
  3.   .PMFDVMFTʢ෼ࢠʣ .PMFDVMFTʢ෼ࢠʣ w 6**NBHF7JFX ݪࢠ ͱ
 6*-BCFM ݪࢠ

    ͕ਫฏʹฒͼɺ
 ؙ֯ͷ࿮ઢ ݪࢠ Λ࣋ͭ7JFX
 
 3PVOEFE$PSOFS8JUI*DPO-BCFM
  4.   ͜͜·Ͱͷ·ͱΊ w ΞϓϦέʔγϣϯΛ࡞Δ্ͰͷҰൠతͳઃܭཧ࿦ w ΞʔΩςΫνϟʔ .7$ .71 .77.

    $MFBO "SDIJUFDUVSF 3FEVY FUDʜ  w σβΠϯύλʔϯ 'BDUPSZ 1SPUPUZQF  4JOHMFUPO 0CTFSWFS FUDʜ
  5.   ͜͜·Ͱͷ·ͱΊ w ΞϓϦέʔγϣϯΛ࡞Δ্ͰͷҰൠతͳઃܭཧ࿦ w ΞʔΩςΫνϟʔ .7$ .71 .77.

    $MFBO "SDIJUFDUVSF 3FEVY FUDʜ  w σβΠϯύλʔϯ 'BDUPSZ 1SPUPUZQF  4JOHMFUPO 0CTFSWFS FUDʜ ੹຿ͷ෼཭
  6.   ᶃ9*#ͰσβΠϯ͢Δ7JFX extension NibLoadable where Self: UIView { static

    var nibName: String { return String(describing: self) } static func loadNib(_ bundle: Bundle? = nil) -> UINib { let bundle = bundle ?? Bundle.main return UINib(nibName: nibName, bundle: bundle) } func loadSubviewFromNib() { let nib = Self.loadNib(Bundle(for: type(of: self))) guard let subview = nib.instantiate(withOwner: self, options: nil).first as? UIView else { return } subviews.forEach { $0.removeFromSuperview() } addSubview(subview) setNeedsUpdateConstraints() } func fitConstraintsSubview() { if let contentView = subviews.first { contentView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ contentView.topAnchor.constraint(equalTo: self.topAnchor), contentView.leftAnchor.constraint(equalTo: self.leftAnchor), contentView.rightAnchor.constraint(equalTo: self.rightAnchor), contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor) ]) } } }
  7.   ᶃ9*#ͰσβΠϯ͢Δ7JFX required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)

    setup() } override init(frame: CGRect) { super.init(frame: frame) setup() } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() setup() } private func setup() { loadSubviewFromNib() } override func updateConstraints() { fitConstraintsContentView() super.updateConstraints() }
  8.   ᶄ!*#%FTJHOBCMFɺ!*#*OTQFDUBCMFΛ࢖͏ @IBDesignable final class RoundedCornerWithIconLabel: UIView { @IBInspectable

    var cornerRadius: CGFloat = -1 @IBInspectable var borderColor: UIColor = ColorPalette.textGray @IBInspectable var borderWidth: CGFloat = 1 @IBInspectable var text: String = "Label" @IBInspectable var textColor: UIColor = ColorPalette.textGray @IBInspectable var isImageShown: Bool = false @IBInspectable var image: UIImage? @IBInspectable var imageTintColor: UIColor = ColorPalette.textGray
  9.   ᶄ!*#%FTJHOBCMFɺ!*#*OTQFDUBCMFΛ࢖͏ @IBInspectable var text: String = "Label" {

    didSet { setText(text) } } private func setText(_ value: String) { label.text = value label.sizeToFit() }
  10.   ᶅQSFQBSF'PS*OUFSGBDF#VJMEFSΛ࢖͏ required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)

    setup() } override init(frame: CGRect) { super.init(frame: frame) setup() } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() setup() } private func setup() { loadSubviewFromNib() setCornerRadius(cornerRadius) setBorderColor(borderColor) setBorderWidth(borderWidth) setImageShown(isImageShown) setImage(image) setImageTintColor(imageTintColor) setTextColor(textColor) setText(text) setTextFontSize(textFontSize) }
  11.   ᶆMB[ZWBS*OJUJBMJ[BUJPO$MPTVSFΛ࢖͏ @IBDesignable final class ItemDetailItemSalesLabelsContainer: UIView { private

    lazy var includeCarriageLabel: UIView = { let label = RoundedCornerWithIconLabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20)) label.labelType = ItemDetailItemSalesLabelType.includeCarriage label.backgroundColor = ColorPalette.lightOrange label.borderWidth = 0 label.isImageShown = true label.image = FLFrilIcon.tagIcon(withSize: 14.0) .image(with: CGSize(width: 14.0, height: 14.0)) .withRenderingMode(.alwaysTemplate) label.imageTintColor = ColorPalette.textWhite label.text = "ૹྉࠐΈ" label.textColor = ColorPalette.textWhite label.setContentHuggingPriority(.defaultHigh, for: .horizontal) return label }()
  12.   ᶇ0QUJPO4FUΛ࢖͏ struct ItemDetailItemSalesLabelOptionSet: OptionSet { let rawValue: UInt8

    static let includeCarriage = ItemDetailItemSalesLabelOptionSet(rawValue: 1 << 0) static let excludeCarriage = ItemDetailItemSalesLabelOptionSet(rawValue: 1 << 1) static let anonymousCarriage = ItemDetailItemSalesLabelOptionSet(rawValue: 1 << 2) static let nowAvailable = ItemDetailItemSalesLabelOptionSet(rawValue: 1 << 3) } @IBDesignable final class ItemDetailItemSalesLabelsContainer: UIView { var labelSet: ItemDetailItemSalesLabelOptionSet = [] }