gpt4 book ai didi

ios - Subview动画受mapview影响

转载 作者:行者123 更新时间:2023-11-30 13:30:42 25 4
gpt4 key购买 nike

我正在使用 swift,我正在开发一个项目,我必须在其中显示一个可拖动的 map ,并更改位置。在 map 下方,我有 subview ,它有一个按钮,单击按钮, subview 将出现,单击同一按钮,我将消失。

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

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
}
}

最佳答案

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

super.bringSubviewToFront(UIView)

快到最后了。我假设您所讨论的 View 是 super View 的直接 subview 。

关于ios - Subview动画受mapview影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com