UITextFieldShouldChange가 두 번 호출됨 (UITextFieldShouldChange being called twice)


문제 설명

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.

(by EliPeyman)

참조 문서

  1. UITextFieldShouldChange being called twice (CC BY‑SA 2.5/3.0/4.0)

#mkmapview #uianimation #swift #uiblureffect #iOS






관련 질문

지도 보기에서 현재 위치 확대 (Zoom on Current Location in Map View)

Is there a way to restrict MKMapView Zoomlevel in iOS6 (Is there a way to restrict MKMapView Zoomlevel in iOS6)

MKMapView가 약 180도 스크롤되지 않습니다. (MKMapView does not scroll around 180 degrees)

mapView에서 길찾기 버튼을 만드는 방법은 무엇입니까? (How to create get direction button in mapView?)

MKMapview를 사용하는 동안 MB의 메모리 사용량이 발생하고 ARC를 사용하는 동안 View가 사라질 때 메모리가 해제되지 않습니다. (While using MKMapview causing MBs of memory usage and using ARC it not releasing memory when View Disappears)

UITextFieldShouldChange가 두 번 호출됨 (UITextFieldShouldChange being called twice)

Mapview Xcode에서 위치 업데이트 (Update Location in Mapview Xcode)

MKMapView 폐색이 주석을 제거합니까? (Does MKMapView occlusion cull it's annotations?)

값이 없는 경우 HTML5 로컬 저장소 개체는 무엇을 반환합니까? (What does the HTML5 Local Storage object return if there is no value?)

MKMapView에서 사용자 위치를 찾는 데 문제가 있음 (Problems Trying to Find User Location in MKMapView)

MKMapView에 주석을 추가할 때 백그라운드에서 앱 충돌 (Crashes app in background when adding annotation on MKMapView)

모든 프로젝트 Swift 5에서 1개의 MapView를 공유하는 방법 (How to Share 1 MapView in all Project Swift 5)







코멘트