我有一个 UIViewController容器,其中包含多个 UIViewController框。我想使用 UIKit Dynamics 将物理行为应用于盒子。容器定义了外部边界。
UIDynamicAnimator是容器中的一个对象,所有动态行为都是盒子的一部分。
问题:当我从盒子(childviewcontrollers)引用动画器以添加盒子行为时,我遇到了错误消息。
这是错误消息
> Terminating app due to uncaught exception
> 'NSInvalidArgumentException', reason: 'View item (<UIView: 0x9972720;
> frame = (0 568; 120 40); clipsToBounds = YES; autoresize = W+H;
> gestureRecognizers = <NSArray: 0x9976f80>; layer = <CALayer:
> 0x9972780>>) should be a descendant of reference view in
> <UIDynamicAnimator: 0x9906d40> Stopped (0.000000s) in <UIView:
> 0x9916bf0> {{0, 0}, {320, 568}}'
UIViewController 容器
- (void)viewDidLoad
{
[super viewDidLoad];
UIDynamicAnimator* animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_animator = animator;
...
}
添加框作为子视图和 ChildViewControllers
- (void)tapOnContainer:(UITapGestureRecognizer*)touch {
...
BoxViewController *note = [[BoxViewController alloc]init];
[box setContainerObject:self];
[self addChildViewController:box];
[self.view addSubview:box.view];
}
UIViewController 框
- (void)viewDidLoad
{
_containerViewController = (ViewController*) self.parentViewController;
_gravityBehaviour = [[UIGravityBehavior alloc]initWithItems:@[self.view]];
[_containerViewController.animator addBehavior:_gravityBehaviour];
...
}
知道如何让它工作吗?像这样拆分 UIDynamicAnimator 和 UIDynamicBehavior 对象可以吗?