我可以在另一个UIScrollView中放置一个UIScrollView吗

时间:2022-11-23 11:36:58

I have a UIScrollView which scrolls only in vertical direction, I need to place UIScrollView which can move horizontally, like the AppStore application in apple devices. I don't want to us UICollectionView since I have static data and I have to only 3 horizontal UIScrollView

我有一个UIScrollView,它只在垂直方向滚动,我需要放置可以水平移动的UIScrollView,就像苹果设备中的AppStore应用程序一样。我不想给我们UICollectionView因为我有静态数据而且我只需要3个水平UIScrollView

4 个解决方案

#1


4  

Yes you can. As UIScrollView subclasses UIView it will behave as expected when adding subviews. Each scroll view will enable scrolling based on its contentSize property.

是的,你可以。作为UIScrollView子类的UIView,它在添加子视图时将按照预期的方式运行。每个滚动视图将启用基于其内容大小属性的滚动。

Objective-C:

objective - c:

UIScrollView *horizontalScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40.0, 40.0, 300.0, 300.0)];
horizontalScrollView.backgroundColor = [UIColor lightGrayColor];
horizontalScrollView.contentSize = CGSizeMake(2000.0, 300.0);
[self.view addSubview:horizontalScrollView];

UIScrollView *verticalScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40.0, 40.0, 220.0, 220.0)];
verticalScrollView.backgroundColor = [UIColor greenColor];
verticalScrollView.contentSize = CGSizeMake(220.0, 2000.0);
[horizontalScrollView addSubview:verticalScrollView];

Swift:

迅速:

let horizontalScrollView = UIScrollView(frame: CGRectMake(40.0, 40.0, 300.0, 300.0))
horizontalScrollView.backgroundColor = UIColor.lightGrayColor()
horizontalScrollView.contentSize = CGSizeMake(2000.0, 300.0)
self.view.addSubview(horizontalScrollView)

let verticalScrollView = UIScrollView(frame: CGRectMake(40.0, 40.0, 220.0, 220.0))
verticalScrollView.backgroundColor = UIColor.greenColor()
verticalScrollView.contentSize = CGSizeMake(220.0, 2000.0)
horizontalScrollView.addSubview(verticalScrollView)

我可以在另一个UIScrollView中放置一个UIScrollView吗

#2


3  

Yes you can. But you need to differentiate the scroll views in scroll view delegate methods. Either you can use tags or if you are declaring them as global variables in the entire class,you can directly access them with their names.Eg:

是的,你可以。但是需要区分滚动视图委托方法中的滚动视图。您可以使用标记,或者如果您将它们声明为整个类中的全局变量,您可以使用它们的名称直接访问它们。

UIScrollView *parentScrollView = [[UIScrollView alloc] init];
parentScrollView.delegate = self;
[self.view addSubview:parentScrollView];

UIScrollView *childScrollView = [[UIScrollView alloc] init];
childScrollView.delegate = self;
[parentScrollView addSubview:childScrollView];

Now inside delegate method you can check for the scroll view like

现在在委托方法中,你可以检查滚动视图

if(scrollview == parentScrollview)
{
// do your actions
}

This is possible only if your scroll view objects are global to the class. You can also give a tag and check for the tag in scroll view delegate method like

只有当您的滚动视图对象对类是全局的时,这才可能实现。您还可以提供一个标记并检查滚动视图委托方法中的标记

parentScrollView. tag = 101;

And in scroll view delegate method

在滚动视图委托方法中

if(scrollview.tag = 101)
    {
    // do your actions
    }

#3


2  

yes it is possible but you have to maintain tag of scrollview for handling delegate methods of scroll view.

是的,这是可能的,但是您必须维护scrollview的标记,以便处理滚动视图的委托方法。

#4


2  

UIScrollView has a property called scrollEnabled, which you can set to NO to disable scrolling in your parent scroll view.

UIScrollView有一个名为scrollEnabled的属性,您可以将其设置为NO以禁用父滚动视图中的滚动。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if(scrollView == innerView)
        outerView.scrollEnabled = NO;
    else
        outerView.scrollEnabled = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView == innerView)
    {
         if(innerView.contentOffset.x + innerView.frame.size.width == innerView.contentSize.width)
         {
              outerView.scrollEnabled = NO;
         }
         else
         {
              outerView.scrollEnabled = YES;
         }
    }
}

Or else you can go through the below link.

或者你可以通过下面的链接。

#1


4  

Yes you can. As UIScrollView subclasses UIView it will behave as expected when adding subviews. Each scroll view will enable scrolling based on its contentSize property.

是的,你可以。作为UIScrollView子类的UIView,它在添加子视图时将按照预期的方式运行。每个滚动视图将启用基于其内容大小属性的滚动。

Objective-C:

objective - c:

UIScrollView *horizontalScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40.0, 40.0, 300.0, 300.0)];
horizontalScrollView.backgroundColor = [UIColor lightGrayColor];
horizontalScrollView.contentSize = CGSizeMake(2000.0, 300.0);
[self.view addSubview:horizontalScrollView];

UIScrollView *verticalScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40.0, 40.0, 220.0, 220.0)];
verticalScrollView.backgroundColor = [UIColor greenColor];
verticalScrollView.contentSize = CGSizeMake(220.0, 2000.0);
[horizontalScrollView addSubview:verticalScrollView];

Swift:

迅速:

let horizontalScrollView = UIScrollView(frame: CGRectMake(40.0, 40.0, 300.0, 300.0))
horizontalScrollView.backgroundColor = UIColor.lightGrayColor()
horizontalScrollView.contentSize = CGSizeMake(2000.0, 300.0)
self.view.addSubview(horizontalScrollView)

let verticalScrollView = UIScrollView(frame: CGRectMake(40.0, 40.0, 220.0, 220.0))
verticalScrollView.backgroundColor = UIColor.greenColor()
verticalScrollView.contentSize = CGSizeMake(220.0, 2000.0)
horizontalScrollView.addSubview(verticalScrollView)

我可以在另一个UIScrollView中放置一个UIScrollView吗

#2


3  

Yes you can. But you need to differentiate the scroll views in scroll view delegate methods. Either you can use tags or if you are declaring them as global variables in the entire class,you can directly access them with their names.Eg:

是的,你可以。但是需要区分滚动视图委托方法中的滚动视图。您可以使用标记,或者如果您将它们声明为整个类中的全局变量,您可以使用它们的名称直接访问它们。

UIScrollView *parentScrollView = [[UIScrollView alloc] init];
parentScrollView.delegate = self;
[self.view addSubview:parentScrollView];

UIScrollView *childScrollView = [[UIScrollView alloc] init];
childScrollView.delegate = self;
[parentScrollView addSubview:childScrollView];

Now inside delegate method you can check for the scroll view like

现在在委托方法中,你可以检查滚动视图

if(scrollview == parentScrollview)
{
// do your actions
}

This is possible only if your scroll view objects are global to the class. You can also give a tag and check for the tag in scroll view delegate method like

只有当您的滚动视图对象对类是全局的时,这才可能实现。您还可以提供一个标记并检查滚动视图委托方法中的标记

parentScrollView. tag = 101;

And in scroll view delegate method

在滚动视图委托方法中

if(scrollview.tag = 101)
    {
    // do your actions
    }

#3


2  

yes it is possible but you have to maintain tag of scrollview for handling delegate methods of scroll view.

是的,这是可能的,但是您必须维护scrollview的标记,以便处理滚动视图的委托方法。

#4


2  

UIScrollView has a property called scrollEnabled, which you can set to NO to disable scrolling in your parent scroll view.

UIScrollView有一个名为scrollEnabled的属性,您可以将其设置为NO以禁用父滚动视图中的滚动。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if(scrollView == innerView)
        outerView.scrollEnabled = NO;
    else
        outerView.scrollEnabled = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView == innerView)
    {
         if(innerView.contentOffset.x + innerView.frame.size.width == innerView.contentSize.width)
         {
              outerView.scrollEnabled = NO;
         }
         else
         {
              outerView.scrollEnabled = YES;
         }
    }
}

Or else you can go through the below link.

或者你可以通过下面的链接。