2014-04-07 148 views
0

今天,我試圖在取消後返回其默認原點。我正在使用兩個VC來做到這一點。一個是tableview中的頁腳控制器,另一個是模態視圖,在第一個動畫之後顯示。每當我嘗試從模態視圖返回時,在完成第一個動畫之後,原點仍然是相同的。這裏是我使用的代碼:將原點移回原位

Footer: 

    -(IBAction)addPerson:(id)sender{ 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.25]; 
     NSLog(@"%f", self.view.frame.origin.y); 
     self.view.frame = CGRectMake(0,-368,320,400); 
     [UIView commitAnimations]; 

     self.tdModal2 = [[TDSemiModalViewController2 alloc]init]; 


     // [self.view addSubview:test.view]; 

     [self presentSemiModalViewController2:self.tdModal2]; 
} 

-(void)moveBack{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.25]; 
    NSLog(@"%f", self.view.frame.origin.y); 
    self.view.frame = CGRectMake(0,368,320,400); 
    [UIView commitAnimations]; 
} 

而且在模式的看法:

-(IBAction)cancel:(id)sender{ 
    [self dismissSemiModalViewController:self]; 
    FooterViewController *foot = [[FooterViewController alloc]init]; 
    self.footer = foot; 
// self.footer.view.frame = CGRectMake(0,35,320,400); 
    [self.footer moveBack]; 

} 
+0

我認爲你的代碼是錯誤的,你正在創建的取消方法的新FooterViewController,但你是不是分配給它的任何視圖。或者如果它被自動分配,那麼它將從當然的來源創建,就像它是一個新的一樣;它與您以前的動畫不一樣。 – htafoya

+0

另外,在iOS 4.0及更高版本中不鼓勵使用[UIView beginAnimations]。您應該使用基於塊的動畫方法來指定您的動畫。 – htafoya

+0

@htafoya嗯。我見過的大多數例子都使用過。另外,你是否有針對所述問題的「可靠」解決方案? – 128keaton

回答

1

我給出以下建議,他們可能是對你有好處。

注意事項1,爲AffineTransform

如果翻譯總是相同的點總是以同樣的措施,我建議使用CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>)而不是通過修改視圖的框架。此方法指定視圖移動的x和y點的數量。

以這種方式,返回的視圖到原來的位置是因爲這樣做view.transform = CGAffineTransformIdentity.

這兩種過程的各自的動畫塊內一樣簡單。

注2,使用CGPoint移動原點

如果你只是移動視圖的起源,則建議是讓:

CGRect hiddenFrame = self.view.frame; 
hiddenFrame.origin.y -= 736; 
self.view.frame = hiddenFrame; 

CGRect hiddenFrame = self.view.frame; 
hiddenFrame.origin.y = -368; 
self.view.frame = hiddenFrame; 

CGRect hiddenFrame = self.view.frame; 
hiddenFrame.origin = CGPointMake(0,-368); 
self.view.frame = hiddenFrame; 

同樣的回遷。這是更多的代碼,但它更容易理解。

注3,UIView的動畫塊

您應該使用新的塊:

[UIView animateWithDuration: 0.25 animations: ^(void) { 
     //animation block 
}]; 

有更多的方法如延遲,完成塊等

其他塊選項,代表或參考通過

當您創建模態控制器,通過電流控制器的參考:

self.tdModal2 = [[TDSemiModalViewController2 alloc]init]; 
self.tdModal2.delegate = self; 

你應該聲明性能在TDSemiModalViewController2.h。要麼宣佈@class FooterViewController以避免交叉進口;通過制定協議並聲明屬性爲id<myModalProtocol>,FooterViewController應該使用方法moveBack來實現該協議;或者只是聲明該物業爲ID並致電[self.delegate performSelector: @selector(moveBack)]

然後在取消方法,簡單地做:

[self dismissSemiModalViewController:self]; 
[self.delegate moveBack] //or performSelector.. in the third option case 
+1

這工作出色! – 128keaton

+0

我很高興:) – htafoya