我想獲得一個UIView,它是以圓形狀使用UIKit動態「反彈」。它應該在到達屏幕底部時反彈。圓是我在viewDidLoad方法創建如下標準的UIView:試圖讓UIView使用UIKit動態反彈
- (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];
}
我希望「球「直落在屏幕上,一旦到達視圖底部,停止並」反彈「。但是,當我運行我的應用程序時,我看到的只是屏幕頂部的圓圈脫離屏幕。我究竟做錯了什麼?
我最初問爲什麼球沒有下降和彈跳。不僅僅是爲什麼它一動不動。我編輯了問題中的代碼,以及包含您的建議的總結問題,但不幸的是我仍然遇到了麻煩。您在書中指出了您的示例代碼,並查看了您在「doButton」方法中添加行爲的相關代碼塊,但不幸的是我仍然沒有找到解決方案。 – syedfa