【问题标题】:How can swipe custom five sub views using uigesture recoginzier in ios objective c如何在 ios Objective c 中使用 uigesturerecognizer 滑动自定义五个子视图
【发布时间】:2015-09-19 05:58:41
【问题描述】:

我正在使用 *UISwipeGesture 识别器,但现在只能使用两个视图,我们可以左右操作,但现在需要五个 UIview 显示为滑动,请帮帮我?

我只需要以下两个方向:

@左方向。 @正确的方向。

我想在我的视图中滑动五个视图,即向左和向右。

【问题讨论】:

  • @Naeem :我正在尝试将我的自定义视图显示为滑块。当用户触摸左方向时,它将显示第一个视图,当用户触摸右方向时,它将滑动第二个视图。所以我的 self.view 中总共有五个子视图
  • 不使用滑动手势,而是使用具有旋转效果的icarousel。

标签: ios objective-c uiswipegesturerecognizer


【解决方案1】:

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

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

    编辑(使用滑动手势的示例代码) 代码仅用于将您指向正确的方向。 我正在创建视图并将它们添加到 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--;
        }
    }

【讨论】:

  • 能否分享一下 SwipGestureRecognizer 的代码,请创建为 poc 并分享。
  • @MukundRaj 我添加了一些示例代码,在视图控制器中添加这三个方法和属性,看看左右滑动是如何工作的
  • 感谢分享代码现在正在工作,但我必须在特定视图上显示数据,如下所示:@ first view : Map @ SecondView : tableViewData @ThirdView : Images 等,所以我不能要理解上面的代码,请您以简单的格式分享代码。
  • 以上代码非常简单,这也是您解决问题的方法,您的向左滑动和向右滑动方法将保持不变,您根本不需要更改它们......只有您创建视图对象并将它们添加到数组的位置将发生变化,我将用更多的 cmets 更新我的答案以帮助您
  • @MukundRaj 我有更新代码,它不完全是你想要的,但它是你可以做到的一种方式
猜你喜欢
  • 1970-01-01
  • 2013-10-10
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多