简体   繁体   English

UITextFieldShouldChange 被调用两次

[英]UITextFieldShouldChange being called twice

I want a MKMapView to blur if there is no entry in a text field by using UIVisualEffectsView over top of the map, i also want it to aniamte, Right now the shouldChangeCharactersInRange its getting called twice when someone starts typing and not at all when the user removes all text from the UITextField .如果文本字段中没有条目,我希望MKMapView通过在地图顶部使用UIVisualEffectsView来模糊,我也希望它能够 aniamte,现在当有人开始打字时, shouldChangeCharactersInRange被调用两次,而不是在用户输入时调用从UITextField删除所有文本。 Heres my code这是我的代码

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

Why don't you do this instead?你为什么不这样做呢?

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

Get text changes and act accordingly.获取文本更改并采取相应措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM