這種行爲絕對有可能。但是,使用不同的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
和各種行爲的影響,這樣你是不是不斷增加,對他們受困模擬了下來。
這是一個很好的答案。不幸的是,我的問題並沒有明確說明:我真正想要的是一個「UIDynamicsAnimator」,用於動畫化所有物品的碰撞,其中一半物品被拉向相反的方向。 我認爲這可以用後來的解決方案充分解決,但?即使它不是理想的,我認爲這回答了這個問題。 – Warpling 2015-01-16 17:32:36
我一直在測試這個和意見回來,但沒有找到時間 - 所以很抱歉的延遲! – Warpling 2015-01-16 17:33:05
@聯繫是的,第二個選項應該正確處理衝突。它實際上只是施加與重力方向相反的力並使用相同的動畫設備。 – 2015-01-16 18:59:54