swift 日曆練習

2021-07-05 08:52:29 字數 3451 閱讀 5519

不喜歡碼字的程式設計師有沒有,寧願多敲兩行**,也不願意多寫兩個字。

import foundation

import uikit

@objc(dateobject)

class dateobject: nsobject

import foundation

extension

nsdate

return week

}/**

當前日期所在月份有多少天

returns:月份天數 */

func numberofdaysinmonth()->nsinteger

/**

當前日期所在月份第一天

returns:月份第一天 */

func monthfirstday() ->nsdate

/**

當前日期所在月份最後一天

returns:月份最後一天 */

func monthlastday() ->nsdate}//

//  mycalendar.swift

//  cooltalk

////  created by joesrain on 15/9/16.

//import uikit

import foundation

@objc(calendarjoes)

class calendarjoes:uiview

required

init(coder adecoder: nscoder)

func initui()

/**

建立日曆具體日期

*/func createfootview()

for j; jweek = 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), cgfloat(width), 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)else}}

}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!)

varswipleft:uiswipegesturerecognizer=uiswipegesturerecognizer(target:

self

, action:

"nextmonth")

varswipright:uiswipegesturerecognizer=uiswipegesturerecognizer(target:

self

, action:

"premonth")

swipleft.direction=uiswipegesturerecognizerdirection.left

swipright.direction=uiswipegesturerecognizerdirection.right

footview?.addgesturerecognizer(swipleft)

footview?.addgesturerecognizer(swipright) }

/**

選中日期

*/func dayclick(sender: uibutton)else

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)

//println("this is day click,\(str)")

}func getdatestring(obj:dateobject) -> string

/**

新增年月跟星期顯示

*/func createheadview()

headview?.addsubview(lal)}}

/**

顯示上乙個月跟下乙個月

*/func buttonaction(sender: uibutton)

else

}/**

日期轉為字串

:param: date 日期

:returns: 日期字串

*/func datetostring(date:nsdate)->string

/**

當前日期前乙個月

returns:前乙個月

*/func premonth()

title?.text = datetostring(predate)

}/**

當前日期下乙個月

returns:下乙個月

*/func nextmonth()

title?.text = datetostring(nextdate)}}

swift新增事件到系統日曆

首先我們要匯入 import eventkit 框架 獲取日曆事件許可權 let eventstore ekeventstore.init weak var weakself self eventstore.requestaccess to ekentitytype.event,completion...

Swift 演算法練習

編寫乙個程式,能交換兩個變數的數值 例如 變數a值為20,變數b值為30,呼叫函式後,a的值變為30,b 的值變為20 答案 func swap a inout int b inout int var x 20 y 30 swap a x b y print x,y 編寫乙個程式,求1 2 3 4 ...

資料結構 樹的遍歷(遞迴遍歷練習)

樹的遍歷 練習 include include include typedef struct treenodetreenode,treenodepointer 計算葉子節點的數目 void leafcount treenodepointer root,int num 遍歷左子樹 leafcount ...