? ? ? ? 元旦湊了年假休息了五天,出去玩了一趟,耽擱了不少天,近幾天會(huì)多寫(xiě)幾個(gè)項(xiàng)目補(bǔ)上來(lái),盡量趕在過(guò)年前完成所有項(xiàng)目。今天完成三個(gè):
一、SlideView

? ? ? ? 仿照原作者做的一個(gè)滑動(dòng)三屏View,第一個(gè)和第三個(gè)都是截圖,中間一個(gè)相機(jī)界面使用了AVFondation中的API,來(lái)自定義相機(jī)界面,相機(jī)界面在界面加viewdidappear和viewdiddisappear中來(lái)初始化加載和關(guān)閉,難點(diǎn)主要就是加載相機(jī)界面這塊,不太熟悉,寫(xiě)起來(lái)稍微花費(fèi)點(diǎn)時(shí)間(考慮內(nèi)存占用的話(huà),可以在根據(jù)scrollView的偏離度來(lái)判斷哪個(gè)view顯示哪個(gè)隱藏,能一定程度優(yōu)化內(nèi)存占用),代碼如下:
func setupCamera() {
? ? ? ? self.session.sessionPreset = AVCaptureSessionPresetHigh
? ? ? ? var input = AVCaptureDeviceInput()
? ? ? ? do {
? ? ? ? ? ? ? ? input = try AVCaptureDeviceInput(device: device)
? ? ? ? ? ? ? } catch {
? ? ? ? ? ? ? ? ? ? ? ? ? //error
? ? ? ? ? ? ? ?}
? ? ? ? ? ?let output = AVCaptureMetadataOutput()
? ? ? ? ? //設(shè)置響應(yīng)區(qū)域
? ? ? ? ?output.rectOfInterest = CGRect(x:0, y:0, width:0, height:0)
? ? ? ? ?output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
? ? ? ? ?if session.canAddInput(input) {
? ? ? ? ? ? ? ? ? session.addInput(input)
? ? ? ? ? }
? ? ? ? ?if session.canAddOutput(output) {
? ? ? ? ? ? ? ? ? session.addOutput(output)
? ? ? ? ? }
? ? ? ? ? layer = AVCaptureVideoPreviewLayer(session: session)
? ? ? ? ? layer!.videoGravity = AVLayerVideoGravityResizeAspectFill
? ? ? ? ? layer!.frame = CGRect(x:0, y:0, width:UIScreen.main.bounds.size.width, height:UIScreen.main.bounds.size.height)
? ? ? ? ? self.view.layer.insertSublayer(self.layer!, at: 0)
? ? ? ? ? session.startRunning()
}
二、CarouselEffect

這個(gè)比較簡(jiǎn)單,就是一個(gè)collectionView,storyboard上將界面搭好,cell關(guān)聯(lián)到自定義類(lèi)上,在主界面的controller里處理datasource和delegate就好,寫(xiě)著寫(xiě)著發(fā)現(xiàn),基本OC有的方法Swift都有,只不過(guò)很多原來(lái)的“[ ]”調(diào)用變成“.”調(diào)用了,當(dāng)然也有不少名字變了的,這就得查文檔了,白色狀態(tài)欄用下面的方法即可
overridevarpreferredStatusBarStyle :UIStatusBarStyle{
? ? ? ? ?returnUIStatusBarStyle.lightContent
}
? ? ? ? 固定的數(shù)據(jù)全部放在一個(gè)數(shù)組,查看,修改都會(huì)比較容易,imageInfo和前面PlayVideo項(xiàng)目里的video一樣,是定義的一個(gè)結(jié)構(gòu)體
var data = [
? ? ? ?imageInfo(image: "bodyLine", title: "Training like this, #bodyline"),
? ? ? ?imageInfo(image: "cuteGirl", title: "Hello there, i miss u."),
? ? ? ?imageInfo(image: "jogging", title: "Seals Documentary"),
? ? ? ?imageInfo(image: "lightSword", title: "Dark Varder, #emoji"),
? ? ? ?imageInfo(image: "man", title: "I have no idea, bitch"),
? ? ? ?imageInfo(image: "sea", title: "I'm hungry, indeed."),
? ? ? ?imageInfo(image: "seaAnimal", title: "??????????")
]
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
? ? ? ? let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
? ? ? ? let imageInfo = data[indexPath.row]
? ? ? ? cell.cellImage.image = UIImage(named:imageInfo.image)
? ? ? ? cell.cellLabel.text = imageInfo.title
? ? ? ? return cell
}
三、GetLocation

? ? ? ? 這個(gè)獲取位置介于之前工作的項(xiàng)目需要,經(jīng)常用到,其實(shí)對(duì)這塊還是很熟悉的,不過(guò)以前都只是獲取經(jīng)緯度。info.plist必須要增加兩個(gè)key:NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription,并且添加對(duì)應(yīng)的string字段用于請(qǐng)求定位時(shí)彈窗提示告訴用戶(hù)使用定位的原因。并且聲明那里需要增加CLLocationManagerDelegate
請(qǐng)求定位代碼:
func loadLocation() {
? ? ? ? locationManager.delegate = self
? ? ? ?//定位方式
? ? ? ? locationManager.desiredAccuracy = kCLLocationAccuracyBest
? ? ? ? let system = UIDevice.current.systemVersion?
? ? ? ? print(system)
? ? ? ?//iOS8.0以上才可以使用
? ? ? ? if((system as NSString).doubleValue >= 8.0){
? ? ? ? //始終允許訪(fǎng)問(wèn)位置信息
? ? ? ? locationManager.requestAlwaysAuthorization()
? ? ? ? //使用應(yīng)用程序期間允許訪(fǎng)問(wèn)位置數(shù)據(jù)
? ? ? ? locationManager.requestWhenInUseAuthorization()
? ? ? ? }
? ? ? ?//開(kāi)啟定位
? ? ? ?locationManager.startUpdatingLocation()
}
開(kāi)始定位后,會(huì)回調(diào)locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])這個(gè)方法,在這個(gè)方法中獲取相關(guān)的位置信息,主要獲取方法如下:
let location:CLLocation = locations[locations.count-1]
if(location.horizontalAccuracy > 0){
? ? ? let lat = Double(String(format: "%.1f", location.coordinate.latitude))
? ? ? let long = Double(String(format: "%.1f", location.coordinate.longitude))
}
獲取到經(jīng)緯度后可以轉(zhuǎn)換為具體地理位置,方法可見(jiàn)代碼,包括,省市區(qū)街道等