设为首页 加入收藏

TOP

Swift-----类型转换 、 嵌套类型 、 扩展 、 协议 、 访问控制(七)
2017-10-10 12:17:31 】 浏览:2037
Tags:Swift----- 类型 转换 扩展 协议 访问 控制
Timer:
"), userInfo: nil, repeats: true) }else { timer?.invalidate() timer = nil } setSettingButtonsEnabled(!newValue) } } //计时方法每一秒调用改变timeLable的显示,直到计时结束 func updateTimer(timer:NSTimer){ remainingSeconds-- if remainingSeconds<=0 { isCounting = false var alertVC = UIAlertController(title: "时间到", message: "", preferredStyle:.Alert) let action = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: { (action) -> Void in println("被按了") }) alertVC.addAction(action) self.presentViewController(alertVC, animated: true, completion: nil) } } //打开或关闭设置时间的按钮 func setSettingButtonsEnabled(enable:Bool) { for button in timeButtons { button.enabled = enable button.alpha = enable ?1.0 : 0.3 } resetButton.enabled = enable resetButton.alpha = enable ?1.0 : 0.3 let title = enable ? "启动" : "停止" startButton.setTitle(title, forState: .Normal) } 此时再实现startButtonTapped:和resetButtonTapped:方法,代码如下所示: func startButtonTapped(button:UIButton){ isCounting = !isCounting } func resetButtonTapped(){ remainingSeconds = 0 } 步骤四:后台计时,推送消息 IOS8对程序的后台运行有很好的支持,当计时器退到后台,不用做任何操作就会自动后台计时,但是当计时完成用户并不知道计时已经完成,所以这个时候就需要使用IOS系统提供的本地消息推送功能,首先需要在程序加载成功的方法中请求用户同意接受消息推送,代码如下所示: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. self.window = UIWindow(frame: UIScreen.mainScreen().bounds) //可选链,设置背景颜色 self.window?.backgroundColor = UIColor.whiteColor() self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() //请求用户获取消息推送权限 let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge,categories: nil) application.registerUserNotificationSettings(settings) return true } 其次从计时开始就添加消息推送,当计时完成时推送消息,代码如下所示: func startButtonTapped(button:UIButton){ isCounting = !isCounting if isCounting { //添加消息推送 createAndFireLocalNotificationAfterSeconds(Double(remainingSeconds)) }else { //计时结束退出消息推送 UIApplication.sharedApplication().cancelAllLocalNotifications() } } //添加消息推送方法 func createAndFireLocalNotificationAfterSeconds(seconds:NSTimeInterval) { UIApplication.sharedApplication().cancelAllLocalNotifications() let notification = UILocalNotification() //推送时间 notification.fireDate = NSDate(timeIntervalSinceNow: seconds) //推送时区 notification.timeZone = NSTimeZone.systemTimeZone() //推送通知的内容 notification.alertBody = "Time is up!" UIApplication.sharedApplication().scheduleLocalNotification(notification) } 运行程序消息推送效果如图-10所示: 图-10 3.4 完整代码 本案例中,AppDelegate.swift文件中完整代码如下所示: import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. self.window = UIWindow(frame: UIScreen.mainScreen().bounds) //可选链,设置背景颜色 self.window?.backgroundColor = UIColor.whiteColor() self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() //请求用户获取消息推送权限 let setti
首页 上一页 4 5 6 7 下一页 尾页 7/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CocoaPods的安装和使用那些事(Xc.. 下一篇Swift语法笔记

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目