uiscrollview怎么横向滚动在xib设置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uiscrollview怎么横向滚动在xib设置相关的知识,希望对你有一定的参考价值。
参考技术A 我有个方法不需要定时器,声明 我的UIScrollView是用作左右滚动的.
具体思路是在滚动视图的时减去坐标来模拟减速.
接口声明:
int
scrollCount; // 滚动次数
float lastScrollX; //
最后一次滚动的x坐标
/**
* 视图滚动改变中,降低滚动视图的速率
*/
-
(void)scrollViewDidScroll:(UIScrollView *)scrollView
//
-1表示滚动结束,不做处理
if(scrollCount == -1)
return ;
scrollCount ++; //记录滚动次数
// “计算速率”
if(scrollCount % 2 == 0)
float gap = scrollTabView.contentOffset.x -
lastScrollX;
// 左右移动时,各减少最多4个单位.
// 这里可以自己计算这个值
if(gap > 4)
gap = 4;
else if(gap <
-4)
gap = -4;
scrollView.contentOffset =
ccp(scrollView.contentOffset.x - gap, scrollView.contentOffset.y);
lastScrollX = scrollTabView.contentOffset.x;
-
(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
// 重置
scrollCount = 0;
lastScrollX = 0;
}
-
(void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
// 记录滚动结束
scrollCount =
-1;
}
基本就这样本回答被提问者采纳
纯代码实现横向滚动的UIScrollView
// 纯代码实现横向滚动的UIScrollView
// NeedListViewController.m
//
//
// Copyright © 2016年 All rights reserved.
//
#import "CFNeedListViewController.h"
#import "CFWaitingViewController.h"
#import "CFRespondedViewController.h"
#import "CFOrderdeViewController.h"
#import "CFCanceledViewController.h"
#import "UIBarButtonItem+Item.h"
@interface CFNeedListViewController ()<UIScrollViewDelegate>
/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIView *titlesView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIScrollView *contentView;
@end
@implementation CFNeedListViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏
[self setupNav];
// 初始化子控制器
[self setupChildVces];
// 设置顶部的标签栏
[self setupTitlesView];
// 底部的scrollView
[self setupContentView];
}
/**
* 初始化子控制器
*/
- (void)setupChildVces
{
CFWaitingViewController * waiting = [[CFWaitingViewController alloc] init];
waiting.title = @"待响应";
[self addChildViewController:waiting];
CFRespondedViewController *responded = [[CFRespondedViewController alloc] init];
responded.title = @"已响应";
[self addChildViewController:responded];
CFOrderdeViewController *ordered = [[CFOrderdeViewController alloc] init];
ordered.title = @"已达成订单";
[self addChildViewController:ordered];
CFCanceledViewController *canceled = [[CFCanceledViewController alloc] init];
canceled.title = @"已取消";
[self addChildViewController:canceled];
}
/**
* 设置顶部的标签栏
*/
- (void)setupTitlesView
{
// 标签栏整体
UIView *titlesView = [[UIView alloc] init];
titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
titlesView.width = self.view.width;
titlesView.height = CFTitlesViewH;
titlesView.y = 0;
[self.view addSubview:titlesView];
self.titlesView = titlesView;
// 底部的橙色指示器
UIView *indicatorView = [[UIView alloc] init];
indicatorView.backgroundColor = [UIColor orangeColor];
indicatorView.height = 2;
indicatorView.tag = -1;
indicatorView.y = titlesView.height - indicatorView.height;
self.indicatorView = indicatorView;
// 内部的子标签
CGFloat width = titlesView.width / self.childViewControllers.count;
CGFloat height = titlesView.height;
for (NSInteger i = 0; i<self.childViewControllers.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.tag = i;
button.height = height;
button.width = width;
button.x = i * width;
UIViewController * vc = self.childViewControllers[i];
[button setTitle:vc.title forState:UIControlStateNormal];
// [button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
[titlesView addSubview:button];
// 默认点击了第一个按钮
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
// 让按钮内部的label根据文字内容来计算尺寸
[button.titleLabel sizeToFit];
self.indicatorView.width = button.width;
self.indicatorView.centerX = button.centerX;
}
}
[titlesView addSubview:indicatorView];
}
- (void)titleClick:(UIButton *)button
{
// 修改按钮状态
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 动画
[UIView animateWithDuration:0.25 animations:^{
self.indicatorView.width = button.width;
self.indicatorView.centerX = button.centerX;
}];
// 滚动
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
/**
* 底部的scrollView
*/
- (void)setupContentView
{
// 不要自动调整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.frame = self.view.bounds;
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;
// 添加第一个控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
/**
* 设置导航栏
*/
- (void)setupNav
{
// 设置导航栏标题
self.title = @"我的发布";
// 设置导航栏右边的按钮
UIBarButtonItem * tag1 = [UIBarButtonItem itemWithImage:@"wshoucang" highImage:@"wshoucang" target:self action:@selector(tag1)];
UIBarButtonItem * tag2 = [UIBarButtonItem itemWithImage:@"wshoucang" highImage:@"wshoucang" target:self action:@selector(tag2)];
self.navigationItem.rightBarButtonItems = @[tag1,tag2];
}
#pragma mark - 两个右边UIBarButtonItem的点击事件
#warning 待开发!!!!!!!!
- (void)tag1
{
CFCLog(@"%s",__func__);
}
- (void)tag2
{
CFCLog(@"%s",__func__);
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// 当前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
// 取出子控制器
UIViewController *vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0; // 设置控制器view的y值为0(默认是20)
vc.view.height = scrollView.height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 点击按钮
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleClick:self.titlesView.subviews[index]];
}
@end
以上是关于uiscrollview怎么横向滚动在xib设置的主要内容,如果未能解决你的问题,请参考以下文章
纵向xib调整为横向时需要改变UIScrollView contentSize
在 UIView (XIB) 和滚动视图之上的 UIScrollView 不会滚动