-1

MKMapView如果文本字段中没有条目,我想通过UIVisualEffectsView在地图顶部使用来模糊,我也希望它可以显示,现在shouldChangeCharactersInRange当有人开始输入时它会被调用两次,而当用户删除所有文本时根本不会调用从UITextField. 这是我的代码

import UIKit
import MapKit
import Parse

class HomeAddressViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate {

let locationManager = CLLocationManager()

@IBOutlet var activityIndicator: UIActivityIndicatorView!
@IBOutlet var homeAddressField: UITextField!
@IBOutlet var homeAddressMap: MKMapView!
@IBOutlet var blurryMap: UIVisualEffectView! = UIVisualEffectView()
override func viewDidLoad() {
    super.viewDidLoad()

    activityIndicator.hidden = true

    homeAddressField.delegate = self

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

    blurAndUnblurMap()


}
func blurAndUnblurMap() {
    if homeAddressField.text?.characters.count == 0 {
        UIView.animateWithDuration(1, animations: {
            self.blurryMap.effect = UIBlurEffect(style: .Light)
        })
    } else {
        UIView.animateWithDuration(0.5, animations: {
            self.blurryMap.effect = nil
        })
    }
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    blurAndUnblurMap()
    return true
}
}
4

1 回答 1

1

你为什么不这样做呢?

homeAddressField.addTarget(self, "someFunction:", forControlEvents: .EditingChanged)

获取文本更改并采取相应措施。

于 2015-11-22T09:35:17.173 回答