2014-03-04 39 views
0

我使用此代码与UISwipeGestureRecognizer方向(左/右)UISwipeGestureRecognizer方向(左/右)与4子视图

两个视图之间滑动,我想做同样的4次:

厂景 - > view2-> view3-> view4

厂景< -view2 < -view3 < -view4

谢谢大家帮忙

//........towards right Gesture recogniser for swiping.....// 
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[rightRecognizer setNumberOfTouchesRequired:1]; 
[cmmtView addGestureRecognizer:rightRecognizer]; 
[rightRecognizer release]; 

//........towards left Gesture recogniser for swiping.....// 
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
[leftRecognizer setNumberOfTouchesRequired:1]; 
[cmmtView addGestureRecognizer:leftRecognizer]; 
[leftRecognizer release]; 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer *)recognizer { 
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 
    // load a different viewController 
    [cmmtView addSubview:viewCmd2]; 
} else { 
    // load an even different viewController   
    [viewCmd removeFromSuperview]; 
    [viewCmd2 removeFromSuperview]; 
    [viewFocus removeFromSuperview]; 
    [viewWeb removeFromSuperview]; 
    [viewLive removeFromSuperview]; 
    [viewLivePatch removeFromSuperview]; 
    [viewLivePatchConsole removeFromSuperview];}} 

- (void)rightSwipeHandle:(UISwipeGestureRecognizer *)recognizer { 
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 
    // load a different viewController  
    [viewCmd2 addSubview:cmmtView]; 
} else { 
    // load an even different viewController 
    [viewCmd removeFromSuperview]; 
    [viewCmd2 removeFromSuperview]; 
    [viewFocus removeFromSuperview]; 
    [viewWeb removeFromSuperview]; 
    [viewLive removeFromSuperview]; 
    [viewLivePatch removeFromSuperview]; 
    [viewLivePatchConsole removeFromSuperview];}} 

回答

0

我不知道为什么你使用

if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { } 

如果手势识别方向已被设置为左,它会永远留。你的其他陈述永远不会运行。

如果你想按照你提到的方式来控制视图,我建议你声明一个viewArray和currentIndex属性,然后像这样处理它。

在您的.h

@property (strong, nonatomic) NSArray * viewArray; 
@property int currentViewIndex; 

在您的m

UIView * view1; // == yourView; 
UIView * view2; // == yourView; 
UIView * view3; // == yourView; 
UIView * view4; // == yourView; 
self.currentViewIndex = 0; 
self.viewArray = [[NSArray alloc]initWithObjects:view1, view2, view3, view4, nil]; 

然后,在你的处理程序,检索的意见这种方式。

- (void)leftSwipeHandle:(UISwipeGestureRecognizer *)recognizer { 
     if ((self.currentViewIndex + 1) < self.viewArray.count) { 
      UIView * viewCurrentlyShowing = self.viewArray[self.currentViewIndex]; 
      UIView * viewToShow = self.viewArray[self.currentViewIndex + 1]; 

      // do whatever with views 

      self.currentViewIndex = self.currentViewIndex + 1; 
     } 
     else { 
      // Reached end of views 
     } 
} 

} 

- (void) rightSwipeHandle:(UISwipeGestureRecognizer *)recognizer { 
    if ((self.currentViewIndex - 1) >= 0) { 
     UIView * viewCurrentlyShowing = self.viewArray[self.currentViewIndex]; 
     UIView * viewToShow = self.viewArray[self.currentViewIndex - 1]; 

     // do whatever with views 

     self.currentViewIndex = self.currentViewIndex - 1; 
    } 
    else { 
     // reached front of views 
    } 
+0

小问题.... 后4向左扫一个有 ***终止应用程序由于未捕获的异常 'NSRangeException',原因:“*** - [__ NSArrayI objectAtIndex:]:指数4 [0..3]' 我不明白为什么这行不行的... if(self.currentViewIndex> = self.viewArray.count)self.currentViewIndex = self.viewArray.count - 1 ; – jeff

+0

已更新为在获取viewToShow属性之前捕获索引范围。让我知道这是否适合你。 – Logan