문제 설명
UITextFieldShouldChange가 두 번 호출됨 (UITextFieldShouldChange being called twice)
지도 위에 UIVisualEffectsView
를 사용하여 텍스트 필드에 항목이 없는 경우 MKMapView
를 흐리게 처리하고 싶습니다. 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
}
}
참조 솔루션
방법 1:
Why don't you do this instead?
homeAddressField.addTarget(self, "someFunction:", forControlEvents: .EditingChanged)
Get text changes and act accordingly.