UITextFieldShouldChange 被调用两次
UITextFieldShouldChange being called twice
我希望 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
}
}
你为什么不这样做呢?
homeAddressField.addTarget(self, "someFunction:", forControlEvents: .EditingChanged)
获取文本更改并采取相应行动。
我希望 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
}
}
你为什么不这样做呢?
homeAddressField.addTarget(self, "someFunction:", forControlEvents: .EditingChanged)
获取文本更改并采取相应行动。