【问题标题】:Moving origin back to original position将原点移回原始位置
【发布时间】:2014-04-07 17:41:58
【问题描述】:

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

 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];

}

【问题讨论】:

  • 我认为您的代码是错误的,您在取消方法中创建了一个新的 FooterViewController,但您没有为其分配任何视图;或者如果它是自动分配的,那么它当然会从原点创建,就好像它是一个新的一样;它与您之前动画的 footer.view 不同。
  • 另外,在 iOS 4.0 及更高版本中不鼓励使用 [UIView beginAnimations]。您应该使用基于块的动画方法来指定您的动画。
  • @htafoya 嗯。我见过的大多数例子都使用了它。另外,对于所述问题,您是否有“可靠”的解决方案?
  • 让我试着理解一下,您单击一个隐藏(或移动)页脚的按钮并打开一个模式。在模式上单击取消后,页脚应返回其原始位置。这是正确的,我会发布我推荐的答案。

标签: ios iphone objective-c uiview ibaction


【解决方案1】:

我给出以下建议,它们可能对你有好处。

注 1,仿射变换

如果翻译总是指向同一点并且总是使用相同的度量,我建议使用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)]

然后在cancel方法中,简单的做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    相关资源
    最近更新 更多