简体   繁体   English

iOS UIView跳动动画

[英]IOS UIView Bounce Animation

I have a UIVIew (container) and another UIView (box) the box view is inside of the ContainerView . 我有一个UIVIew (容器)和另一个UIView (框),框视图位于ContainerView内部。 When a UIButton is pressed I would like the box view to drop down off the bottom of the screen, and bounce with 10px left; 当按下UIButton ,我希望框视图从屏幕底部下拉,并向左反弹10px。 then once the bouncing has stopped I still want the box to have 10px showing. 然后一旦弹跳停止,我仍然希望该框显示10px。 Here is some sample code of from another question : 这是另一个问题的一些示例代码:

UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self]; //self is the container

UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[box]];
[animator addBehavior:gravityBehavior];

UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[reportBar.bar]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[animator addBehavior:collisionBehavior];

UIDynamicItemBehavior *elasticityBehavior =
[[UIDynamicItemBehavior alloc] initWithItems:@[box]];
elasticityBehavior.elasticity = 0.7f;
[animator addBehavior:elasticityBehavior];

The code is running when it should but the box isn't dropping. 该代码应在适当的时候运行,但该框没有丢失。

布局示例

Edit 1: 编辑1:

  • Self refers to the container UIView 自指容器UIView
  • I have also tried changing self for the currentViewController.view, no change. 我也尝试过为currentViewController.view更改自身,没有更改。

Edit 2: 编辑2:

  • all of this code is inside the container view implementation file. 所有这些代码都在容器视图实现文件中。

give a try to make animator property, 尝试制作动画师属性,

@property UIDynamicAnimator *animator;

and then your code, 然后是你的代码

_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];

 ...

I think you didn't specify the correct reference view. 我认为您没有指定正确的参考视图。 It should be either self.view or self.containerView 它应该是self.viewself.containerView

UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];

Update I think you should put the code in the ViewController for this scene and as @BaSha suggested create a animator property and after a button click you will add the behavior and will reference self.containerView Just make sure that boxView is inside of the containerView 更新我认为您应该将代码放入该场景的ViewController中,并按照@BaSha建议创建一个动画设计器属性,并在单击按钮后添加行为并引用self.containerView只需确保boxView位于containerView内

The following code snippet can provide BOUNCE EFFECT on your views. 以下代码段可以在您的视图上提供BOUNCE EFFECT

CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position.y"];</br>
[animation setFromValue:[NSNumber numberWithFloat:y-position1]];   
[animation setToValue:[NSNumber numberWithFloat:y-position2]];
[animation setDuration:.7];    //   time gap between the bounces
animation.repeatCount=500;    //    no:of times bounce effect has to be done 
[YOURVIEW.layer addAnimation:animation forKey:@"somekey"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM