xcode transitionWithView 和 animateWithDuration 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13885603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with transitionWithView and animateWithDuration
提问by Patrick Veilleux
I have problems with transitionWithView
and animateWithDuration
. One of my animateWithDuration
blocks doesn't transition, it is a sudden change, and transitionWithView
does not temporarily disable user interaction. I have checked the docs and believe I am doing everything correctly, but obviously something is wrong. Here are the two blocks of code:
我有transitionWithView
和 的问题animateWithDuration
。我的一个animateWithDuration
块没有转换,它是一个突然的变化,并且transitionWithView
不会暂时禁用用户交互。我已经检查了文档并相信我做的一切都是正确的,但显然有些问题。下面是两块代码:
This is in my main View Controller ViewController
which has three container views/child view controllers. This block moves one of the container views, but does not block the user from other interactions in ViewController
while the transition is occurring.
这是在我的主视图控制器中ViewController
,它具有三个容器视图/子视图控制器。此块移动容器视图之一,但不会在ViewController
发生转换时阻止用户进行其他交互。
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^ {
CGRect frame = _containerView.frame;
frame.origin.y = self.view.frame.size.height - _containerView.frame.size.height;
_containerView.frame = frame;
}completion:^(BOOL finished) {
// do something
}];
This is in one of my container view controllers. The animation seems to have no effect as the text of the productTitleLabel
and productDescriptionTextView
changes suddenly as if the animation block does not exist.
这是在我的容器视图控制器之一中。动画似乎没有任何效果的文本productTitleLabel
和productDescriptionTextView
突然改变,仿佛动画块不存在。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.viewController toggleFlavoredOliveOilsTableView];
if (indexPath.row > 0) {
NSDictionary *selectedCellDict = [[_flavoredOliveOilsDict objectForKey:@"Unflavored Olive Oils"] objectAtIndex:indexPath.row - 1];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^ {
self.viewController.productTitleLabel.text = [_flavoredOliveOilsTableView cellForRowAtIndexPath:indexPath].textLabel.text;
self.viewController.productDescriptionTextView.text = [selectedCellDict objectForKey:@"Description"];
}completion:nil];
if (indexPath.row == 1) {
[self.viewController setProductDescriptionTextViewFrameForInformationTab];
}
else {
[self.viewController setProductDescriptionTextViewFrameForNonInformationTab];
//self.viewController.productImageView.image = [UIImage imageNamed:[selectedCellDict objectForKey:@"Image"]];
}
}
}
I think the problems are somewhat related as most of my animation and transition blocks don't work completely as expected. Thanks for any help.
我认为这些问题有些相关,因为我的大部分动画和过渡块都没有按预期完全工作。谢谢你的帮助。
Edit
编辑
What I am trying to accomplish is moving a container view in ViewController
and set the text and image properties of a label, text view, and image view; all of which are in the main view. The details of these properties are sent via the child view controller. The transitionWithView
is in a method called toggleFlavoredOiveOilsTableView
which is called in didSelectRowAtIndexPath
. I think the problem is that I am trying to call two different animation/transition blocks at the same time.
我想要完成的是将容器视图移入ViewController
并设置标签、文本视图和图像视图的文本和图像属性;所有这些都在主视图中。这些属性的详细信息通过子视图控制器发送。的transitionWithView
是在一个称为方法toggleFlavoredOiveOilsTableView
被称为在didSelectRowAtIndexPath
。我认为问题在于我试图同时调用两个不同的动画/过渡块。
回答by Rob
You can experience this behavior of two animations interfering with each other if one animation is done on subview of another view undergoing animation. Thus, if you perform transitionWithView:self.view
(i.e. on the main view) like your code snippet suggests, you can have problems. If you perform the two animations on distinct subviews, the problem may go away. In my original answer below, I:
如果一个动画是在另一个正在执行动画的视图的子视图上完成的,则您可以体验到两个动画相互干扰的这种行为。因此,如果您按照transitionWithView:self.view
代码片段建议的方式执行(即在主视图上),您可能会遇到问题。如果您在不同的子视图上执行两个动画,问题可能会消失。在我下面的原始答案中,我:
Perform
transitionWithView
on a subview that has the twoUILabel
controls;Put my view controller containment child views within a subview of the main view, and then the
transitionFromViewController
is constrained to that subview.
transitionWithView
在具有两个UILabel
控件的子视图上执行;将我的视图控制器包含子视图放在主视图的子视图中,然后将
transitionFromViewController
其限制为该子视图。
When I put the two animated portions on distinct subviews, the animations can take place simultaneously without incident.
当我将两个动画部分放在不同的子视图上时,动画可以同时发生而不会发生意外。
If you want to animate the changing of the contents of two text labels, you can:
如果要为两个文本标签的内容更改设置动画,您可以:
[UIView transitionWithView:self.viewController.textLabelsContainerView
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.viewController.productTitleLabel.text = [_flavoredOliveOilsTableView cellForRowAtIndexPath:indexPath].textLabel.text;
self.viewController.productDescriptionTextView.text = [selectedCellDict objectForKey:@"Description"];
}
completion:nil];
Generally I'd use animateWithDuration
, but the text attribute is not an animatable property, so that's why I use transitionWithView
, making sure that I have a container for those text fields. But I've tried animating other controls using animateWithDuration
, while simultaneously animating the changing of the child controllers, and it works fine.
通常我会使用animateWithDuration
,但 text 属性不是一个可动画的属性,所以这就是我使用的原因transitionWithView
,确保我有一个用于这些文本字段的容器。但是我尝试使用 为其他控件设置动画animateWithDuration
,同时为子控制器的更改设置动画,并且效果很好。
For transitioning the child view controllers, I use transitionFromViewController
to animate the swapping of the containers child controller's views (it's part of the Managing Child View Controllers in a Custom Containerfamily of methods). In order to facilitate that process, I put a container view on my main view controller's view, and add the child controller's views as a subview of that container. That way, when I animate the transition of the child, the animations are nicely constrained to that container view.
为了转换子视图控制器,我使用transitionFromViewController
动画容器子控制器视图的交换(它是在自定义容器系列方法中管理子视图控制器的一部分)。为了促进该过程,我在主视图控制器的视图上放置了一个容器视图,并将子控制器的视图添加为该容器的子视图。这样,当我为孩子的过渡设置动画时,动画会很好地限制在该容器视图中。
So, here is some sample code to add a view controller to my container view, and then the code I use to transition between two child view controllers using a segmented button:
所以,这里有一些示例代码,用于将视图控制器添加到我的容器视图,然后是我使用分段按钮在两个子视图控制器之间转换的代码:
- (void)addFirstChild
{
UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"Stooge"];
[self addChildViewController:child];
child.view.frame = self.bottomContainerView.bounds;
[self.bottomContainerView addSubview:child.view];
[child didMoveToParentViewController:self];
}
- (void)changedChild
{
UIViewController *oldController = [self.childViewControllers lastObject];
UIViewController *newController;
if (self.segmentedControl.selectedSegmentIndex == 0)
newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Stooge"];
else
newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Marx"];
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];
// start off screen below
CGRect startFrame = self.bottomContainerView.bounds;
startFrame.origin.y += startFrame.size.height;
// end up where it's supposed to be, right in the container
newController.view.frame = startFrame;
[self transitionFromViewController:oldController
toViewController:newController
duration:0.5
options:0
animations:^{
newController.view.frame = self.bottomContainerView.bounds;
}
completion:^(BOOL finished) {
[oldController removeFromParentViewController];
[newController didMoveToParentViewController:self];
}];
}
Bottom line, I have no problem animating both container child controllers and the fields in the parent controller. Perhaps I'm not understanding the problem you're describing. Or maybe there's something subtle about the differences in how we're animating. If you want, I've put this above codeon github if you want to take a look.
最重要的是,我可以为容器子控制器和父控制器中的字段设置动画。也许我不明白你所描述的问题。或者也许我们动画制作方式的差异有些微妙。如果你愿意,如果你想看一看,我已经把上面的代码放在了github上。