不喜欢码字的程序员有没有,宁愿多敲两行代码,也不愿意多写两个字。
import Foundation
import UIKit
@objc(DateObject)
class DateObject: NSObject{
var day : Int?
var month : Int?
var year : Int?
var btn :UIButton?
}
import Foundation
extension NSDate{
/**
当前日期所在星期几
returns:星期几
*/
func dayOfWeek() ->NSInteger{
var calendar:NSCalendar =NSCalendar.currentCalendar()
var weekdayComponents:NSDateComponents = calendar.components(NSCalendarUnit.CalendarUnitWeekday,fromDate:self)
var week:NSInteger = weekdayComponents.weekday
week -= 2
if(week < 0){
week += 7;
}
return week
}
/**
当前日期所在月份有多少天
returns:月份天数
*/
func numberOfDaysInMonth()->NSInteger{
var calendar:NSCalendar =NSCalendar.currentCalendar()
var range:NSRange = calendar.rangeOfUnit(NSCalendarUnit.CalendarUnitDay,inUnit:NSCalendarUnit.CalendarUnitMonth,forDate: self)
var num:NSInteger = range.length
return num;
}
/**
当前日期所在月份第一天
returns:月份第一天
*/
func monthFirstDay() ->NSDate{
var calendar:NSCalendar =NSCalendar.currentCalendar()
var dayComponents:NSDateComponents = calendar.components((NSCalendarUnit.CalendarUnitYear |NSCalendarUnit.CalendarUnitMonth),fromDate: self)
dayComponents.day = 2;
var firstDate:NSDate = calendar.dateFromComponents(dayComponents)!
return firstDate;
}
/**
当前日期所在月份最后一天
returns:月份最后一天
*/
func monthLastDay() ->NSDate{
self)
dayComponents.day =self.numberOfDaysInMonth()+1;
var lastDate:NSDate = calendar.dateFromComponents(dayComponents)!
return lastDate;
}
}
//
// MyCalendar.swift
// CoolTalk
// Created by JoesRain on 15/9/16.
// Copyright (c) 2015年 BreazeMago. All rights reserved.
//
import UIKit
import Foundation
@objc(CalendarJoes)
class CalendarJoes:UIView {
var num:NSInteger!
var date:NSDate!
var firstDate:NSDate!
var lastDate:NSDate!
var headView:UIView!
var footView:UIView!
var title:UILabel!
var dayArray:NSMutableArray!
var lastBtn:UIButton!
var delegate:TestDelegate!
var Today:DateObject!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor =UIColor.whiteColor();
date = NSDate()
num = date!.numberOfDaysInMonth()
firstDate =date!.monthFirstDay()
lastDate =date!.monthLastDay()
dayArray =NSMutableArray()
Today = DateObject()
var calendar:NSCalendar =NSCalendar.currentCalendar()
var dayComponents:NSDateComponents = calendar.components((NSCalendarUnit.CalendarUnitYear |NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay),fromDate: date!)
Today?.year = dayComponents.year
Today?.month = dayComponents.month
Today?.day = dayComponents.day
initUI()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initUI(){
createHeadView()
createFootView()
}
/**
创建日历具体日期
*/
func createFootView(){
dayArray?.removeAllObjects()
var scw = self.bounds.width
footView = UIView(frame: CGRectMake(0,85,scw,200))
//footView?.backgroundColor = UIColor.lightGrayColor()
var thisDay = NSDate()
var week:NSInteger =firstDate!.dayOfWeek()
var tmp:NSInteger = week
var col:NSInteger = 0
var max:Float = (Float)(week+num!)/7
max += 0.4;
max = roundf(max)
//println("week+num = \(week+num!),max = \(max)")
var height:Float = 200.00/max
height = roundf(height)
var width:Float = (Float)(scw-40)/7
width = roundf(width)
var j:NSInteger = 0
vartest1:Bool =NSUserDefaults.standardUserDefaults().boolForKey("test1")as Bool!
test2:Bool =NSUserDefaults.standardUserDefaults().boolForKey("test2")as Bool!
if(NSUserDefaults.standardUserDefaults().dictionaryForKey("test3") != nil){
vartest3: Dictionary =NSUserDefaults.standardUserDefaults().dictionaryForKey("test3")as Dictionary!
//varvalue:NSString = dict.objectForKey("three") as NSString
var test4:NSInteger =test3["test3"]!.integerValue
}
for j; j<num ;j++ {
var obj:DateObject = DateObject()
var calendar:NSCalendar =NSCalendar.currentCalendar()
var dayComponents:NSDateComponents = calendar.components((NSCalendarUnit.CalendarUnitYear |NSCalendarUnit.CalendarUnitMonth),fromDate: date!)
obj.year = dayComponents.year
obj.month = dayComponents.month
obj.day = j+1
if(j != 0){
tmp += 1
}
week = tmp%7
col = tmp/7
var off_x = Float(week)*width + 20
var off_y = Float(col)*height
var btn:UIButton = UIButton(frame:CGRectMake(CGFloat(off_x),CGFloat(off_y),CGFloat(width),CGFloat(height)))
var title:UILabel = UILabel(frame: CGRectMake(0, CGFloat((height-15)/2),15))
title.text ="\(j+1)"
title.font =UIFont.systemFontOfSize(18);
title.textAlignment =NSTextAlignment.Center
btn.addTarget(self,action:"dayClick:",forControlEvents: UIControlEvents.TouchUpInside)
footView?.addSubview(btn)
btn.enabled =false;
title.textColor =UIColor(red: CGFloat(0.62),green:CGFloat(0.62),blue: CGFloat(0.62),alpha: CGFloat(1))
if(date?.laterDate(thisDay) ==date){
btn.enabled =true;
title.textColor =UIColor.blackColor()
}else{
if(obj.year! == Today.year!)
{
if(obj.month! ==Today.month!){
if((Int(obj.day!)-Int(Today.day!)) >= 3){
btn.enabled =true;
title.textColor =UIColor.blackColor()
}
}
}
}
if(!isStartTime){
}
btn.addSubview(title)
obj.btn = btn
btn.tag = j
dayArray?.addObject(obj)
//println("obj is \(obj.year) \(obj.month) \(obj.day)")
}
self.addSubview(footView!)
var swipLeft:UISwipeGestureRecognizer =UISwipeGestureRecognizer(target:self,action:"nextMonth")
var swipRight:UISwipeGestureRecognizer =UISwipeGestureRecognizer(target:self,action:"preMonth")
swipLeft.direction =UISwipeGestureRecognizerDirection.Left
swipRight.direction =UISwipeGestureRecognizerDirection.Right
footView?.addGestureRecognizer(swipLeft)
footView?.addGestureRecognizer(swipRight)
}
/**
选中日期
*/
func dayClick(sender: UIButton){
if((sender.imageForState(UIControlState.Normal) ) !=nil){
sender.setImage(nil,forState:UIControlState.Normal)
}else{
if(lastBtn != nil){
lastBtn!.setImage(nil,forState:UIControlState.Normal)
}
sender.setImage(UIImage(named:"btn_calendar"),forState: UIControlState.Normal)
lastBtn = sender
}
var obj:DateObject? = dayArray!.objectAtIndex(Int(sender.tag))as? DateObject
//println("this is day click,\(day!.year)\(day!.month) \(day!.day)")
var str:String = self.getDateString(obj!)
self.delegate?.selectStartDay(str)
}
func getDateString(obj:DateObject) -> String{
var str = String(format:"%ld/%02ld/%02ld",obj.year!,obj.month!,obj.day!)//"\(year)\(month)\(day)"
return str;
}
/**
*/
func createHeadView(){
var scw = self.bounds.width
headView = UIView(frame: CGRectMake(0,70))
headView?.backgroundColor =UIColor.whiteColor()
self.addSubview(headView!)
//var leftBtn:UIButton = UIButton(frame: CGRectMake(25,15,8,13))
var leftBtn:UIButton = UIButton(frame: CGRectMake(20,28,28))
//leftBtn.backgroundColor = UIColor.grayColor()
leftBtn.tag = 100;
var leftImg:UIImage = UIImage(named:"list_arrowback.png")!
//leftBtn.setBackgroundImage(leftImg,forState: UIControlState.Normal)
leftBtn.setImage(leftImg,forState:UIControlState.Normal)
leftBtn.imageEdgeInsets =UIEdgeInsetsMake(5,5,10,15)
leftBtn.addTarget(self,action:Selector("buttonAction:"),forControlEvents: UIControlEvents.TouchUpInside)
headView?.addSubview(leftBtn)
title = UILabel(frame: CGRectMake((scw-100)/2,100,20))
title!.text =DatetoString(date!)
title!.textAlignment =NSTextAlignment.Center
headView?.addSubview(title!)
var rightBtn:UIButton = UIButton(frame: CGRectMake(scw-25-18,28))
rightBtn.tag = 101;
//rightBtn.backgroundColor = UIColor.grayColor()
var rightImg:UIImage = UIImage(named:"list_arrowhead.png")!
//rightBtn.setBackgroundImage(rightImg,forState: UIControlState.Normal)
rightBtn.setImage(rightImg,forState:UIControlState.Normal)
rightBtn.imageEdgeInsets =UIEdgeInsetsMake(10,5)
rightBtn.addTarget(self,forControlEvents: UIControlEvents.TouchUpInside)
headView?.addSubview(rightBtn)
var i:CGFloat = 0
for i ; i<7 ;i++ {
var w:CGFloat = ((CGFloat)(scw-40)/7)
var off_x = i*w + 20
var lal:UILabel = UILabel(frame: CGRectMake(off_x,60,w,10))
lal.textAlignment =NSTextAlignment.Center
switch i {
case 0:
lal.text ="日"
case 1:
lal.text ="一"
case 2:
lal.text ="二"
case 3:
lal.text ="三"
case 4:
lal.text ="四"
case 5:
lal.text ="五"
case 6:
lal.text ="六"
default:
""
}
headView?.addSubview(lal)
}
}
/**
显示上一个月跟下一个月
*/
func buttonAction(sender: UIButton)
{
//println("btn click tag is \(sender.tag)")
if(sender.tag == 100){
preMonth()
}else{
nextMonth()
}
}
/**
日期转为字符串
:param: date 日期
:returns: 日期字符串
*/
func DatetoString(date:NSDate)->String
{
var outputFormatter:NSDateFormatter = NSDateFormatter()
var timezone:NSTimeZone = NSTimeZone(name: "Asia/Shanghai")!
outputFormatter.timeZone = timezone
outputFormatter.dateFormat ="yyyy年MM月"
var str:String = outputFormatter.stringFromDate(date)
return str;
}
/**
当前日期前一个月
returns:前一个月
*/
func preMonth(){
var calendar:NSCalendar =NSCalendar.currentCalendar()
var dayComponents:NSDateComponents = calendar.components((NSCalendarUnit.CalendarUnitYear |NSCalendarUnit.CalendarUnitMonth),fromDate: date!)
dayComponents.month -= 1;
var preDate:NSDate = calendar.dateFromComponents(dayComponents)!
date = preDate
num = date!.numberOfDaysInMonth()
lastDate =date!.monthLastDay()
if((footView) != nil){
footView?.removeFromSuperview()
createFootView()
}
title?.text = DatetoString(preDate)
}
/**
当前日期下一个月
returns:下一个月
*/
func nextMonth(){
date!)
dayComponents.month += 1;
var nextDate:NSDate = calendar.dateFromComponents(dayComponents)!
date = nextDate
footView?.removeFromSuperview()
createFootView()
}
title?.text = DatetoString(nextDate)
}
}