是否可以向UIDynamicAnimator添加多个UIGravityBehaviors?

War*_*ing 1 uikit ios uikit-dynamics

我正在尝试创建一个视图,其中一些项目沿着正常的重力矢量落下,而其他项目则沿着相反的矢量落下

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

// Normal Gravity
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[ ]];
self.gravityBehavior.gravityDirection = CGVectorMake(0, 1);
[self.animator addBehavior:self.gravityBehavior];

// Inverse Gravity
self.inverseGravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[ ]];
self.inverseGravityBehavior.gravityDirection = CGVectorMake(0, -1);
[self.animator addBehavior:self.inverseGravityBehavior];
Run Code Online (Sandbox Code Playgroud)

我认为那时我可以添加一些项目到一个行为,一些到另一个,但似乎添加第二个引力行为会覆盖第一个?

    [self.gravityBehavior        addItem: ballA];
    [self.inverseGravityBehavior addItem: ballB];
Run Code Online (Sandbox Code Playgroud)

这是真的,如果有,还有另一种方法可以达到这个效果吗?

And*_*deh 6

这种行为绝对可行.但是,使用不同的UIGravityBehaviors的一个动画师无法实现.

一种解决方案是使用两个UIDynamicAnimator实例,每个实例都有自己的UIGravityBehavior实例.下面是一个视图控制器的示例,其中连接了两个轻敲手势识别器,一个用于单击,一个用于双击.单曲将添加一个对正常重力作出反应的视图.双击增加了一个对倒置重力作出反应的视图.显然,需要调整代码以满足您的特定要求.

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, strong) UIDynamicAnimator *secondAnimator;

@property (nonatomic, strong) UIGravityBehavior *normalGravity;
@property (nonatomic, strong) UIGravityBehavior *inverseGravity;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    self.normalGravity = [[UIGravityBehavior alloc] init];
    [self.animator addBehavior:self.normalGravity];

    self.secondAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    self.inverseGravity = [[UIGravityBehavior alloc] init];
    [self.inverseGravity setAngle:self.normalGravity.angle magnitude:(-1.0 * self.normalGravity.magnitude)];
    [self.secondAnimator addBehavior:self.inverseGravity];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)viewWasTapped:(id)sender {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 10, 25, 25)];
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
    [self.normalGravity addItem:v];
}

- (IBAction)viewWasDoubleTapped:(id)sender {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(90, 400, 25, 25)];
    v.backgroundColor = [UIColor greenColor];
    [self.view addSubview:v];
    [self.inverseGravity addItem:v];
}

@end
Run Code Online (Sandbox Code Playgroud)

第二种选择是使用a UIPushBehavior来模拟倒置重力.下面是一个类似设置的视图控制器,但没有第二个动画师并使用推送行为.

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDynamicAnimator *animator;

@property (nonatomic, strong) UIGravityBehavior *normalGravity;
@property (nonatomic, strong) UIPushBehavior *pushBehavior;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    self.normalGravity = [[UIGravityBehavior alloc] init];
    [self.animator addBehavior:self.normalGravity];

    self.pushBehavior = [[UIPushBehavior alloc] initWithItems:@[] mode:UIPushBehaviorModeContinuous];
    [self.pushBehavior setAngle:self.normalGravity.angle magnitude:(-1.0 * self.normalGravity.magnitude)];
    [self.animator addBehavior:self.pushBehavior];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)viewWasTapped:(id)sender {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 10, 25, 25)];
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
    [self.normalGravity addItem:v];
}

- (IBAction)viewWasDoubleTapped:(id)sender {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(90, 400, 25, 25)];
    v.backgroundColor = [UIColor greenColor];
    [self.view addSubview:v];

    [self.pushBehavior addItem:v];
}

@end
Run Code Online (Sandbox Code Playgroud)

在所有情况下,请确保您正在管理哪些项目受到UIDynamicAnimator各种行为的影响,以便您不会不断添加它们并使模拟陷入困境.