元旦凑了年假休息了五天,出去玩了一趟,耽搁了不少天,近几天会多写几个项目补上来,尽量赶在过年前完成所有项目。今天完成三个:
一、SlideView
仿照原作者做的一个滑动三屏View,第一个和第三个都是截图,中间一个相机界面使用了AVFondation中的API,来自定义相机界面,相机界面在界面加viewdidappear和viewdiddisappear中来初始化加载和关闭,难点主要就是加载相机界面这块,不太熟悉,写起来稍微花费点时间(考虑内存占用的话,可以在根据scrollView的偏离度来判断哪个view显示哪个隐藏,能一定程度优化内存占用),代码如下:
func setupCamera() {
self.session.sessionPreset = AVCaptureSessionPresetHigh
var input = AVCaptureDeviceInput()
do {
input = try AVCaptureDeviceInput(device: device)
} catch {
//error
}
let output = AVCaptureMetadataOutput()
//设置响应区域
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
这个比较简单,就是一个collectionView,storyboard上将界面搭好,cell关联到自定义类上,在主界面的controller里处理datasource和delegate就好,写着写着发现,基本OC有的方法Swift都有,只不过很多原来的“[ ]”调用变成“.”调用了,当然也有不少名字变了的,这就得查文档了,白色状态栏用下面的方法即可
overridevarpreferredStatusBarStyle :UIStatusBarStyle{
returnUIStatusBarStyle.lightContent
}
固定的数据全部放在一个数组,查看,修改都会比较容易,imageInfo和前面PlayVideo项目里的video一样,是定义的一个结构体
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
这个获取位置介于之前工作的项目需要,经常用到,其实对这块还是很熟悉的,不过以前都只是获取经纬度。info.plist必须要增加两个key:NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription,并且添加对应的string字段用于请求定位时弹窗提示告诉用户使用定位的原因。并且声明那里需要增加CLLocationManagerDelegate
请求定位代码:
func loadLocation() {
locationManager.delegate = self
//定位方式
locationManager.desiredAccuracy = kCLLocationAccuracyBest
let system = UIDevice.current.systemVersion
print(system)
//iOS8.0以上才可以使用
if((system as NSString).doubleValue >= 8.0){
//始终允许访问位置信息
locationManager.requestAlwaysAuthorization()
//使用应用程序期间允许访问位置数据
locationManager.requestWhenInUseAuthorization()
}
//开启定位
locationManager.startUpdatingLocation()
}
开始定位后,会回调locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])这个方法,在这个方法中获取相关的位置信息,主要获取方法如下:
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))
}
获取到经纬度后可以转换为具体地理位置,方法可见代码,包括,省市区街道等