回到页面后动画图像位置不同返回页面

问题描述:

我有用于iPad的选项卡式应用程序。在第一个标签上,我有一个单独的菜单,其中有一个指针,用于突出显示当前活动的菜单按钮点击按钮后,指针(一个UIImage)被动画到位。回到页面后动画图像位置不同返回页面

问题是,当你离开高亮显示的图像上的任何按钮,这不是原来的/默认按钮,并移动到另一个选项卡中的应用程序,然后再回来,图像已移回默认位置。但是,当您点击另一个按钮时,图像朝错误的方向移动。我认为这是因为图像后退了,但旧的坐标被保留,或者相反?与直接在iPad上相比,它在模拟器中有点受挫,并且表现出不同的测试。

我想我需要开始与绝对坐标,并与他们坚持整个过程。但是我无法理解如何用CGAffine函数实现它。

这里是我当前的代码

- (IBAction)diplayForm1:(id)sender { 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    buttonHighlightImage.transform = CGAffineTransformMakeTranslation(0, 0); 
    [UIView commitAnimations]; 

} 

- (IBAction)diplayForm2:(id)sender { 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    buttonHighlightImage.transform = CGAffineTransformMakeTranslation(0, 80); 
    [UIView commitAnimations]; 

} 

- (IBAction)diplayForm3:(id)sender { 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    buttonHighlightImage.transform = CGAffineTransformMakeTranslation(0, 160); 
    [UIView commitAnimations]; 

} 

编辑 - - -

@Mundi我想你的建议的代码。我通过newFrame假设你的意思是定义一个新的UIImageView和属性,我在.h(我称之为'sfh')中做了,然后在.m中使用@synthesize。

这是我在.M

-(void)viewWillAppear:(BOOL)animated { 

    // The "pointer" and the "old" frame are the same so I thought it pointless to do 
    // selectedFormHighlight.frame = selectedFormHighlight.frame; 

    // I tried anyway and also tried this and many other combinations 
    selectedFormHighlight.frame = sfh.frame; 

} 

-(void)viewDidAppear:(BOOL)animated { 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView animateWithDuration:0.3 animations:^{ 
     selectedFormHighlight.frame = CGRectMake(0,0,0,0); 
    }]; 
    [UIView commitAnimations]; 

} 

代码当我运行这个动画指针从屏幕(左上)和不回来。我已经尝试了所有可能的组合,我可以在viewWillAppear和viewDidAppear中想到它,但它没有任何区别。我尝试删除viewDidAppear中的其他行,它仍然执行相同的操作。

我认为你建议在用户从另一个屏幕返回时从原始(默认)位置的指针动画。我不希望这样,它应该简单地留在他们没有动画的地方。或者至少在没有意识到的情况下把它放回去。唯一的动画是他们选择在该视图中点击不同的按钮。

我感谢@Mundi的帮助,但无法让它以这种方式工作。我最终得到了下面的代码。

// This initially sets the frame with a variable value that 
// corresponds to the current child view 
-(void)viewDidAppear:(BOOL)animated { 
    selectedFormHighlight.frame = CGRectMake(0, self.getY, 250, 4100); 
} 

// Button actions (converted to a single action instead of one for each button) 
// This was done by referencing a tag added to each button (the button tags 
// match the children's tags) 
-(IBAction)goToForm:(id)sender { 
    UIButton *btn = (UIButton *)sender; 
    self.currentChild = self.formViewControllers[btn.tag]; 
    [UIView beginAnimations.nil context:NULL]; 
    [UIView animateWithDuration:0.3 animations:^{ 
     selectedFormHighlight.frame = CGRectMake(0, self.getY, 250, 4100); 
    }]; 
    [UIView commitAnimations]; 
    sfh.frame = selectedFormHighlight.frame; 
} 

// This returns the 'y' (vertical center position) of the pointer 
// frame for each of the child views 
-(int)getY { 
    switch(_currentChild.view.tag) { 
     case 1: 
      return -2005; 
      break; 
     case 2: 
      return -1925; 
      break; 
     default: 
      return -1845; 
      break; 
    } 
} 

您遇到这种情况的原因是当视图隐藏并重新显示时,实际视图对象及其表示层会混淆。解决方案是更改视图属性而不是动画化图层。

因此,您不需要使用仿射变换。只需通过更改其framecenter直接设置指针视图的新位置。

更先进的日期格式要做到这一点是基于块的动画:

[UIView animateWithDuration:0.5 animations:^{ 
    pointer.frame = newFrame; 
}]; 

而且,它似乎非常低效的有三种方法只是一个变量不同。你可以仅仅通过80

编辑繁殖所需的y位置:

另一个原因您的按钮复位是因为这是它当视图被实例化被提。只需在您的视图控制器的viewWillAppear中设置视图的位置,然后将其动画到viewDidAppear中的新位置。

+0

嗨@Mundi,对不起,我是新来的,你可以给我一些更多的信息。你如何制作一个框架?我尝试了你的代码,但是使用'buttonHighlightImage.center = CGPointMake(125,205)'的中心方法;'但它在返回页面后仍然重置图像位置。否则,你如何创建newFrame? – PaulMrG 2013-02-21 02:04:04

+0

我尝试使用按钮的中心作为参考'buttonHighlightImage.center = CGPointMake(buttonHighlightImage.center.x,button1.center.y);'但它在返回到页面后仍然重置。 – PaulMrG 2013-02-21 02:17:14

+0

看到我的编辑/除了答案。 – Mundi 2013-02-21 09:06:22