2014-03-14 14 views
1

我希望2個視圖的行爲就好像它們是「一個視圖」 - 意味着視圖1在'x'n個像素上移動我希望視圖2移動x軸的像素數量相同(任意方向) - 無需計算各種偏移量等等。如何附加2個視圖以便它們可以作爲一個視圖平移

我想用新的UIDynamicAnimator將是一個偉大的候選人,所以我想這

UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(320-150, 150, 150, 100)]; 
v1.backgroundColor = [UIColor blackColor]; 
[self.view addSubview:v1]; 

self.v1 = v1; 
UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 
                 0, 
                 self.view.bounds.size.width, 
                 self.view.bounds.size.height)]; 
v2.backgroundColor = [UIColor redColor]; 
[self.view addSubview:v2]; 
self.v2 = v2; 

UIPanGestureRecognizer *p = 
[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 
[v1 addGestureRecognizer:p]; 

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

UIAttachmentBehavior *att = 
[[UIAttachmentBehavior alloc] initWithItem:self.v1 
          offsetFromCenter:UIOffsetZero 
          attachedToItem:self.v2 
          offsetFromCenter:UIOffsetMake(0, 
             self.v1.center.y - self.v2.center.y)]; 
att.damping = 0; 
[self.animator addBehavior:att]; 
self.att = att; 

-(void)pan:(UIPanGestureRecognizer *)gesture { 
    if(gesture.state == UIGestureRecognizerStateChanged) { 
     CGPoint p = [gesture locationInView:self.view]; 
     // move only on x axis but keep on same y point 
     self.v1.center = CGPointMake(p.x, self.v1.center.y); 
     [self.animator updateItemUsingCurrentState:self.v1]; 

    } 
} 

但他們相互交融 - 小視圖傾斜和改變其旋轉和y位置。

我本來希望 - 只有在1軸和「硬」彼此連接 - 任何方式來做到這一點?

回答

2

鑑於沒有提供關於上下文的更多細節,我假設您希望兩個視圖的相對距離保持不變。如果確實如此,我不確定你爲什麼考慮UIDynamicAnimator。相反,您可以將兩個視圖嵌入到容器視圖中,然後分別操作容器的原點而不是兩個視圖。通過採用這種方法,您不必擔心在移動另一個視圖時重新計算一個視圖的來源,反之亦然。

self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)]; 
UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(100 0, 100, 100)]; 
[self.containerView addSubview:v1]; 
[self.containerView addSubview:v2]; 

// now, changing the origin of containerView will move v1 and v2 simultaniously 
- (void)pan:(UIPanGestureRecognizer *)gesture 
{ 
    if(gesture.state == UIGestureRecognizerStateChanged) 
    { 
     CGPoint p = [gesture locationInView:self.view]; 
     // move only on x axis but keep on same y point 
     self.containerView.center = CGPointMake(p.x, self.containerView.center.y); 
    } 
} 

作爲替代方案,你可以使用自動佈局,並涉及在這種方式的兩種觀點,你的要求得到滿足。然後,當移動其中一個視圖時,另一個會相應移動。但是,如果您實際上是使用UIDynamics來操作該視圖層次結構中的視圖,則自動佈局不會自動佈局,因爲自動佈局和UIDynamics傾向於相互干擾。

+0

而且不要忘了對上海華和上海華盈的Alpha設置平移手勢識別爲0 –

+0

我希望我可以添加這兩個觀點對同樣的層面 - 但這是不可能的 - 一個視圖屬於一個表視圖 - 另一個屬於一個容器 - 所以他們確實沒有任何關係或者彼此之間的知識 - 這個例子僅用於演示目的(因爲我正在玩它)。目標是讓兩個視圖一起移動 - 自動佈局是不可能的 - 我現在正在做的是手動將'view2'的原點改爲'x'個像素 - 但這是不穩定的,並且看起來不太好:( –

+0

In在這種情況下,我認爲沒有一種簡單的方法可以省略手動更改原點。您可以考慮[KVO](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving。 html)(這裏簡單的解釋)(http://nshipster.com/key-value-observing/))來觀察視圖的起源何時改變,但如果它是'UITableViewCell',它可能由於電池重複使用而使您陷入困境。 –

1

解決此問題的一個簡單方法可能是將其中一個視圖作爲子視圖添加到另一個視圖中,或者將兩個視圖都添加爲僅用於對視圖進行分組的新視圖的子視圖。這取決於你打算如何使用視圖,但這可能並不適用於你。

如果您想讓一個視圖成爲另一個視圖的子視圖,但希望子視圖在超視圖範圍外可見,您可以在超級視圖上設置clipToBoundsNO

2

在panGesture中將v1原點'X'設置爲v2原點'X'。

- (空)鍋:(UIPanGestureRecognizer *)手勢{

CGPoint point = [gesture locationInView:self.view]; 

CGRect boundsRect = CGRectMake(15, 40, 285, self.view.frame.size.height-60); 

if (CGRectContainsPoint(boundsRect, point)) 
{ 
    v1.center = point; 
} 



v2.frame = CGRectMake(v1.frame.origin.x, v2.frame.origin.y, v2.frame.size.width, v2.frame.size.height); 

}

相關問題