问题描述
- 我添加的UIGravityBehavior并没有动画显示
-
// // ViewController.m // DropGame // // Created by bayMax on 10/16/15. // Copyright ? 2015 bayMax. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *gameView; @property (strong,nonatomic) UIDynamicAnimator *animator; @property (strong,nonatomic) UIGravityBehavior *gravity; @property (strong,nonatomic) UIPushBehavior *push; @end @implementation ViewController static const CGSize DROP_SIZE = {40,40}; -(UIDynamicAnimator *)animaotr { if(!_animator) { _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.gameView]; } return _animator; } -(UIGravityBehavior *)gravity { if(!_gravity) { _gravity = [[UIGravityBehavior alloc] init]; _gravity.magnitude = 1; [self.animator addBehavior:_gravity]; } return _gravity; } /*-(UIPushBehavior *)push { if(!_push) { _push = [[UIPushBehavior alloc] init]; [self.animator addBehavior:_push]; } return _push; } */ - (IBAction)tap:(UITapGestureRecognizer *)sender { [self drop]; } -(void)drop { CGRect frame; frame.origin = CGPointZero; frame.size = DROP_SIZE; int x = (arc4random()%(int)self.gameView.bounds.size.width) / DROP_SIZE.width; frame.origin.x = x * DROP_SIZE.width; UIView *dropView = [[UIView alloc] initWithFrame:frame]; dropView.backgroundColor = [self randomColor]; [self.gameView addSubview:dropView]; [self.gravity addItem:dropView]; } -(UIColor *)randomColor { switch (arc4random()%5) { case 0:return [UIColor greenColor];break; case 1:return [UIColor redColor];break; case 2:return [UIColor blueColor];break; case 3:return [UIColor yellowColor];break; case 4:return [UIColor purpleColor];break; } return [UIColor blackColor]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
有UIDynamicAnimator我也添加了gravity但是就是没有动画,这是为什么。。
解决方案
你都没有访问gravity,其中的方法永远不会调用
时间: 2024-09-17 04:38:11