2015-09-18 38 views
0

我使用* UISwipeGesture识别器,但现在的工作只有两个观点,我们可以做到这一点的左,右,但现在Ineed 5 的UIView显示为刷卡烦请帮我?iOS中使用uigesture recoginzier如何可以刷卡定制五个子视图目标C

我只需要两个方向如下:

@ leftft direction。 正确的方向。

我想滑动五个视图,即左视图和右视图。

+0

你已经尝试 – Naeem

+0

@Naeem将增加手势识别:我试图到我的自定义视图来显示为滑块。当用户触摸向左方向时,它将显示第一个视图,而当用户触摸正确方向时,则会刷新第二个视图。所以我总共有五个子视图在我的self.view – MRJ

+0

而不是使用滑动手势,使用旋转效果的卡集。 –

回答

1

您可以通过以下方式在iOS中实现此目的。

  1. 使用UIScrollView添加要刷卡的意见,然后设置contentSize,并设置enablePaging = YES,它的完成。
  2. 如果您的视图位于视图控制器中,您可以使用UIPageViewController并添加set viewControllers属性并设置滚动方向(垂直或水平)及其完成。
  3. 如果你想通过SwipeGestureRecognizer来处理它,你仍然可以做到这一点,在视图中添加手势识别器,然后添加所有想要扫过的视图作为其子视图,然后在左右滑动或前面的视图通过链接它的框架,跟踪哪些应该是当前可见视图,以及什么是前一个视图和下一个视图,您可以保留一个整数,并在开始时将其值设置为0,并在向左滑动时递增1向右滑动减量,应该始终为0 < =索引< 5.

    编辑(实施例代码与滑动手势)代码只是用于指向你正确的方向。 我创建视图并将它们添加到viewDidAppear的子视图中,绝对可以做到任何你想要的位置(也可以在viewDidLoad中)。

添加两个属性

@property (nonatomic, strong) NSMutableArray *views;// to hold subviews 
@property (nonatomic, assign) NSInteger index;// to keep track which view is currently visible 
@property (nonatomic, assign) BOOL viewsCreated; 

现在我要补充一些意见,并有上海华

 - (void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 
     // Adding an if condition so we create views only once other wise you can take this code to viewDidLoad method 
     if (!_viewCreated) 
     { 
      _viewsCreated = YES; 
      int xPos = 0; 
      _views = [NSMutableArray new]; 
      for (int i = 0; i< 5; i ++) 
      { 
       CGRect frame = CGRectMake(xPos, 0, self.view.bounds.size.width, self.view.bounds.size.height); 
       if (i == 0) 
       { 
        // Create MapView object and add it to _views array 
        MKMapView *mapView = [[MKMapView alloc] initWithFrame:frame]; 
        [self.view addSubview:mapView]; 
        [_views addObject:mapView]; 
       } 
       if (i == 1) 
       { 
        // Create tableView object and add it to _views array 
        UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain]; 
        //set tableView delegate and datasource 
        tableView.delegate = self; 
        tableView.dataSource = self; 
        [_views addObject:tableView]; 
        [self.view addSubview:tableView]; 
       } 
       if (i == 2) 
       { 
        // Create an imageView and add it to _views array 
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame]; 
        //Set image 
        //imageView.image = <image>; 
        [_views addObject:imageView]; 
        [self.view addSubview:imageView]; 
       } 
       if (i == 3) 
       { 
        //Create whatever view you want and add it to array and subView as to self.view as well 
       } 
       if (i == 4) 
       { 
        //Create whatever view you want and add it to array and subView as to self.view as well 
       } 
      } 

     UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeLeft:)]; 
     leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; 
     [self.view addGestureRecognizer:leftSwipe]; 

     UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeRight:)]; 
     rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; 
     [self.view addGestureRecognizer:rightSwipe]; 
    } 

} 

- (void)onSwipeLeft:(UISwipeGestureRecognizer*)swipe 
{ 
    if (_index<4) 
    { 
     UIView *currentView = [_views objectAtIndex:_index]; 
     CGRect frame = currentView.frame; 
     frame.origin.x = -self.view.bounds.size.width; 

     UIView *nextView = [_views objectAtIndex:_index+1]; 
     [UIView animateWithDuration:0.5 animations:^{ 
      currentView.frame = frame; 
      nextView.frame = self.view.bounds; 
     }]; 
     _index++; 
    } 
} 
    - (void)onSwipeRight:(UISwipeGestureRecognizer*)swipe 
    { 
     if (_index>1) 
     { 
      UIView *currentView = [_views objectAtIndex:_index]; 
      CGRect frame = currentView.frame; 
      frame.origin.x = self.view.bounds.size.width; 

      UIView *nextView = [_views objectAtIndex:_index-1]; 

      [UIView animateWithDuration:0.5 animations:^{ 
       currentView.frame = frame; 
       nextView.frame = self.view.bounds; 
      }]; 
      _index--; 
     } 
    } 
+0

您可以共享关于SwipGestureRecognizer的代码,请创建为poc和共享。 – MRJ

+0

@MukundRaj我添加了一些示例代码,在视图控制器中添加这三个方法和属性,看看左右滑动是如何工作的 –

+0

感谢分享代码现在正在工作,但我将不得不在特定视图上显示数据,如下所示:@第一视图:地图@ SecondView:tableViewData @ThirdView:图片等,所以我无法理解上面的代码,请你分享代码作为简单的格式。 – MRJ

相关问题