Day6 - CoreLocation,CATransition

效果圖:壓縮很厲害

效果圖

<br />


本來是這樣子的

<br />

1、UI:ImageView、VisualEffectView、Button、Label

布局如下

UI比較簡單,不做介紹

1.1按鈕布局,

  • 1.創建一個類,繼承與UIButton

  • 2.重寫layoutSubviews

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 動畫

  • 1 把imageView、button、label拖線到ViewController中
  • 2 在button的點擊事件調用開始定位的方法
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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容