Advertisement
Guest User

Compass, lock and swipe

a guest
Dec 2nd, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 10.71 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  papp
  4. //
  5. //  Created by Marco Lemmens on 26-11-15.
  6. //  Copyright © 2015 Fontys. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import GoogleMaps
  11. import CoreLocation
  12. import Foundation
  13.  
  14. class CompassViewController: UIViewController, CLLocationManagerDelegate {
  15.    
  16.     @IBOutlet weak var mapView: GMSMapView!
  17.     @IBOutlet weak var compassImage: UIImageView!
  18.     @IBOutlet weak var planeImage: UIImageView!
  19.     @IBOutlet weak var lockImage: UIImageView!
  20.    
  21.     // force images
  22.    
  23.     @IBOutlet weak var force1: UIImageView!
  24.     @IBOutlet weak var force2: UIImageView!
  25.     @IBOutlet weak var force3: UIImageView!
  26.     @IBOutlet weak var force4: UIImageView!
  27.     @IBOutlet weak var force5: UIImageView!
  28.     @IBOutlet weak var force6: UIImageView!
  29.     @IBOutlet weak var force7: UIImageView!
  30.    
  31.    
  32.     //
  33.    
  34.     var lm:CLLocationManager!
  35.     let locationManager = CLLocationManager()
  36.     var currentLat: Double = 0.0
  37.     var currentLon: Double = 0.0
  38.     var previousDegrees: Double = 0.0
  39.     var locked: Bool = false
  40.     var forceTimer: NSTimer!
  41.     var currentForce: Int = 0
  42.     var forceUp: Bool = true
  43.     var planeLocation: CGPoint = CGPoint(x: 0, y: 0)
  44.     var initY: CGFloat = 0
  45.     var startTime: CFAbsoluteTime = 0
  46.    
  47.     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  48.        
  49.         if locked == true {
  50.            
  51.             forceTimer.invalidate()
  52.            
  53.             startTime = CFAbsoluteTimeGetCurrent()
  54.            
  55.             let touch = touches.first
  56.            
  57.             planeLocation = (touch?.locationInView(self.view))!
  58.            
  59.             planeImage.center.y = planeLocation.y
  60.            
  61.         }
  62.     }
  63.    
  64.     override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
  65.        
  66.         if locked == true {
  67.            
  68.             let touch = touches.first
  69.            
  70.             planeLocation = (touch?.locationInView(self.view))!
  71.            
  72.             planeImage.center.y = planeLocation.y
  73.            
  74.         }
  75.  
  76.        
  77.     }
  78.    
  79.     override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
  80.        
  81.         if locked == true {
  82.            
  83.             let speed = (CFAbsoluteTimeGetCurrent() - startTime) * 2.5
  84.            
  85.             print(speed)
  86.            
  87.             if speed < 0.5 {
  88.            
  89.             UIView.animateWithDuration(speed, delay: 0, options: .CurveLinear, animations: {
  90.                
  91.                 self.planeImage.center.y = -120
  92.                
  93.                 }, completion: { finished in
  94.                    
  95.                    
  96.                    
  97.                     print("klaar is kees")
  98.                     let refreshAlert = UIAlertController(title: "Verstuurd!", message: "Je vliegtuigje is verstuurd", preferredStyle: UIAlertControllerStyle.ActionSheet)
  99.                    
  100.                     refreshAlert.addAction(UIAlertAction(title: "Doorgaan", style: .Default, handler: { (action: UIAlertAction!) in
  101.                        
  102.                        
  103.                         self.unlocked()
  104.                        
  105.                        
  106.                     }))
  107.                    
  108.                     self.presentViewController(refreshAlert, animated: true, completion: nil)
  109.                    
  110.                    
  111.             })
  112.            
  113.             print("Voert einde uit")
  114.            
  115.             }
  116.            
  117.             else {
  118.                
  119.                 unlocked()
  120.                
  121.             }
  122.  
  123.            
  124.         }
  125.        
  126.        
  127.     }
  128.    
  129.            
  130.        
  131.        
  132.        
  133.    
  134.    
  135.     override func shouldAutorotate() -> Bool {
  136.         return false
  137.     }
  138.    
  139.     override func viewDidLoad() {
  140.         super.viewDidLoad()
  141.        
  142.         print(planeImage.center.y)
  143.        
  144.         initY = planeImage.center.y
  145.        
  146.         force1.alpha = 0
  147.         force2.alpha = 0
  148.         force3.alpha = 0
  149.         force4.alpha = 0
  150.         force5.alpha = 0
  151.         force6.alpha = 0
  152.         force7.alpha = 0
  153.        
  154.        
  155.         planeImage.userInteractionEnabled = true
  156.         let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "singleTapping:")
  157.         singleTap.numberOfTapsRequired = 1
  158.         planeImage.addGestureRecognizer(singleTap)
  159.        
  160.        
  161.        
  162.        
  163.        
  164.         self.locationManager.delegate = self
  165.         self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
  166.         self.locationManager.requestWhenInUseAuthorization()
  167.         self.locationManager.startUpdatingLocation()
  168.        
  169.         lm = CLLocationManager()
  170.         lm.delegate = self
  171.         lm.startUpdatingHeading()
  172.        
  173.        
  174.         let camera = GMSCameraPosition.cameraWithLatitude(self.currentLat,
  175.             longitude: self.currentLon, zoom: 7, bearing: 30, viewingAngle: 0)
  176.        
  177.         mapView.camera = camera
  178.         mapView.mapType = kGMSTypeNormal
  179.         mapView.myLocationEnabled = true
  180.  
  181.        
  182.     }
  183.    
  184.     func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
  185.        
  186.         CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) ->
  187.             Void in
  188.            
  189.             if error != nil{
  190.                 print("Error: " + (error!.localizedDescription))
  191.                 return
  192.             }
  193.            
  194.             if placemarks!.count > 0{
  195.                
  196.                 let pm = placemarks![0]
  197.                 self.currentLat = Double((pm.location?.coordinate.latitude)!)
  198.                
  199.                 self.currentLon = Double((pm.location?.coordinate.longitude)!)
  200.                
  201.                 self.updateMapCenter()
  202.                
  203.             }
  204.            
  205.         })
  206.     }
  207.    
  208.     func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
  209.        
  210.         if self.locked == false{
  211.        
  212.         let degrees = newHeading.magneticHeading
  213.        
  214.         let newDegrees = CGFloat(degrees) / 180.0 * CGFloat(M_PI)
  215.        
  216.         self.mapView.animateToBearing(degrees)
  217.            
  218.         UIView.animateWithDuration(0.3, delay: 0.1, options: .CurveEaseOut, animations: {
  219.            
  220.             self.compassImage.transform = CGAffineTransformMakeRotation(CGFloat(newDegrees * -1))
  221.            
  222.            
  223.                 }, completion: { finished in
  224.             })
  225.            
  226.        
  227.         }
  228.        
  229.     }
  230.    
  231.    
  232.     func updateMapCenter(){
  233.        
  234.         let location = CLLocationCoordinate2D(latitude: self.currentLat, longitude: self.currentLon)
  235.         mapView.animateToLocation(location)
  236.     }
  237.    
  238.     func singleTapping(recognizer: UIGestureRecognizer) {
  239.         if self.locked == false{
  240.             self.locked = true
  241.             self.lockImage.image = UIImage(named: "locked.png")
  242.             self.mapView.userInteractionEnabled = false
  243.             self.mapView.myLocationEnabled = false
  244.            
  245.             self.forceTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("animateForce"), userInfo: nil, repeats: true)
  246.            
  247.         }
  248.        
  249.         else{
  250.  
  251.             unlocked()
  252.  
  253.            
  254.         }
  255.     }
  256.    
  257.    
  258.     func unlocked(){
  259.        
  260.         self.locked = false
  261.         self.lockImage.image = UIImage(named: "unlocked.png")
  262.         self.lm.startUpdatingHeading()
  263.         self.forceTimer.invalidate()
  264.         self.currentForce = 0
  265.         self.forceUp = true
  266.         appendForceImages(0)
  267.         self.mapView.userInteractionEnabled = true
  268.         self.mapView.myLocationEnabled = true
  269.        
  270.         UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: {
  271.            
  272.             self.planeImage.center.y = self.initY + 140
  273.            
  274.             }, completion: { finished in
  275.         })
  276.        
  277.     }
  278.    
  279.     func animateForce(){
  280.        
  281.         if self.currentForce == 1{
  282.            
  283.             self.forceUp = true
  284.         }
  285.        
  286.         if self.currentForce == 7 {
  287.            
  288.             self.forceUp = false
  289.            
  290.         }
  291.        
  292.         if self.forceUp == true{
  293.            
  294.             self.currentForce++
  295.            
  296.         }
  297.         else{
  298.             self.currentForce--
  299.         }
  300.        
  301.        
  302.         appendForceImages(self.currentForce)
  303.        
  304.     }
  305.    
  306.    
  307.     func appendForceImages(forceAmount: Int){
  308.        
  309.        
  310.         switch forceAmount
  311.         {
  312.         case 1:
  313.             self.force1.alpha = 1
  314.             self.force2.alpha = 0
  315.             self.force3.alpha = 0
  316.             self.force4.alpha = 0
  317.             self.force5.alpha = 0
  318.             self.force6.alpha = 0
  319.             self.force7.alpha = 0
  320.         case 2:
  321.             self.force1.alpha = 1
  322.             self.force2.alpha = 1
  323.             self.force3.alpha = 0
  324.             self.force4.alpha = 0
  325.             self.force5.alpha = 0
  326.             self.force6.alpha = 0
  327.             self.force7.alpha = 0
  328.         case 3:
  329.             self.force1.alpha = 1
  330.             self.force2.alpha = 1
  331.             self.force3.alpha = 1
  332.             self.force4.alpha = 0
  333.             self.force5.alpha = 0
  334.             self.force6.alpha = 0
  335.             self.force7.alpha = 0
  336.         case 4:
  337.             self.force1.alpha = 1
  338.             self.force2.alpha = 1
  339.             self.force3.alpha = 1
  340.             self.force4.alpha = 1
  341.             self.force5.alpha = 0
  342.             self.force6.alpha = 0
  343.             self.force7.alpha = 0
  344.         case 5:
  345.             self.force1.alpha = 1
  346.             self.force2.alpha = 1
  347.             self.force3.alpha = 1
  348.             self.force4.alpha = 1
  349.             self.force5.alpha = 1
  350.             self.force6.alpha = 0
  351.             self.force7.alpha = 0
  352.         case 6:
  353.             self.force1.alpha = 1
  354.             self.force2.alpha = 1
  355.             self.force3.alpha = 1
  356.             self.force4.alpha = 1
  357.             self.force5.alpha = 1
  358.             self.force6.alpha = 1
  359.             self.force7.alpha = 0
  360.         case 7:
  361.             self.force1.alpha = 1
  362.             self.force2.alpha = 1
  363.             self.force3.alpha = 1
  364.             self.force4.alpha = 1
  365.             self.force5.alpha = 1
  366.             self.force6.alpha = 1
  367.             self.force7.alpha = 1
  368.         default:
  369.             self.force1.alpha = 0
  370.             self.force2.alpha = 0
  371.             self.force3.alpha = 0
  372.             self.force4.alpha = 0
  373.             self.force5.alpha = 0
  374.             self.force6.alpha = 0
  375.             self.force7.alpha = 0
  376.            
  377.         }
  378.        
  379.     }
  380.    
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement