所以,你只關注左/右平移...
讓我們假設你有2個UIImageViews,一個& b,作爲一些其他UIView的子視圖並被定義爲某個類的類成員。
你可以得到幫助檢測通過創建UIPanGestureRecognizer的實例,並將它們添加到每個UIImageView的使用方法addGestureRecognizer平移手勢(拖動觸摸)關於這些觀點:
在創建每個UIPanGestureRecognizer,你需要指定一個選擇接收手勢事件。我們說,這就是所謂的didMove:
現在,一些示例代碼:
- (void) didMove:(UIPanGestureRecognizer*)recognizer {
UIView* view = [recognizer view];
// Remember our UIImageViews are class members called, a & b
//
UIView* otherView = view == a ? b : a;
if (recognizer.state == UIGestureRecognizerStateBegin
|| recognizer.state == UIGestureRecognizerStateChanged) {
// Moving left will have a negative x, moving right positive
//
CGPoint translation = [recognizer translationInView:view.superview];
view.center = CGPointMake(view.center.x + translation.x, view.center.y);
// Reset so we always track the translation from the current view position
//
[recognizer setTranslation:CGPointZero inView:view.superview];
if (CGRectIntersectsRect(view.frame, otherView.frame)) {
// Translate by the same number of points to keep views in sync
//
otherView.center = CGPointMake(otherView.center.x + translation.x, otherView.center.y);
}
}
}
它可能不是正是你需要的,但它應該給你一個好主意,它可以怎麼做。
你是說像滑動? – booleanBoy
是的...我基本上在尋找一種方法來使用一個UIImageView來向左或向右滑動另一個,這取決於平移手勢的移動方向。 – Jason
你的意思是當用戶滑動時,當前圖像應該被下一個圖像推到左邊? – booleanBoy