纯代码实现横向滚动的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

时间: 2024-08-29 05:09:41

纯代码实现横向滚动的UIScrollView的相关文章

滚动视图 UIScrollView

UIScrollView:提供可以显?示?大于应?用窗??口的内容功能的控件, ?用户可以通过?手势使内容滚动和缩放,从?而查 看全部内容. 初始化一个UIScrollView的对象 1 UIScrollView *_scroll=[[UIScrollView alloc] init]; 首先介绍一下UIScrollView这个类的基本属性 属性:  contentSize     滚动视图的内容大小 //UIScrollView能够滚动的前提是contentsize大于大的本身尺寸.即大于fr

autoLyout纯代码适配

前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 window的size固定为(320,480) 我们只需要简单计算一下相对位置就好了 在iphone4-iphone4s时代 苹果推出了retina屏 但是给了码农们非常大的福利:window的size不变 在iphone5-iphone5s时代 window的size变了(320,568) 这时auto

jQuery实现文字横向滚动效果

HTML代码: <div id="aaa" style="width:100px; position:relative; white-space:nowrap; overflow:hidden; height:20px;"> <div id="noticeList" style="position:absolute; top:0; height:20px;"> <span>jQuery文字横

(源码推荐)快速开发10几天时间纯代码高仿《内涵段子》

这个项目是用OC编写,如果有的朋友已经下载下来看了这个项目, 就会意识到这个项目没有一个storyboard或者是nib,不是因为不喜欢用storyboard或者nib,而是因为一直以来就想用纯代码写个项目,(好远大的梦想..开玩笑的..),但是项目是写出来的,光想不做不写是不行的,所以我就开始我的”内涵之旅“了. Github 地址 https://github.com/Charlesyaoxin/NeiHanDuanZI 日志: 8.30号:没怎么做东西,就是搭建了项目的架构,拉入了之前经常

4个74HC595级联控制16x16点阵横向滚动带仿真(二)

废话 续上篇 <4个74HC595级联控制16x16点阵横向滚动带仿真(一)> 将横向滚动的程序分享给QQ群里后,群内的晚秋兄弟提出了另一种思路——采用行扫描.移位方式做滚动,于是熬夜将其实现了一下.仿真程序还是跟前篇一样,不再赘述. 效果预览 代码 /** ********************************************************** ****** Copyright(C), 2010-2016,吐泡泡的虾 ****** ****************

解决iScroll横向滚动区域无法拉动页面的问题

近期项目中使用iScroll遇到一个问题,在设定wrapper为横向滚动时,如果你手指放在该区域,将无法拉动页面,也就是说该区域取消了默认事件.这个体验是实在是无法接受,特别是页面中有多个横向滚动区域时,很容易触碰到这种区域,这时用户将觉得页面很卡. Google搜了一下,看来很多人都为这个问题而烦恼.有高人给出了解决方案,在 这里 可以找到. 代码如下: myScroll = new iScroll('scrollpanel', { // other options go here... vS

web开发 小方法1-禁止横向滚动

最近学了学做了个公司的网站  总结了一些小方法 比如取消横向滚动条并禁止的横向滚动 这样就可以吧超出的切掉让网页更和谐 在body 标签 body{ text-align: center; overflow:scroll; overflow-x:hidden} 加入该代码就行  overflow属性详情可以去 w3cSchool 或者菜鸟教程

scroll-view——小程序横向滚动

这是官方给的布局代码 <view class="section"> <view class="section__title">vertical scroll</view> <scroll-view scroll-y style="height: 200px;" bindscrolltoupper="upper" bindscrolltolower="lower" b

Android 横向列表GridView 实现横向滚动

Android 横向列表实现,可左右滑动,如下图 1.主界面布局代码:activity_main.xml a.包裹HorizontalScrollView控件是GirdView横向滚动的基本条件b.GirdView外包裹LinearLayout是java代码中参数设置的必要条件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schema