新聞中心
本篇內(nèi)容主要講解“swift怎么實(shí)現(xiàn)在線天氣預(yù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“swift怎么實(shí)現(xiàn)在線天氣預(yù)”吧!
實(shí)現(xiàn)功能簡單,但知識(shí)點(diǎn)用到很多,比如微信搜索地理位置等。
// ViewController.swift // WeatherApp // 文啟領(lǐng)航培訓(xùn) // bjflexedu.com // qq:376610000 import UIKit import CoreLocation class ViewController: UIViewController,CLLocationManagerDelegate { @IBOutlet var cityName : UILabel @IBOutlet var icon : UIImageView @IBOutlet var tempTxt : UILabel @IBOutlet var busy : UIActivityIndicatorView @IBOutlet var messageInfo : UILabel let locationManage:CLLocationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() busy.startAnimating() let img = UIImage(named: "background.jpg") self.view.backgroundColor = UIColor(patternImage: img) locationManage.delegate = self locationManage.desiredAccuracy = kCLLocationAccuracyBest locationManage.requestAlwaysAuthorization() locationManage.startUpdatingLocation() } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!){ var location = locations[locations.count-1] as CLLocation if location.horizontalAccuracy > 0{ let latitude = location.coordinate.latitude let longitude = location.coordinate.longitude self.updateWeatherData(latitude,longitude:longitude) locationManage.stopUpdatingLocation() } } func updateWeatherData(latitude:CLLocationDegrees,longitude:CLLocationDegrees){ let url = "http://api.openweathermap.org/data/2.5/weather" let params = ["lat":latitude,"lon":longitude,"cnt":0] let manage = AFHTTPRequestOperationManager() let success = { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in //println(response) self.busy.stopAnimating() self.busy.hidden = true self.messageInfo.text = nil //name let jsonResult:NSDictionary = response as NSDictionary self.cityName.text = jsonResult["name"]? as String var tempNum:Double if let tempValue = jsonResult["main"]?["temp"]? as? Double{ tempNum = round(tempValue - 273.15) self.tempTxt.text = "\(tempNum) " } let idCon = (jsonResult["weather"]? as NSArray)[0]?["id"]? as? Int let nowTime = NSDate().timeIntervalSince1970 let sunrise = jsonResult["sys"]?["sunrise"]? as? Double let sunset = jsonResult["sys"]?["sunset"]? as? Double var nightTime = false//true 表示黑夜;false表示白天 if nowTime < sunrise || nowTime > sunset{ nightTime = true }else{ nightTime = false } self.updateIcon(idCon!,nightTime:nightTime) } let failure = { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in println("\(error)") self.messageInfo.text = "遠(yuǎn)程數(shù)據(jù)取不到" } manage.GET(url, parameters: params, success: success, failure: failure) } func updateIcon(condition:Int,nightTime:Bool){ if (condition < 300) { if nightTime { self.icon.p_w_picpath = UIImage(named: "tstorm1_night") } else { self.icon.p_w_picpath = UIImage(named: "tstorm1") } } // Drizzle else if (condition < 500) { self.icon.p_w_picpath = UIImage(named: "light_rain") } // Rain / Freezing rain / Shower rain else if (condition < 600) { self.icon.p_w_picpath = UIImage(named: "shower3") } // Snow else if (condition < 700) { self.icon.p_w_picpath = UIImage(named: "snow4") } // Fog / Mist / Haze / etc. else if (condition < 771) { if nightTime { self.icon.p_w_picpath = UIImage(named: "fog_night") } else { self.icon.p_w_picpath = UIImage(named: "fog") } } // Tornado / Squalls else if (condition < 800) { self.icon.p_w_picpath = UIImage(named: "tstorm3") } // Sky is clear else if (condition == 800) { if (nightTime){ self.icon.p_w_picpath = UIImage(named: "sunny_night") // sunny night? } else { self.icon.p_w_picpath = UIImage(named: "sunny") } } // few / scattered / broken clouds else if (condition < 804) { if (nightTime){ self.icon.p_w_picpath = UIImage(named: "cloudy2_night") } else{ self.icon.p_w_picpath = UIImage(named: "cloudy2") } } // overcast clouds else if (condition == 804) { self.icon.p_w_picpath = UIImage(named: "overcast") } // Extreme else if ((condition >= 900 && condition < 903) || (condition > 904 && condition < 1000)) { self.icon.p_w_picpath = UIImage(named: "tstorm3") } // Cold else if (condition == 903) { self.icon.p_w_picpath = UIImage(named: "snow5") } // Hot else if (condition == 904) { self.icon.p_w_picpath = UIImage(named: "sunny") } // Weather condition is not available else { self.icon.p_w_picpath = UIImage(named: "dunno") } } func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){ self.messageInfo.text = "地理位置信息找不到" println("地理位置信息找不到\(error)") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
到此,相信大家對(duì)“swift怎么實(shí)現(xiàn)在線天氣預(yù)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:swift怎么實(shí)現(xiàn)在線天氣預(yù)-創(chuàng)新互聯(lián)
當(dāng)前URL:http://fisionsoft.com.cn/article/ejoph.html