iOS自定义动画替换子视图

时间:2016-11-10 08:37:24

标签: ios objective-c animation uiview uiviewanimationtransition

我有一个带有ViewController的应用程序,它包含一个headerView,headerView只是一个应该容纳其他视图的容器。 headerView中的视图是我想在运行时用动画替换的视图。我希望oldView(这是headerView的旧内容)向左滑动,newView从右侧滑入。 我已经包含了下面的代码和日志。 newView动画中的幻灯片有效,但oldView始终滑动到(0,0)坐标,而不是(-headerView.bounds.size.width,0)坐标。这种动画是不可能的,还是不是为这种动画制作的UIView animateWithDuration?非常感谢。

代码:

//put the frame at the right side of out bounds (to slide in)
[newView setFrame:CGRectMake(newView.frame.origin.x + newView.frame.size.width, newView.frame.origin.y, newView.frame.size.width, newView.frame.size.height)];
NSLog(@"Oldview bounds before transition: %f - %f - %f - %f", oldView.frame.origin.x, oldView.frame.origin.y, oldView.frame.size.width, oldView.frame.size.height);

[UIView animateWithDuration:1.0 animations:^{
    //put it in the correct position (slide in from right to left)
    [newView setFrame:CGRectMake(0, 0, self.headerView.frame.size.width, self.headerView.frame.size.height)];

    //Slide the old view out of the view to the left
    [oldView setFrame:CGRectMake(-self.headerView.bounds.size.width, 0, self.headerView.frame.size.width, self.headerView.frame.size.height)];
    NSLog(@"Oldview bounds after transition: %f - %f - %f - %f", oldView.frame.origin.x, oldView.frame.origin.y, oldView.frame.size.width, oldView.frame.size.height);
} completion:^(BOOL fin){
    //remove the old view from the headerview
    [oldView removeFromSuperview];
}];

日志输出:

Oldview bounds before transition: 0.000000 - 0.000000 - 760.000000 - 400.000000
Oldview bounds after transition: -760.000000 - 0.000000 - 760.000000 - 400.000000

[溶液] 我的oldView包含通过设置:

将视图保持在适当位置的约束
oldView.translatesAutoresizingMaskIntoConstraints = YES;

在动画解决问题之前。

0 个答案:

没有答案