中和软件技术培训

上海中和软件公司培训专用博客

MapKit ScrollView UserNotification 部分的源代码

MapKit ScrollView UserNotification 部分的源代码


//
//  ViewController.swift
//  MapDemo
//
//  Created by Hu on 2017/10/9.
//  Copyright © 2017年 Hu. All rights reserved.
//

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController , CLLocationManagerDelegate,MKMapViewDelegate {
    
    @IBOutlet weak var mapLocation: MKMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //创建一个MKCoordinateSpan对象,设置地图的范围(越小越精确)
        //获得了Location之后的设定
        let latDelta = 0.05
        let longDelta = 0.05
        let currentLocationSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        //let center:CLLocation = locations.first!
        //使用自定义位置
        let center:CLLocation = CLLocation(latitude: 32.029171, longitude: 118.788231)
        let currentRegion:MKCoordinateRegion = MKCoordinateRegion(center: center.coordinate,                                                                  span: currentLocationSpan)
        //设置显示区域
        self.mapLocation.setRegion(currentRegion, animated: true)
        mapLocation.delegate = self
        
        //创建一个大头针对象
        let objectAnnotation = MKPointAnnotation()
        //设置大头针的显示位置
        objectAnnotation.coordinate =
            CLLocation(latitude: 32.029171, longitude: 118.788231).coordinate
        //设置点击大头针之后显示的标题
        objectAnnotation.title = "南京夫子庙"
        //设置点击大头针之后显示的描述
        objectAnnotation.subtitle = "南京市秦淮区秦淮河北岸中华路"
        //添加大头针
        self.mapLocation.addAnnotation(objectAnnotation)
        
    }
    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if (pinView == nil ){
            pinView = MKPinAnnotationView()
        }
        let size = 50
        pinView!.canShowCallout = true
        let usrImage = UIImageView(frame: CGRect(x: 0, y: 0, width: size, height: size))
        usrImage.image = UIImage(named:"Monkey")
            pinView!.leftCalloutAccessoryView = usrImage
        pinView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            return pinView
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

注意:一定要添加MapKitFramework

 

 

//
//  ViewController.swift
//  UIScrollView
//
//  Created by Hu on 2017/10/9.
//  Copyright © 2017年 Hu. All rights reserved.
//

import UIKit

class ViewController: UIViewController , UIScrollViewDelegate{


    @IBOutlet weak var _scrollview: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        _scrollview.delegate = self
        _scrollview.maximumZoomScale = 2.0
        _scrollview.minimumZoomScale = 0.5
 
        let imageView = UIImageView()
        imageView.image = UIImage(named:"MyPic")
        let imgW = imageView.image?.size.width
        let imgH = imageView.image?.size.height
        imageView.frame = CGRect(x: 0, y: 0, width: imgW!, height: imgH!)
        _scrollview.addSubview(imageView)
        _scrollview.contentSize = (imageView.image?.size)!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        print("scrollViewWillBeginDragging")
    }
}

 

 

 

//
//  AppDelegate.swift
//  LocalNotification
//
//  Created by Hu on 2017/10/10.
//  Copyright © 2017年 Hu. All rights reserved.
//

import UIKit
import UserNotifications


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            granted, error in
            if granted {
                // 用户允许进行通知
            }
        }
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        // 1. 创建通知内容
        let content = UNMutableNotificationContent()
        content.title = "Time Interval Notification"
        content.body = "My first notification"
        // 2. 创建发送触发
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        // 3. 发送请求标识符
        let requestIdentifier = "com.onevcat.usernotification.myFirstNotification"
        // 4. 创建一个发送请求
        let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
        // 将请求添加到发送中心
        UNUserNotificationCenter.current().add(request) { error in
            if error == nil {
                print("Time Interval Notification scheduled: \(requestIdentifier)")
            }
        }
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

博文最后更新时间:


评论

  • ScottPloca

    Hello. http://jakshgy773733.us

  • Guestgoand

    guest test post <a href="http://gdhyuei23kol.com/">bbcode</a> <a href="http://gdhyuei23kol.com/">html</a> http://gdhyuei23kol.com/ simple

发表评论

博客统计

访问量:20371

博文总数:16 评论总数:9

原创14 翻译0 转载2