如果在地图顶部使用MKMapView
文字字段中没有条目,我希望UIVisualEffectsView
模糊,我也希望它为aniamte,现在是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
}
}
答案 0 :(得分:1)
为什么不这样做呢?
homeAddressField.addTarget(self, "someFunction:", forControlEvents: .EditingChanged)
获取文本更改并采取相应措施。