UISwipeGestureRecognizer方向(左/右),带有4个子视图

时间:2014-03-04 19:41:54

标签: ios xcode gesture-recognition

我使用此代码在两个视图之间使用UISwipeGestureRecognizer Direction(左/右)进行滑动

我希望对4个视图做同样的事情:

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

1 个答案:

答案 0 :(得分: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
    }