我试图创建一个视图,其中某些项目沿着正常的重力矢量落下,而其他项目则沿着相反的矢量落下
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];
我认为那时我可以将一些项添加到一个行为中,一些添加到另一个行为中,但似乎添加第二个重力行为会覆盖第一个行为?
[self.gravityBehavior addItem: ballA];
[self.inverseGravityBehavior addItem: ballB];
这是真的,如果有,还有另一种方法可以达到这种效果吗?
答案 0 :(得分:6)
这种行为绝对可行。但是,使用不同UIGravityBehavior
s的一个动画师无法实现。
一种解决方案是使用两个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
第二种选择是使用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
在所有情况下,请确保您正在管理哪些项目受UIDynamicAnimator
以及各种行为影响,以便您不会不断添加它们并阻碍模拟。