iOS 你不知道的新鲜事iOS 开发成长中心

Day6 - CoreLocation,CATransition

2016-09-10  本文已影响51人  Codepgq

效果图:压缩很厉害

效果图

<br />


本来是这样子的

<br />

1、UI:ImageView、VisualEffectView、Button、Label

布局如下

UI比较简单,不做介绍

1.1按钮布局,

override func layoutSubviews() {
        super.layoutSubviews()
        self.imageView?.frame = self.bounds
        self.titleLabel?.removeFromSuperview()
        self.titleLabel?.frame = self.bounds
        self.titleLabel?.textAlignment = NSTextAlignment.Center
        self.insertSubview(self.titleLabel!, aboveSubview: self.imageView!)
    }

<br />
<br />

2、定位、闭包、动画

<br />

2.1 定位、闭包

  • 1 创建一个类,继承:ViewController
  • 2 import CoreLocatioin
  • 3 定义一个CLLocationManager对象
  • 4 写一个方法,用于开启定位
  • 5 监听回调
  • 6 解析并返回
import UIKit
import CoreLocation

/*
 
 plist文件里面加上
 NSLocationAlwaysUsageDescription string 你在这里写的内容会提示在弹窗中
 NSLocationWhenInUseUsageDescription string 你在这里写的内容会提示在弹窗中
 **/

//闭包定义
typealias feedBackBlock = (message : String) -> ()

class LocationViewController: UIViewController , CLLocationManagerDelegate{
    
    var locationManager :CLLocationManager!
    var block = feedBackBlock?()
    var timer : NSTimer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    func startLocation(feedBack :feedBackBlock){
        block = feedBack
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
        
        //模拟定位获取成功
//        timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(self.send), userInfo: nil, repeats: true)
    }
    
    func send(){
        timer?.invalidate()
        timer = nil
        block!(message: "来自遥远的国度,手机可获得真实位置")
    }
    
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print(error)
    }
    
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
            
            self.locationManager.stopUpdatingLocation()
            
            if (error != nil) {
                print("Reverse geocoder failed with error" + error!.localizedDescription)
                return
            }
            if placemarks!.count > 0 {
                let pm = placemarks![0]
                self.displayLocationInfo(pm)
            } else {
                print("Problem with the data received from geocoder")
            }
        })
    }
    
    func displayLocationInfo(placemark: CLPlacemark?) {
        if let containsPlacemark = placemark {
            if block != nil {
                self.block!(message: containsPlacemark.name!)
            }
        }
        
    }
}

2.2 动画

startLocation { ( message) in
            print(message)
            self.address.text = message
            
            let cat = CATransition();
            cat.type = "cube"
            cat.duration = 1
            self.address.layer.addAnimation(cat, forKey: nil)
        }
  • 3 重写touchesBegan
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        
        let cat = CATransition();
        cat.type = "rippleEffect"
        cat.duration = 1
        view.layer.addAnimation(cat, forKey: nil)
        
        changeBackgroundImage()
    }
    
var item : Int = 1
    func changeBackgroundImage(){
        item += 1
        if item > 6 {
            item = 1
        }
        
        backgroundImage.image = UIImage(named: "c\(item).jpg")
    }

<br />
<br />

简单的介绍一下CATransition

type:动画类型:

kCATransitionFade 交叉淡化过渡
kCATransitionMoveIn 新视图移到旧视图上面
kCATransitionPush 新视图把旧视图推出去
kCATransitionReveal 将旧视图移开,显示下面的新视图
pageCurl 向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect 收缩效果,如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果

3 duration:动画时长
4 timingFunction:动画执行(先快后慢,先慢后快……)
5 subType:方向(fromLeft, fromRight, fromTop ,fromBottom)
6 startProgress:开始点 0-1取值
7 endProgress:结束点 0-1取值

OK,到这里功能就完成了,如果项目有需要的时候,就可以把自己创建的ViewController拿走,让后页面中继承他,就可以使用获取定位的方法了

<br /><br /><br />

Demo - Location

上一篇下一篇

猜你喜欢

热点阅读