2015-12-26 32 views
0

我用KVO到observe changes in a frame並設置相應的iOS:動畫不在observeValueForKeyPath

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ 
    if ([keyPath isEqualToString:KeyPathForFrameBeingObserved]) { 
     [UIView animateWithDuration:1 animations:^{ 
      CGRect frame = [self playWithFrame:viewBeingMonitored.frame]; 
      self.viewToAnimate.frame = frame; 
     }]; 
    } 
} 

代碼在動畫塊被執行但動畫是不工作的另一幀的工作,我懷疑動畫方法重複調用可能導致這但使用日誌消息,我發現動畫即使對於一次調用也不起作用,任何人都可以解釋嗎?

的iPad 2 /(iOS8.4)

  • 我曾嘗試包括dispatch_async(dispatch_get_main_queue(), ^{ ... });動畫方法調用,但是動畫仍然沒有工作。
  • 我嘗試推動隊列中觀察到的更改並延遲動畫方法一秒,使用dispatch_after僅使用隊列中的最後一個元素來運行它,以避免重複調用但不起作用。
  • KVO工作正常(添加觀察者已經處理),但動畫塊中的代碼在沒有動畫的情況下執行。
+0

你在對象中調用了addObserver forKeyPath嗎? – Coyote

+0

@Coyote是的,我做過,KVO工作得很好,但結果不是動畫。 –

+0

這個線程是否適用? http://stackoverflow.com/questions/26277109/uiview-animatewithduration-not-animating-in-ios8 –

回答

1

看起來好像你正在試圖觀察一個UIKit對象的屬性。請記住:

... UIKit框架中的類一般不支持志願......

來源:https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html

所以,你必須檢查,如果你observeValueForKeyPath:ofObject:change:context:事實上正在呼叫。

+0

KVO已經在我的案例中工作,請參閱http://stackoverflow.com/a/ 19687115/1356559,問題是執行動畫塊中的代碼但沒有動畫。 –

0

這是一個較老的問題,但我想我會回答,以防別人正在閱讀此內容。您需要調用setFrame來設置動畫的框架,而不是簡單的賦值。

CGRect frame = [self playWithFrame:viewBeingMonitored.frame]; [self.viewToAnimate setFrame:frame];