xcode 在 iOS 中创建水平无限滚动菜单栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26831662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:08:40  来源:igfitidea点击:

Creation of a Horizontal Infinite Scrolling Menu Bar in iOS

iosobjective-cxcodeuser-interface

提问by lele0108

I am interested in creating a menu bar like this:

我有兴趣创建这样的菜单栏:

Menu Bar Example

菜单栏示例

From what I have been reading, I should be doing a UIScrollView. Are there any examples online that do this with infinite scrolling (wrapping around the menu bar) and the ability to have touch control? (i.e. swipe and it changes the menu bar).

根据我一直在阅读的内容,我应该做一个 UIScrollView。在线是否有任何示例可以通过无限滚动(环绕菜单栏)和触摸控制功能来执行此操作?(即滑动并更改菜单栏)。

Thanks!

谢谢!

回答by Azure Chen

Your request is very similar to mine. But I cannot find a good enough solution too. So I decide to create something to do that by myself.

你的要求和我的很相似。但我也找不到足够好的解决方案。所以我决定自己创造一些东西来做到这一点。

The ACTabScrollViewsupports some different gestures. And the tabsand pageswill always scroll synchronously.

ACTabScrollView支持一些不同的手势。并且tabspages将始终同步滚动。

  • Swipepages normally
  • Dragtabs can quickly move pages
  • Clicka tab to change to that page
  • Swipe正常页面
  • Drag标签可以快速移动页面
  • Click要更改到该页面的选项卡

demo1demo2demo3

演示1演示2演示3

The Infinite Scrollingfeature is not ready yet, but I will keep to implement.

Infinite Scrolling功能尚未准备就绪,但我会继续实施。

I use the similar UI as examples in this project. I don't know what the app is, but if using it as an example bother anyone. Contact me and I will immediately remove that.

我在这个项目中使用了类似的 UI 作为示例。我不知道该应用程序是什么,但如果使用它作为示例会打扰任何人。联系我,我会立即删除它。

Feel free to give me any suggestion to improve it :)

随时给我任何改进它的建议:)

回答by Shrikant K

I hope this code will work for you.

我希望这段代码对你有用。

- (void)viewDidLoad
{
 [super viewDidLoad];
 [self createHorizontalScroll];
}

- (void)createHorizontalScroll
{
   UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 80)];

   int buttonX = 0;
   for (int i = 0; i < 5; i++)
   {
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, 100, 60)];
    [button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
    [scrollView addSubview:button];
    buttonX = buttonX+button.frame.size.width;
    [button addTarget:self
               action:@selector(changeView:)
     forControlEvents:UIControlEventTouchUpInside];

   }

scrollView.contentSize = CGSizeMake(buttonX, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];

}

 -(void)changeView:(UIButton*)sender
 {
   NSLog(@"I Clicked a button %ld",(long)sender.tag);
 }

You can set scroll content as much you want and menu buttons.Change the view according to the button click. Here the screen shot of the above code

您可以根据需要设置滚动内容和菜单按钮。根据单击按钮更改视图。这是上面代码的屏幕截图

enter image description here

在此处输入图片说明

回答by Ramprasad A

I hope this might help you. This doesn't have more animation but works as you expected.

我希望这可以帮助你。这没有更多动画,但按您的预期工作。

You can make use of CollectionView in this to scroll Horizontally with menu options as CollectionView Cell. For achieving the left and right swiping we can use UISwapeGuestureRecognizers and can scroll between pages.

您可以在此使用 CollectionView 使用菜单选项作为 CollectionView Cell 水平滚动。为了实现左右滑动,我们可以使用 UISwapeGuestureRecognizers 并且可以在页面之间滚动。

Here is how i have implemented it using CollectionView for ScrollableMenu as tabs in the top of the screen and the Pages in the bottom of the menu using Container View.

这是我如何使用 CollectionView for ScrollableMenu 作为屏幕顶部的选项卡和使用容器视图的菜单底部的页面来实现它。

You can download this project and check for getting idea about the implementation form the below link.

您可以下载此项目并通过以下链接查看有关实施的想法。

Horizontal Scrollable Menu

水平滚动菜单

Hope this might help you. This is implemented with lates Swift 4.0 and X-Code 9.2

希望这可以帮助你。这是用最新的 Swift 4.0 和 X-Code 9.2 实现的

Happy Coding..

快乐编码..