從主視圖中刪除子視圖(子視圖「變暗」父視圖,因此爲什麼稱爲dimView)我使用動畫;它基本上移向屏幕的底部,並最終在屏幕的子視圖:爲什麼在調用主線程的閉包時忽略了我的動畫?
let centerY = CGRectGetMidY(view.bounds)
let animation: CABasicAnimation = CABasicAnimation(keyPath: "position")
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
animation.fromValue = NSValue(CGPoint: view.center)
animation.toValue = NSValue(CGPoint: CGPoint(x: view.center.x, y: 4 * centerY))
animation.duration = 0.2
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
dimView.layer.addAnimation(animation, forKey: "DimRemovingAnimation")
然後通過事件驅動的功能,我把這個動畫像這樣:
@IBAction func done(sender: AnyObject) {
let dimView = view.viewWithTag(1)
if let dimView = dimView {
removingAnimation(dimView)
Delay.delay(0.4){
dimView.removeFromSuperview()
}
}
}
這是我的延時類:
class Delay{
class func delay(delay: Double, block:() ->()){
let when = dispatch_time(DISPATCH_TIME_NOW, Int64(Int(delay) * Int(NSEC_PER_SEC)))
dispatch_after(when, dispatch_get_main_queue(), block)
}
}
當我運行在主線程此閉合(這是串行線,並應以串行方式執行任務),我的動畫被忽略,dimView爲r一次從視圖層次中挖掘出來。但是,當我在全局併發線程上運行閉包時,不會忽略動畫,代碼將成功刪除層次結構的dimView。但是這是非法的,因爲你必須從主線程訪問UIKit。
您能否向我解釋當在主線程上調用閉包時問題是什麼?並可能解決我的問題?
感謝