2014-04-07 93 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