【问题标题】:UIKit Dynamics add gravity/collision/bounce to relocate a subviewUIKit Dynamics 添加重力/碰撞/反弹来重新定位子视图
【发布时间】:2014-03-01 05:40:22
【问题描述】:

我正在尝试使用 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];

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

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

非常感谢您的帮助。

【问题讨论】:

    标签: ios iphone objective-c uikit-dynamics


    【解决方案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);
    
    }
    

    这将使您的子视图从下方转到您的原始位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 2014-01-20
      • 1970-01-01
      • 2018-01-21
      相关资源
      最近更新 更多