尝试使用 UIKit Dynamics 使 UIView 反弹
Trying to get UIView to bounce using UIKit Dynamics
我正在尝试使用 UIKit Dynamics "bounce" 获得一个圆形的 UIView。它应该在到达屏幕底部时反弹。圆圈是我在 viewDidLoad 方法中创建的标准 UIView,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.orangeBall = [[UIView alloc] initWithFrame:CGRectMake(100.0, 100.0, 50.0, 50.0)];
self.orangeBall.backgroundColor = [UIColor orangeColor];
self.orangeBall.layer.cornerRadius = 25.0;
self.orangeBall.layer.borderColor = [UIColor blackColor].CGColor;
self.orangeBall.layer.borderWidth = 0.0;
[self.view addSubview:self.orangeBall];
// Initialize the property UIDynamicAnimator
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
[self demoGravity];
}
我正在尝试在 demoGravity 方法中实现弹跳效果,如下所示:
-(void)demoGravity{
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.orangeBall]];
[self.animator addBehavior:gravityBehavior];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.orangeBall]];
[collisionBehavior addBoundaryWithIdentifier:@"myView"
fromPoint:self.orangeBall.frame.origin
toPoint:CGPointMake(self.orangeBall.frame.origin.x, self.orangeBall.frame.origin.y + self.view.frame.size.height)];
[self.animator addBehavior:collisionBehavior];
}
我想让 "ball" 直接落在屏幕上,一旦到达视图底部就停止,"bounce"。但是,当我 运行 我的应用程序时,我所看到的只是屏幕顶部的圆圈从屏幕上掉下来了。我做错了什么?
我的球不弹跳的原因有两个:
它没有掉落,因为我的 UIDynamicAnimator 对象没有附加 gravityBehaviour:
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.orangeBall]];
[self.animator addBehavior:gravityBehavior];
它没有弹跳,因为我需要将 CollisionBehaviour: "TranslatesReferenceBoundsIntoBoundary" 的布尔值 属性 设置为 "YES"。
[collisionBehavior setTranslatesReferenceBoundsIntoBoundary:YES];
特别感谢 Ray Wenderlich,其中 article 为我提供了解决方案,以下段落是文章的直接引用:
Rather than explicitly adding boundary co-ordinates, the above code sets the translatesReferenceBoundsIntoBoundary property to YES. This causes the boundary to use the bounds of the reference view supplied to the UIDynamicAnimator.
我正在尝试使用 UIKit Dynamics "bounce" 获得一个圆形的 UIView。它应该在到达屏幕底部时反弹。圆圈是我在 viewDidLoad 方法中创建的标准 UIView,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.orangeBall = [[UIView alloc] initWithFrame:CGRectMake(100.0, 100.0, 50.0, 50.0)];
self.orangeBall.backgroundColor = [UIColor orangeColor];
self.orangeBall.layer.cornerRadius = 25.0;
self.orangeBall.layer.borderColor = [UIColor blackColor].CGColor;
self.orangeBall.layer.borderWidth = 0.0;
[self.view addSubview:self.orangeBall];
// Initialize the property UIDynamicAnimator
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
[self demoGravity];
}
我正在尝试在 demoGravity 方法中实现弹跳效果,如下所示:
-(void)demoGravity{
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.orangeBall]];
[self.animator addBehavior:gravityBehavior];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.orangeBall]];
[collisionBehavior addBoundaryWithIdentifier:@"myView"
fromPoint:self.orangeBall.frame.origin
toPoint:CGPointMake(self.orangeBall.frame.origin.x, self.orangeBall.frame.origin.y + self.view.frame.size.height)];
[self.animator addBehavior:collisionBehavior];
}
我想让 "ball" 直接落在屏幕上,一旦到达视图底部就停止,"bounce"。但是,当我 运行 我的应用程序时,我所看到的只是屏幕顶部的圆圈从屏幕上掉下来了。我做错了什么?
我的球不弹跳的原因有两个:
它没有掉落,因为我的 UIDynamicAnimator 对象没有附加 gravityBehaviour:
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.orangeBall]]; [self.animator addBehavior:gravityBehavior];
它没有弹跳,因为我需要将 CollisionBehaviour: "TranslatesReferenceBoundsIntoBoundary" 的布尔值 属性 设置为 "YES"。
[collisionBehavior setTranslatesReferenceBoundsIntoBoundary:YES];
特别感谢 Ray Wenderlich,其中 article 为我提供了解决方案,以下段落是文章的直接引用:
Rather than explicitly adding boundary co-ordinates, the above code sets the translatesReferenceBoundsIntoBoundary property to YES. This causes the boundary to use the bounds of the reference view supplied to the UIDynamicAnimator.