thieumao

DemoViewController.swift

Nov 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.05 KB | None | 0 0
  1. //
  2. //  DemoViewController.swift
  3. //  DemoView
  4. //
  5. //  Created by Nguyen Van Thieu B on 11/7/16.
  6. //  Copyright © 2016 Thieu Mao. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class DemoViewController: UIViewController {
  12.    
  13.     @IBOutlet weak var myScrollView: UIScrollView!
  14.    
  15.     override func viewDidLoad() {
  16.         super.viewDidLoad()
  17.        
  18.         let pading:CGFloat = 1;
  19.         let leftMarginHour:CGFloat = 5
  20.         let rightMarginEvent:CGFloat = 10
  21.         let widthHour:CGFloat = 50
  22.         let widthRest:CGFloat = UIScreen.main.bounds.width - widthHour - leftMarginHour - rightMarginEvent
  23.        
  24.         //  draw Hour
  25.         var d1hourBegin:CGFloat = 0;
  26.         var d1hourEnd:CGFloat = 0;
  27.         for index in 0...24 {
  28.             let y : CGFloat = CGFloat(index * 100)
  29.             var text = String(index)
  30.             if (index < 10) {
  31.                 text = "0" + text
  32.             }
  33.             text = text + ":00"
  34.             drawHour(text: text, x: leftMarginHour, y: y, width: widthHour, height: 50)
  35.             if (index == 0) {
  36.                 d1hourBegin = y;
  37.             }
  38.             if (index == 1) {
  39.                 d1hourEnd = y;
  40.             }
  41.         }
  42.         var d1hour:CGFloat = d1hourEnd - d1hourBegin;
  43.        
  44.         // draw Event
  45.         var a:[CGFloat] = [1, 3, 4, 4, 4.2, 11, 14, 15]
  46.         var b:[CGFloat] = [4, 4, 5, 5, 5, 13, 17, 18]
  47.         var n:Int = 8
  48.  
  49.         var pairs:[(position: Int, count: Int)] = []
  50.         var count = 1
  51.         for i in 0...n-2 {
  52.             print("i= ", i)
  53.             if (a[i]<=a[i+1] && a[i+1]<b[i]) {
  54.                 count += 1
  55.                 if (i == n-2) {
  56.                     pairs += [(position: i+1, count: count)]
  57.                     print("---- i=", i+1, " count=", count)
  58.                 }
  59.             } else {
  60.                 // save i, count
  61.                 pairs += [(position: i, count: count)]
  62.                 print("---- i=", i, " count=", count)
  63.                 count = 1
  64.                 if (i == n-2) {
  65.                     pairs += [(position: i+1, count: count)]
  66.                     print("---- i=", i+1, " count=", count)
  67.                 }
  68.             }
  69.         }
  70.        
  71.         for pair in pairs {
  72.             var positionFrom:Int = pair.position - pair.count + 1
  73.             var positionTo:Int = pair.position
  74.             var count:Int = pair.count
  75.             print("from:", positionFrom, " to:", positionTo, " count:", count)
  76.             var dem:Int = 0
  77.             for i in positionFrom...positionTo {
  78.                 print(i)
  79.                 let width:CGFloat = widthRest / CGFloat(count);
  80.                 let height:CGFloat = (b[i] - a[i]) * d1hour;
  81.                 let x:CGFloat = 60 + CGFloat(dem) * width
  82.                 let y:CGFloat = 25 + a[i] * d1hour
  83.                 drawEvent(text: "Mao", x: x, y: y, width: width, height: height)
  84.                 dem += 1
  85.             }
  86.         }
  87.        
  88.         // add contentSize for ScrollView
  89.         var contentRect = CGRect.zero
  90.         for view: UIView in self.myScrollView.subviews {
  91.             contentRect = contentRect.union(view.frame)
  92.         }
  93.         self.myScrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: contentRect.height)
  94.     }
  95.    
  96.     func drawHour(text: String, x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
  97.         let label:UILabel = UILabel(frame: CGRect(x: x, y: y, width: width, height: height))
  98.         label.text = text
  99.         myScrollView.addSubview(label)
  100.     }
  101.    
  102.     func drawEvent(text: String, x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
  103.         let myView:UIView = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
  104.         myView.backgroundColor = UIColor.orange
  105.         myView.layer.borderWidth = 1.0
  106.         let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: height))
  107.         label.text = text
  108.         label.textColor = UIColor.white
  109.         label.textAlignment = .center
  110.         myView.addSubview(label)
  111.         myScrollView.addSubview(myView)
  112.     }
  113.    
  114. }
Add Comment
Please, Sign In to add comment