Ios 将原点移回原始位置
今天,我试图让视图在取消后返回其默认原点。我正在使用两个VCs来完成这项工作。一个是表视图中的页脚控制器,另一个是模式视图,在第一个动画之后显示。每当我尝试从模态视图返回时,在我制作第一个动画之后,原点仍然是相同的。以下是我正在使用的代码:Ios 将原点移回原始位置,ios,iphone,objective-c,uiview,ibaction,Ios,Iphone,Objective C,Uiview,Ibaction,今天,我试图让视图在取消后返回其默认原点。我正在使用两个VCs来完成这项工作。一个是表视图中的页脚控制器,另一个是模式视图,在第一个动画之后显示。每当我尝试从模态视图返回时,在我制作第一个动画之后,原点仍然是相同的。以下是我正在使用的代码: Footer: -(IBAction)addPerson:(id)sender{ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDur
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];
}
我给出以下建议,它们可能对你有好处 注1,仿射变换 如果转换始终指向相同的点并且始终使用相同的度量,我建议使用
cGraffineTransformMakeTransform(,)
而不是修改视图的框架。此方法指定视图将移动多少x和y点
这样,将视图返回到原始位置就像执行view.transform=CGAffineTransformity一样简单。
当然,它们都在各自的动画块中
注2,使用CGPoint移动原点
如果仅移动视图的原点,则建议:
CGRect hiddenFrame = self.view.frame;
hiddenFrame.origin.y -= 736;
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
,FooterViewController应使用方法moveBack
实现协议;或者只需将属性声明为id并调用[self.delegate performSelector:@selector(moveBack)]
然后在cancel方法中,只需执行以下操作:
[self dismissSemiModalViewController:self];
[self.delegate moveBack] //or performSelector.. in the third option case
我认为您的代码是错误的,您正在cancel方法中创建一个新的FooterViewController,但是您没有为它分配任何视图;或者,如果它是自动分配的,那么它将从原点创建,就像它是一个新的一样;它与之前设置动画的footer.view不同。此外,在iOS 4.0及更高版本中不鼓励使用[UIView beginAnimations]。您应该使用基于块的动画方法来指定动画。@htafoya-hmm。我看到的大多数示例都使用了这种方法。另外,您是否有针对所述问题的“可靠”解决方案?让我试着理解一下,您单击隐藏(或移动)页脚的按钮并打开一个模式。在模式上单击“取消”后,页脚应返回其原始位置。这是正确的,我会发布我推荐的答案。
self.tdModal2 = [[TDSemiModalViewController2 alloc]init];
self.tdModal2.delegate = self;
[self dismissSemiModalViewController:self];
[self.delegate moveBack] //or performSelector.. in the third option case