UIKit Dynamics add gravity/collision/bounce to relocate a subview

I'm trying to move down a subview using UIKit Dynamics. I need to move the subview to a particular position but it doesn't work it goes all the way down of the main view. Here is my code:

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];

What I'm trying to do is let the subview go down and bounce back to the position I want in this case I want the subview to have this final origen (352,700)

any of you knows how can do this? or a work around ?

I'll really appreciate your help.


On what device are you trying your code for an iphone higher than 4S the height of your view would be 568 hence when you give the to point as CGPointMake( 352,700) your subview would go below your frame your iphone in portrait mode would have height as 568 and 320 in landscapemode so which mode are you using?

Shouldn't the code for collision be the opposite as you want the subview to go back to its original position on reaching the bottom? as in changing your values to

[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)];

or change the position of your subview in this delegate method

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

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

}

this would make your subview go from below to your original position

链接地址: http://www.djcxy.com/p/96784.html

上一篇: 如何使用UIDynamicAnimator避免动态变形

下一篇: UIKit Dynamics添加重力/碰撞/反弹来重定位子视图