UIKit Dynamics添加重力/碰撞/反弹以重新定位子视图

时间:2014-03-01 05:40:22

标签: ios iphone objective-c uikit-dynamics

我试图使用UIKit Dynamics向下移动子视图。我需要将子视图移动到一个特定的位置,但它不会工作,它会一直向下移动到主视图。这是我的代码:

UIView *mySubview = [self.view viewWithTag:102];

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[mySubview]];
self.gravityBehavior.magnitude = 1;
[self.animator addBehavior:self.gravityBehavior];

self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[ mySubview]];
[self.collisionBehavior addBoundaryWithIdentifier:@"bottom"
                                        fromPoint:CGPointMake(352, 233)
                                          toPoint:CGPointMake( 352,700)];
self.collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:self.collisionBehavior];


self.bounceBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[ mySubview]];
self.bounceBehaviour.elasticity = 0.6;
[self.animator addBehavior:self.bounceBehaviour];

我试图做的是让子视图下降并反弹到我想要的位置,在这种情况下,我希望子视图有这个最终的origen(352,700)

你们任何人都知道如何做到这一点?或解决方法?

我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

你试用高于4S的iphone的设备是什么设备你的视图高度是568因此当你给CGPointMake(352,700)指向你的子视图将低于你的框架你的iphone在纵向模式下在横向模式中高度为568和320,那么你使用哪种模式?

碰撞代码不应该是相反的,因为你希望子视图在到达底部时回到原来的位置吗?如将值更改为

[self.collisionBehavior addBoundaryWithIdentifier:@"bottom"
fromPoint:CGPointMake(352, the total here should be 568 which you would get by adding your subview's height and y-value)
 toPoint:CGPointMake( 352,233)];

或在此委托方法中更改子视图的位置

- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(your subview) withItem:(bottom of your view) {

subview.frame=CGRectMake(352,233 ,subview height ,subview width);

}

这会使您的子视图从下方移动到原始位置