2014-10-27 45 views
1

我試圖創建一個視圖,其中一些項目一起正常重力矢量下降等沿相反的矢量是否可以向UIDynamicAnimator添加多個UIGravityBehaviors?

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]; 

這是真的,如果有的話還有另一種方法來達到這種效果嗎?

回答

5

這種行爲絕對有可能。但是,使用不同的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和各種行爲的影響,這樣你是不是不斷增加,對他們受困模擬了下來。

+0

這是一個很好的答案。不幸的是,我的問題並沒有明確說明:我真正想要的是一個「UIDynamicsAnimator」,用於動畫化所有物品的碰撞,其中一半物品被拉向相反的方向。 我認爲這可以用後來的解決方案充分解決,但?即使它不是理想的,我認爲這回答了這個問題。 – Warpling 2015-01-16 17:32:36

+0

我一直在測試這個和意見回來,但沒有找到時間 - 所以很抱歉的延遲! – Warpling 2015-01-16 17:33:05

+0

@聯繫是的,第二個選項應該正確處理衝突。它實際上只是施加與重力方向相反的力並使用相同的動畫設備。 – 2015-01-16 18:59:54

相關問題