2014-03-01 47 views
0

我試圖使用UIKit動態下移子視圖。我需要將子視圖移到特定位置,但它不起作用,它會一直沿着主視圖移動。這裏是我的代碼: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]; 

我試圖做的是讓子視圖下去,反彈至我想在這種情況下的位置我想子視圖有這個最終奧利(352700)

你們誰都知道怎麼可以做到這一點?或工作?

我真的很感謝你的幫助。

回答

1

在什麼設備上,你嘗試你的iPhone的代碼高於4S,那麼你的視圖的高度應該是568,因此當你將指向點設置爲CGPointMake(352,700)時,你的子視圖將會在你的框架下方,在landscapemode中的高度是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); 

} 

這將使你的子視圖從下到原來的位置