gpt4 book ai didi

ios - 将原点移回原始位置

转载 作者:行者123 更新时间:2023-11-29 03:04:40 29 4
gpt4 key购买 nike

今天,我试图让 View 在取消后返回到其默认原点。我使用两个 VC 来完成此操作。一个是 TableView 中的页脚 Controller ,另一个是模态视图,在第一个动画之后呈现。每当我尝试从模态视图返回时,在完成第一个动画后,原点仍然是相同的。这是我正在使用的代码:

 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,AffineTransform

如果翻译始终到同一点并且始终采用相同的度量,我建议使用 CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>)而不是修改 View 的框架。此方法指定 View 将移动多少 x 和 y 点。

这样,将 View 返回到原始位置就像执行 view.transform = CGAffineTransformIdentity. 一样简单

当然,这两者都在各自的动画 block 中。

注2,使用CGPoint移动原点

如果您只是移动 View 的原点,那么建议是:

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动画 block

你应该使用新的 block :

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

还有其他 block 有更多方法,如延迟、完成 block 等。

选项、委托(delegate)或引用传递

创建模态 Controller 时,传递当前 Controller 的引用:

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

您应该在 TDSemiModalViewController2.h 中声明该属性。通过声明 @class FooterViewController避免交叉进口;通过制定协议(protocol)并将属性声明为 id<myModalProtocol> , FooterViewController 应使用 moveBack 方法实现协议(protocol);或者只是将属性声明为 id 并调用 [self.delegate performSelector: @selector(moveBack)] .

然后在取消方法中,简单地做:

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

关于ios - 将原点移回原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22919429/

29 4 0