子视图动画受mapview的影响

时间:2016-04-13 16:01:25

标签: ios swift dictionary uiview mkmapview

我正在使用swift,我正在开展一个项目,在那里我必须显示一个可更改位置的可拖动地图。在地图下方我有子视图,它有一个按钮,按钮点击子视图将出现,在同一个按钮点击我将消失。

但问题是有时它的工作正常,有时这个观点会下降而不会出现在屏幕上。特别是当我使用按钮标题更改代码时。

class LocationMAP: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    @IBOutlet weak var locationLabel: UILabel!
    @IBOutlet weak var selectAnyoneButton: UIButton!
    @IBOutlet weak var selectingView: UIView!
    var changingText:Bool = false


    @IBOutlet weak var map: MKMapView!

    var locationManger = CLLocationManager()
    let geoCoder = CLGeocoder()
    var myLocation: CLLocation!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManger.delegate = self
        locationManger.desiredAccuracy = kCLLocationAccuracyBest
        locationManger.requestWhenInUseAuthorization()
        locationManger.startUpdatingLocation()

        if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){

        }

        self.map.showsUserLocation = true
        self.map.delegate = self
        self.map.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
        let location = CLLocationCoordinate2DMake(20.59368, 78.96288)

        let span = MKCoordinateSpanMake(0.2, 0.2)
        _ = MKCoordinateRegionMake(location, span)

        let annotation = MKPointAnnotation()
        annotation.coordinate = (location)

        selectAnyoneButton.setTitle("Submit", forState: .Normal)


       }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //MARK:- MapView Delegates


    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .Authorized, .AuthorizedWhenInUse:
            manager.startUpdatingLocation()
            self.map.showsUserLocation = true
        default: break
        }
    }


        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.myLocation = locations.last! as CLLocation

        let userLocation:CLLocation = locations.last!
        let long = userLocation.coordinate.longitude
        let lat = userLocation.coordinate.latitude
        print(long , lat)
//        locationManger.stopUpdatingLocation()

        self.map.centerCoordinate = myLocation.coordinate
        let reg = MKCoordinateRegionMakeWithDistance(myLocation.coordinate, 1500, 1500)
        self.map.setRegion(reg, animated: true)
        geoCode(myLocation)
}


    func geoCode(location : CLLocation!){
                geoCoder.cancelGeocode()
        self.locationManger.stopUpdatingLocation()
   geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemark, error) -> Void in


            guard let placeMarks = placemark as [CLPlacemark]! else {
                return
            }

            let loc: CLPlacemark = placeMarks[0]
            let addressDict : [NSString:NSObject] = loc.addressDictionary as! [NSString: NSObject]
            let addrList = addressDict["FormattedAddressLines"] as! [String]
            let address = (addrList.joinWithSeparator(", "))
            self.locationLabel.text = address
            let lat = loc.location!.coordinate.latitude
            let long = loc.location!.coordinate.longitude

            print(lat , long)

            SharedPreferenceManager.sharedInstance.userLatitude = lat
            SharedPreferenceManager.sharedInstance.userLongitude = long
            SharedPreferenceManager.sharedInstance.userAddress = address
            })

    }


       func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print(error.localizedDescription)
    }


    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        let location = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)

        geoCode(location)



        self.map.removeAnnotations(mapView.annotations)
        let annotation = MKPointAnnotation()
        annotation.coordinate = map.centerCoordinate
        annotation.title = "title"
        annotation.subtitle = "subtitle"
        self.map.addAnnotation(annotation)
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        let annotationView = MKPinAnnotationView()
        if #available(iOS 9.0, *) {
            annotationView.pinTintColor = UIColor.blueColor()
            annotationView.center = CGPointMake(160, 200)

        } else {
        }
        return annotationView
    }


    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState

        newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {

        if (newState == MKAnnotationViewDragState.Starting) {
            view.dragState = MKAnnotationViewDragState.Dragging
        }
        else if (newState == MKAnnotationViewDragState.Ending || newState == MKAnnotationViewDragState.Canceling){
            view.dragState = MKAnnotationViewDragState.None
        }
    }

    //MARK:- Button Action Methods

    @IBOutlet weak var downBtn: UILabel!
    @IBAction func chooseButtonAction(sender: AnyObject) {
        if (changingText == false) {
            let newCenter:CGPoint = CGPointMake(selectingView.center.x, selectingView.center.y - 230)
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.55)
            selectingView.center = newCenter
            UIView.commitAnimations()
           selectAnyoneButton.setTitle("Select a service", forState: .Normal)
            changingText = true
        } else {
            let newCenter:CGPoint = CGPointMake(selectingView.center.x, selectingView.center.y + 230)
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.55)
            selectingView.center = newCenter
            UIView.commitAnimations()
           selectAnyoneButton.setTitle("Submit", forState: .Normal)
            changingText = false
        }        
    }

1 个答案:

答案 0 :(得分:0)

在按钮的操作方法中,添加:

super.bringSubviewToFront(UIView)
到了最后。 我假设你的观点是superview的直接孩子。