我遇到了类似的问题,但在实际设备上。
我与苹果开发者技术支持 (Apple DTS) 进行了交谈,他们告诉我这是一个 swift 编译器或 Xcode 错误。
我做了一些解决方法。在苹果解决这个问题之前不要认为这是最好的解决方案,但现在它可以正常工作。
只需覆盖 UITextField 类:
class PBTextField : UITextField {
var keyboardShowDate = NSDate()
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
self.initialisation()
}
override init(frame: CGRect)
{
super.init(frame: frame)
self.initialisation()
}
func initialisation()
{
self.addTarget(self, action: "editingDidBegin:", forControlEvents: UIControlEvents.EditingDidBegin)
self.addTarget(self, action: "editingDidEnd:", forControlEvents: UIControlEvents.EditingDidEnd)
}
// MARK: Delegates
func editingDidBegin(sender: AnyObject)
{
self.becomeFirstResponder()
self.keyboardShowDate = NSDate()
}
func editingDidEnd(sender: AnyObject)
{
if(fabs(self.keyboardShowDate.timeIntervalSinceNow) <= 0.5)
{
self.becomeFirstResponder()
dispatch_after(0.1, block: { () -> Void in
self.becomeFirstResponder()
})
}
}
}
调度方法:
typealias dispatch_cancelable_block_t = ((cancel: Bool) -> Void)
func dispatch_async(block: dispatch_block_t, background: Bool = true) -> dispatch_cancelable_block_t?
{
return dispatch_after(0, block: block, background: background)
}
func dispatch_after(delay: NSTimeInterval = 0, block: dispatch_block_t, background: Bool = false) -> dispatch_cancelable_block_t?
{
var cancelableBlock : dispatch_cancelable_block_t?
let delayBlock : dispatch_cancelable_block_t = { (cancel) -> Void in
if(cancel == false)
{
if(background)
{
block()
}
else
{
dispatch_async(dispatch_get_main_queue(), block)
}
}
cancelableBlock = nil
};
cancelableBlock = delayBlock
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * NSTimeInterval(NSEC_PER_SEC))), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
if let cb = cancelableBlock
{
cb(cancel: false)
}
})
return cancelableBlock
}
func cancel_block(block: dispatch_cancelable_block_t)
{
block(cancel: true)
}
希望这对某人有所帮助。
如果您有更好的解决方案,请在 cmets 中告诉我。
谢谢
乔治