我希望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軸和「硬」彼此連接 - 任何方式來做到這一點?
而且不要忘了對上海華和上海華盈的Alpha設置平移手勢識別爲0 –
我希望我可以添加這兩個觀點對同樣的層面 - 但這是不可能的 - 一個視圖屬於一個表視圖 - 另一個屬於一個容器 - 所以他們確實沒有任何關係或者彼此之間的知識 - 這個例子僅用於演示目的(因爲我正在玩它)。目標是讓兩個視圖一起移動 - 自動佈局是不可能的 - 我現在正在做的是手動將'view2'的原點改爲'x'個像素 - 但這是不穩定的,並且看起來不太好:( –
In在這種情況下,我認爲沒有一種簡單的方法可以省略手動更改原點。您可以考慮[KVO](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving。 html)(這裏簡單的解釋)(http://nshipster.com/key-value-observing/))來觀察視圖的起源何時改變,但如果它是'UITableViewCell',它可能由於電池重複使用而使您陷入困境。 –