UITextFieldShouldChange被调用两次

时间:2015-11-22 09:19:15

标签: ios swift mkmapview uianimation uiblureffect

如果在地图顶部使用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
}
}

1 个答案:

答案 0 :(得分:1)

为什么不这样做呢?

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

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