[英]IOS UIView Bounce Animation
我有一个UIVIew
(容器)和另一个UIView
(框),框视图位于ContainerView
内部。 当按下UIButton
,我希望框视图从屏幕底部下拉,并向左反弹10px。 然后一旦弹跳停止,我仍然希望该框显示10px。 这是另一个问题的一些示例代码:
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];
该代码应在适当的时候运行,但该框没有丢失。
编辑1:
编辑2:
尝试制作动画师属性,
@property UIDynamicAnimator *animator;
然后是你的代码
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
...
我认为您没有指定正确的参考视图。 它应该是self.view
或self.containerView
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
更新我认为您应该将代码放入该场景的ViewController中,并按照@BaSha建议创建一个动画设计器属性,并在单击按钮后添加行为并引用self.containerView
只需确保boxView位于containerView内
以下代码段可以在您的视图上提供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.