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
Creation of a Horizontal Infinite Scrolling Menu Bar in iOS
提问by lele0108
I am interested in creating a menu bar like this:
我有兴趣创建这样的菜单栏:
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 tabs
and pages
will always scroll synchronously.
该ACTabScrollView支持一些不同的手势。并且tabs
和pages
将始终同步滚动。
Swipe
pages normallyDrag
tabs can quickly move pagesClick
a tab to change to that page
Swipe
正常页面Drag
标签可以快速移动页面Click
要更改到该页面的选项卡
The Infinite Scrolling
feature 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
您可以根据需要设置滚动内容和菜单按钮。根据单击按钮更改视图。这是上面代码的屏幕截图
回答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.
您可以下载此项目并通过以下链接查看有关实施的想法。
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..
快乐编码..