DemoView.swift 2.87キロバイト
//
//  DemoView.swift
//  jacket_test_ios
//
//  Created by USER on 2017/10/11.
//  Copyright © 2017年 ドラッサル 亜嵐. All rights reserved.
//

import UIKit

class DemoView: UIView, UITextFieldDelegate {

    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    
    let label = UILabel()
    let textbox_distance_fillter = UITextField()
    let button_distance_fillter = UIButton()
    
    override func draw(_ rect: CGRect) {
        Caller.sheardInstance.setProximityNofitication(proximityFillter:50)
        textbox_distance_fillter.text = "50"
        // Drawing code
        label.text = " Demo "
        label.sizeToFit()
        self.addSubview(label)
        ///////
        
        let tWidth: CGFloat = 200
        let tHeight: CGFloat = 40
        let posX: CGFloat = (self.bounds.width - tWidth)
        let posY: CGFloat = (self.bounds.height - tHeight)
        

        //////
        textbox_distance_fillter.keyboardType = .numberPad
       // textbox_distance_fillter.sizeToFit()
        textbox_distance_fillter.frame = CGRect(x: (posX/6), y: (posY/12), width: tWidth,height: tHeight)
        textbox_distance_fillter.delegate = self
        // 枠を表示する.
        textbox_distance_fillter.borderStyle = .roundedRect

        self.addSubview(textbox_distance_fillter)
        ///////
        
        ///////
        button_distance_fillter.addTarget(self, action: #selector(setDistanceFillter), for:.touchUpInside)
        button_distance_fillter.frame = CGRect(x: (posX/6), y: (posY/6), width: tWidth, height: tHeight)
        button_distance_fillter.setTitle("fillter", for: .normal)
        
        button_distance_fillter.setBackgroundImage(self.createImageFromUIColor(color: UIColor.gray), for: .normal)
        button_distance_fillter.setBackgroundImage(self.createImageFromUIColor(color: UIColor.lightGray), for: .highlighted)
        
        button_distance_fillter.setTitleColor(UIColor.white, for: .normal)
        self.addSubview(button_distance_fillter)
        ///////
    }
    func setDistanceFillter(sender: UIButton){
        if let fillter = textbox_distance_fillter.text{
            if let distanceF = Int(fillter){Caller.sheardInstance.setProximityNofitication(proximityFillter:distanceF)
                self.endEditing(true)
            }
        }
    }
    private func createImageFromUIColor(color: UIColor) -> UIImage {
        // 1x1のbitmapを作成
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        // bitmapを塗りつぶし
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        // UIImageに変換
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }

}