2012-10-21 23 views
0

我想在一個視圖中觸發動畫,基於單獨的類文件中發生的情況。它得到的方法,通過這樣的事實,它並吐出兩個NSLog聲明支持,但並沒有提交UIView Animation動畫沒有通過執行到另一個視圖控制器

下面是代碼:

ViewController.h

@interface ViewController : UIViewController { 

} 

-(void)closeMenu; 

ViewController.m

-(void)closeMenu{ 

    //close the menu 
    NSLog(@"Got here"); 

    [UIView animateWithDuration:0.6 animations:^{ 

     [UIView setAnimationBeginsFromCurrentState:YES]; // so it doesn't cut randomly, begins from where it is 

     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

     [menuView setFrame:CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height)]; 

    }]; 


    NSLog(@"Got here2"); 

} 

OtherClass.m(註釋的代碼可以是irrelev螞蟻對這個問題,當然還是在實際應用中使用。 Jut認爲這可能會讓它更容易理解)

#import "ViewController.h" 

... 

//- (void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItem 
//{ 
    ViewController *foo = [[[ViewController alloc] init] autorelease]; 

    //SelectableCellState state = subItem.selectableCellState; 
    //NSIndexPath *indexPath = [item.subTable indexPathForCell:subItem]; 
    //switch (state) { 
     //case Checked: 
      //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Checked\"", indexPath); 

      //close the menuView 

      [foo closeMenu]; 

      //break; 
     //case Unchecked: 
      //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Unchecked\"", indexPath); 
      //break; 
     //default: 
      //break; 
    //} 
} 
+0

你是什麼意思它不承諾?你的意思是動畫不會發生?在設置menuView框架的動畫的行上設置一個斷點。斷點到達了嗎? menuView不是零嗎? – rmaddy

+0

@rmaddy是啊動畫沒有發生。斷點已經達到,menuView不爲零,我用IB製作了 –

+0

你確定它是在Interface Builder中連接的嗎? – WrightsCS

回答

1

你在混合老式動畫和基於塊的動畫。例如,文檔,它指出了setAnimationBeginsFromCurrentState

這種方法的使用中的iOS 4.0及更高版本氣餒。相反,您應使用theanimateWithDuration:delay:options:動畫:完成: 方法來指定您的動畫和動畫選項。

我不是100%確定是否支持。你應該至少改變你的動畫代碼:

[UIView animateWithDuration:0.6 
         delay:0.0 
        options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
    [menuView setFrame:CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height)]; 

          } 
       completion:nil]; 

除此之外,它似乎應該工作。如果影響框架,則可能會導致問題。在動畫塊之前計算幀可能是值得的。網HRS歐洲:

CGRect newFrame = CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height) 

[UIView animateWithDuration...:^{ menuView.frame = newFrame; }...]; 

編輯:哦,等等,看起來你是分配/ init'ing在(void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItem對象,並調用它的方法,但認爲是無處視圖層次結構。您需要在正在屏幕上顯示的實例上調用動畫。希望這是有道理的?

編輯2:要在已經顯示的實例上調用它,通常需要將其存儲在實例變量中。我不能說究竟如何在你的情況,但通常它會是這樣的形式:

@interface OtherClass() { 
    ViewController* m_viewController; 
} 
@end 

.... 

- (void)viewDidLoad // Or where ever you first create your view controller 
{ 
    ... 
    // If you're adding a ViewController within another ViewController, you probably need View Controller Containment 
    m_viewController = [[ViewController alloc] init]; 
    [self addChildViewController:m_viewController]; 
    [m_viewController didMoveToParentViewController:self]; 
    [self.view addSubview:m_viewController.view]; 
    ... 
} 

// If you're using ARC code, the dealloc wouldn't typically be necessary 
- (void)dealloc 
{ 
    [m_viewController release]; 
    [super dealloc]; 
} 

//- (void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItem 
//{ 
    //SelectableCellState state = subItem.selectableCellState; 
    //NSIndexPath *indexPath = [item.subTable indexPathForCell:subItem]; 
    //switch (state) { 
     //case Checked: 
      //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Checked\"", indexPath); 

      //close the menuView 

      [m_viewController closeMenu]; 

      //break; 
     //case Unchecked: 
      //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Unchecked\"", indexPath); 
      //break; 
     //default: 
      //break; 
    //} 
    } 

如果您需要從類的外部訪問,這是不夠的,對於使用性能。即

頭文件

@property (nonatomic, strong) ViewController* myViewController 

.m文件

// Use it as such 
[self.myViewController closeMenu]; 
+0

如何在實例中調用動畫...? (這似乎是在正確的軌道上,但你可以編輯你的答案,如何做到這一點)將不勝感激!謝謝! –

+0

@SirKaydian增加了一些關於在實例變量中保存它們的問題,這可能是問題;沒有看到它被使用的完整背景很難說。預先有效的代碼是創建一個新實例,而不是使用之前創建的實例。 – WDUK

+0

我試過你更新的。但是這也行不通。也許我不清楚。我有用於ViewController的xib文件,並且我想從另一個類文件爲其創建動畫。它達到了這個方法。但是,在這兩種方式中,它仍然無法激發觀點 –

1

該動畫的代碼是很奇怪的。你正在混合新的和舊的UIView動畫代碼,我不認爲你可以這樣做(但我可能是錯的)。

既然你已經開始使用基於塊的API,我會建議去那條路線(Apple推薦同樣的東西)。

有一種類似的方法可以用於您使用的選項,名稱爲animateWithDuration:delay:options:animations:completion:。您可以傳遞0作爲延遲,並且可以傳遞一個空的塊,以完成BOOL。

您想要通過選項的兩個標誌是UIViewAnimationOptionBeginFromCurrentStateUIViewAnimationOptionCurveEaseInOut

您的代碼將是這個樣子

[UIView animateWithDuration:0.6 
         delay:0.0 
        options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        // set your frame here... 
        [menuView setFrame:CGRectMake(menuView.frame.origin.x, 
                -menuView.frame.size.height, 
                menuView.frame.size.width, 
                menuView.frame.size.height)]; 
       } completion:^(BOOL finished){}]; 
相關問題