- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章iOS自定义可展示、交互的scrollView滚动条由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
上一篇简述了封装上拉、下拉刷新控件,本篇在此基础上添加了一个自定义的scrollview滚动条,可展示、交互,首先看一下效果图:
简单阐述一下实现逻辑:自定义滚动条视图继承uiview,添加滚动条滑动事件、其他区域点击事件,通过代理方法与列表关联。在列表刷新完成及scrollview代理方法中更新滚动条.
简单说一下计算逻辑,如上图(原谅博主的图)所示,其中b、c、d是已知的。首先计算滚动条的高度a,理想情况下它与整个滚动区域b的比值应该等于scrollview的展示区域b与scrollview的内容高度d的比值,就是 a/b = b/d,即 a = b*b/d,也是就代码中的“_scrollbar.barheight = pow(tableview.bounds.size.height,2) / tableview.contentsize.height;”这句话.
既然是理想情况,就有特殊情况,首先如果内容高度d小于展示区域b,就是说不需要滑动时,这里可以有两种处理,第一种是隐藏滚动条,第二种是将滚动条高度设为与滚动区域一致,方便观察,这里使用后一种。还有一种特殊情况就是,如果内容区域d无限增大,则滚动条高度a无限减小,所以需要给定一个最小高度限制.
好了,上面计算出滚动条高度a,然后计算滚动条y向位置x,很容易看出来 x/b = c/d,正常情况下这是没有问题的,但是当滚动条高度非常小,小于我们设定的最小高度时就会有误差,那么换另一种写法 x/(b-a) = c/(d-b),即 x = (b-a)*c/(d-b),代码中“_scrollbar.yposition = (_scrollbar.bounds.size.height - _scrollbar.barheight) *_tableview.contentoffset.y / (_tableview.contentsize.height -_scrollbar.bounds.size.height);”这句话。那么在scrollview代理方法中更新这两项就实现了滚动条高度根据scrollview内容增减,并根据scrollview滑动而移动.
最后在我们自定义滚动条的代理方法中设置scrollview的contentoffset,即可实现scrollview随着滚动条的点击滑动而移动。计算方法与上面一致 x/(b-a) = c/(d-b),区别是这次动条y向位置x是已知的,scrollview的y向偏移量c是未知的,即 c = (d-b)*x/(b-a),代码中“[_tableviewsetcontentoffset:cgpointmake(0, (_tableview.contentsize.height -_scrollbar.bounds.size.height) * scrollbar.yposition / (_scrollbar.bounds.size.height - _scrollbar.barheight))];”这句话.
下面贴上相关代码:
控制器viewcontroller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller
@end
/*** ---------------分割线--------------- ***/
#import "viewcontroller.h"
#import "hwrefresh.h"
#import "hwscrollbar.h"
@interface viewcontroller ()<uitableviewdatasource, uitableviewdelegate, hwscrollbardelegate>
@property (nonatomic, strong) nsmutablearray *array;
@property (nonatomic, strong) uitableview *tableview;
@property (nonatomic, weak) hwscrollbar *scrollbar;
@property (nonatomic, weak) hwscrollbar *tablebar;
@property (nonatomic, assign) nsinteger page;
@end
@implementation viewcontroller
- (nsmutablearray *)array
{
if
(!_array) {
_array = [nsmutablearray array];
}
return
_array;
}
- (
void
)viewdidload {
[super viewdidload];
self.view.backgroundcolor = [uicolor blackcolor];
self.page = 1;
//模拟获取信息
[self getinfo];
//创建控件
[self creatcontrol];
//添加头部刷新
[self addheaderrefresh];
//添加尾部刷新
[self addfooterrefresh];
}
- (
void
)getinfo
{
nsarray *array = @[@
"ios hero博客"
, @
"ios hero博客"
, @
"ios hero博客"
, @
"ios hero博客"
, @
"http://blog.csdn.net/hero_wqb"
, @
"ios hero博客"
, @
"ios hero博客"
, @
"ios hero博客"
, @
"ios hero博客"
, @
"http://blog.csdn.net/hero_wqb"
];
dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(1.0f * nsec_per_sec)), dispatch_get_main_queue(), ^{
if
(self.page == 1) {
self.array = [nsmutablearray arraywitharray:array];
}
else
{
[self.array addobjectsfromarray:array];
}
[_tableview reloaddata];
[_tableview headerendrefreshing];
[_tableview footerendrefreshing];
nslog(@
"已经刷新好了"
);
});
}
- (
void
)creatcontrol
{
//列表视图
_tableview = [[uitableview alloc] initwithframe:cgrectmake(20, 64, [[uiscreen mainscreen] bounds].size.width - 100, [[uiscreen mainscreen] bounds].size.height - 164) style:uitableviewstyleplain];
_tableview.datasource = self;
_tableview.delegate = self;
[self.view addsubview:_tableview];
//滚动展示条
hwscrollbar *tablebar = [[hwscrollbar alloc] initwithframe:cgrectmake(cgrectgetmaxx(_tableview.frame), cgrectgetminy(_tableview.frame), 5, _tableview.bounds.size.height)];
tablebar.forecolor = [uicolor greencolor];
tablebar.backcolor = [uicolor graycolor];
tablebar.userinteractionenabled = no;
[self.view addsubview:tablebar];
_tablebar = tablebar;
//滚动条
hwscrollbar *scrollbar = [[hwscrollbar alloc] initwithframe:cgrectmake(cgrectgetmaxx(_tableview.frame) + 20, cgrectgetminy(_tableview.frame), 40, _tableview.bounds.size.height)];
scrollbar.delegate = self;
scrollbar.minbarheight = 80;
[self.view addsubview:scrollbar];
_scrollbar = scrollbar;
}
- (
void
)addheaderrefresh
{
__weak typeof(self) weakself = self;
[_tableview addheaderrefreshwithcallback:^{
__strong typeof(weakself) strongself = weakself;
strongself.page = 1;
[strongself getinfo];
}];
}
- (
void
)addfooterrefresh
{
__weak typeof(self) weakself = self;
[_tableview addfooterrefreshwithcallback:^{
__strong typeof(weakself) strongself = weakself;
strongself.page ++;
[strongself getinfo];
}];
}
#pragma mark - uitableviewdatasource
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
return
self.array.count;
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
static
nsstring *identifier = @
"refreshtest"
;
uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:identifier];
if
(!cell) {
cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier];
}
cell.textlabel.text = [_array[indexpath.row] stringbyappendingstring:[nsstring stringwithformat:@
"_%ld"
, indexpath.row]];
return
cell;
}
- (
void
)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath
{
dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(0.1f * nsec_per_sec)), dispatch_get_main_queue(), ^{
//更新滚动条高度
if
(tableview.contentsize.height <= tableview.bounds.size.height) {
_scrollbar.barheight = tableview.bounds.size.height;
_tablebar.barheight = tableview.bounds.size.height;
}
else
{
_scrollbar.barheight =
pow
(tableview.bounds.size.height, 2) / tableview.contentsize.height;
_tablebar.barheight =
pow
(tableview.bounds.size.height, 2) / tableview.contentsize.height;
}
//更新滚动条y向位置
_scrollbar.yposition = (_scrollbar.bounds.size.height - _scrollbar.barheight) * _tableview.contentoffset.y / (_tableview.contentsize.height - _scrollbar.bounds.size.height);
_tablebar.yposition = (_tablebar.bounds.size.height - _tablebar.barheight) * _tableview.contentoffset.y / (_tableview.contentsize.height - _tablebar.bounds.size.height);
});
}
- (
void
)scrollviewdidscroll:(uiscrollview *)scrollview
{
//滑动到底部自动刷新
if
(_tableview.contentsize.height > _tableview.frame.size.height && _tableview.contentoffset.y + _tableview.frame.size.height > _tableview.contentsize.height - 40 && _page < 50) {
[_tableview footerbeginrefreshing];
}
//更新滚动条位置
_scrollbar.yposition = (_scrollbar.bounds.size.height - _scrollbar.barheight) * scrollview.contentoffset.y / (scrollview.contentsize.height - _scrollbar.bounds.size.height);
_tablebar.yposition = (_tablebar.bounds.size.height - _tablebar.barheight) * scrollview.contentoffset.y / (scrollview.contentsize.height - _tablebar.bounds.size.height);
}
#pragma mark - sxscrollbardelegate
- (
void
)scrollbardidscroll:(hwscrollbar *)scrollbar
{
[_tableview setcontentoffset:cgpointmake(0, (_tableview.contentsize.height - _scrollbar.bounds.size.height) * scrollbar.yposition / (_scrollbar.bounds.size.height - _scrollbar.barheight))];
}
- (
void
)scrollbartouchaction:(hwscrollbar *)scrollbar
{
[uiview animatewithduration:scrollbar.barmoveduration animations:^{
[_tableview setcontentoffset:cgpointmake(0, (_tableview.contentsize.height - _scrollbar.bounds.size.height) * scrollbar.yposition / (_scrollbar.bounds.size.height - _scrollbar.barheight))];
}];
}
@end
|
自定义滚动条hwscrollbar:
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#import <uikit/uikit.h>
@
class
hwscrollbar;
@protocol hwscrollbardelegate <nsobject>
//滚动条滑动代理事件
- (
void
)scrollbardidscroll:(hwscrollbar *)scrollbar;
//滚动条点击代理事件
- (
void
)scrollbartouchaction:(hwscrollbar *)scrollbar;
@end
@interface hwscrollbar : uiview
//背景色
@property (nonatomic, strong) uicolor *backcolor;
//前景色
@property (nonatomic, strong) uicolor *forecolor;
//滚动动画时长
@property (nonatomic, assign) cgfloat barmoveduration;
//限制滚动条最小高度
@property (nonatomic, assign) cgfloat minbarheight;
//滚动条实际高度
@property (nonatomic, assign) cgfloat barheight;
//滚动条y向位置
@property (nonatomic, assign) cgfloat yposition;
//代理
@property (nonatomic, weak) id<hwscrollbardelegate> delegate;
@end
/*** ---------------分割线--------------- ***/
#import "hwscrollbar.h"
#import "uicolor+hw.h"
@interface hwscrollbar ()
@property (nonatomic, weak) uiview *scrollbar;
@property (nonatomic, weak) uiview *backview;
@end
@implementation hwscrollbar
- (instancetype)initwithframe:(cgrect)frame
{
if
(self = [super initwithframe:frame]) {
//初始化设置
[self initinfo];
//创建控件
[self creatcontrol];
//添加手势
[self addswipegesture];
}
return
self;
}
- (
void
)initinfo
{
_minbarheight = 40.0f;
_barmoveduration = 0.25f;
_forecolor = [uicolor colorwithhexstring:@
"#2f9cd4"
];
_backcolor = [uicolor colorwithhexstring:@
"#e6e6e6"
];
self.layer.cornerradius = self.bounds.size.width * 0.5;
self.layer.maskstobounds = yes;
self.backgroundcolor = _backcolor;
}
- (
void
)creatcontrol
{
//背景视图
uiview *backview = [[uiview alloc] initwithframe:self.bounds];
[self addsubview:backview];
_backview = backview;
//滚动条
uiview *scrollbar = [[uiview alloc] initwithframe:cgrectmake(0, 0, self.bounds.size.width, self.bounds.size.height)];
scrollbar.backgroundcolor = _forecolor;
scrollbar.layer.cornerradius = self.bounds.size.width * 0.5;
scrollbar.layer.maskstobounds = yes;
[self addsubview:scrollbar];
_scrollbar = scrollbar;
}
- (
void
)addswipegesture
{
//添加点击手势
uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(handletap:)];
[_backview addgesturerecognizer:tap];
//添加滚动条滑动手势
uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)];
[_scrollbar addgesturerecognizer:pan];
}
- (
void
)setforecolor:(uicolor *)forecolor
{
_forecolor = forecolor;
_scrollbar.backgroundcolor = _forecolor;
}
- (
void
)setbackcolor:(uicolor *)backcolor
{
_backcolor = backcolor;
self.backgroundcolor = backcolor;
}
- (
void
)setbarheight:(cgfloat)barheight
{
_barheight = barheight > _minbarheight ? barheight : _minbarheight;
cgrect temframe = _scrollbar.frame;
temframe.size.height = _barheight;
_scrollbar.frame = temframe;
}
- (
void
)setyposition:(cgfloat)yposition
{
_yposition = yposition;
cgrect temframe = _scrollbar.frame;
temframe.origin.y = yposition;
_scrollbar.frame = temframe;
}
- (
void
)handlepan:(uipangesturerecognizer *)sender
{
//获取偏移量
cgfloat movey = [sender translationinview:self].y;
//重置偏移量,避免下次获取到的是原基础的增量
[sender settranslation:cgpointmake(0, 0) inview:self];
//在顶部上滑或底部下滑直接返回
if
((_yposition <= 0 && movey <= 0) || (_yposition >= self.bounds.size.height - _barheight && movey >= 0))
return
;
//赋值
self.yposition += movey;
//防止瞬间大偏移量滑动影响显示效果
if
(_yposition < 0) self.yposition = 0;
if
(_yposition > self.bounds.size.height - _barheight && movey >= 0) self.yposition = self.bounds.size.height - _barheight;
//代理
if
(_delegate && [_delegate respondstoselector:@selector(scrollbardidscroll:)]) {
[_delegate scrollbardidscroll:self];
}
}
- (
void
)handletap:(uitapgesturerecognizer *)sender
{
//点击滚动条返回
if
(sender.view == _scrollbar)
return
;
//获取点击的位置
cgfloat positiony = [sender locationinview:self].y;
//赋值
[uiview animatewithduration:_barmoveduration animations:^{
self.yposition = positiony > _yposition ? positiony - _barheight : positiony;
}];
//代理
if
(_delegate && [_delegate respondstoselector:@selector(scrollbartouchaction:)]) {
[_delegate scrollbartouchaction:self];
}
}
@end
|
demo 下载链接 。
猜你喜欢:自定义水平滚动条、进度条.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/hero_wqb/article/details/73610132 。
最后此篇关于iOS自定义可展示、交互的scrollView滚动条的文章就讲到这里了,如果你想了解更多关于iOS自定义可展示、交互的scrollView滚动条的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如果附加了 'not-scroll' 类,我希望我的 body 不滚动,否则它应该正常工作。 我已经搜索这个问题两天了,但找不到任何适合我的解决方案。 我想要的是向 body 添加一个 class,并
我发现似乎是 iOS Safari 中的错误(我正在 iOS 8 上进行测试)。当绝对定位的 iFrame 漂浮在一段可滚动内容上方时,滚动 iFrame 也会滚动下面的内容。以下 HTML (ava
我有以下代码来显示一系列投资组合图片,这些图片以 SVG 格式存储在滚动 div 中: 在 Safari 中滚动使用两根手指或鼠标滚轮当光标位于 SVG 之一上时不起作用。 该页
我想用 javascript 做的是: 一旦你向下滚动页面,将#sidebar-box-fixed 的位置从 position: relative; 更改为定位:固定;。改回position:rela
我对 Elasticsearch 的滚动功能有点困惑。在 elasticsearch 中,每当用户在结果集上滚动时,是否可以每次调用搜索 API?来自文档 "search_type" => "scan
我试图做到这一点,以便当我向上或向下滚动页面时,它会运行不同的相应功能。我发现了一个类似的问题here但我已经尝试了他们的答案并且没有运气。 注意:此页面没有正常显示的滚动条。没有地方可以滚动。 bo
(C语言,GTK库) 在我的表单上,我有一个 GtkDrawingArea 小部件,我在上面使用 Cairo 绘制 GdkPixbufs(从文件加载)。我想要完成的是能够在窗口大小保持固定的情况下使用
最近我一直在尝试创建一个拉到(刷新,加载更多)swiftUI ScrollView !!,灵感来自 https://cocoapods.org/pods/SwiftPullToRefresh 我正在努
我正在开发一个应用程序,其中有两个带有可放置区域的列表和一个带有可拖动项目的侧面菜单。 当我滚动屏幕时,项目的位置困惑。 我试图在谷歌上寻找一些东西,最后得到了这个问题:jQuery draggabl
我在 UIWebView 中加载了一个 HTML 表单,而我的 UIWebView 恰好从 View 的中间开始并扩展。我必须锁定此 webView 不滚动并将其放在 ScrollView 之上以允许
如何在每个元素而不是整个元素上应用淡入淡出(与其高度相比)? HTML: CSS: * { padding: 0; margin: 0; box-sizing: border
我想使用带有垂直轴的 PageView 并使用鼠标滚动在页面之间移动,但是当我使用鼠标滚动时页面不滚动...仅页面单击并向上/向下滑动时滚动。 有什么办法吗? 我想保留属性 pageSnapping:
我制作这个程序是为了好玩,但我被卡住了,因为程序在屏幕外运行。如何在不完全更改代码的情况下实现滚动条。 public static void main(String args[]) throws IO
我想使用带有垂直轴的 PageView 并使用鼠标滚动在页面之间移动,但是当我使用鼠标滚动时页面不滚动...仅页面单击并向上/向下滑动时滚动。 有什么办法吗? 我想保留属性 pageSnapping:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
使用 jquery 技术从 css-tricks.com 获得滚动/跟随侧边栏,如果您不知道我在说什么,这里是代码: $(function() { var $sidebar = $
我是 jQuery Mobile 新手。我需要向我的应用程序添加 Facebook 滑动面板功能。 我经历了 sliding menu panel ,它工作正常,但我在菜单面板中的内容超出了窗口大小,
有没有办法在 js 或 jQuery 或任何其他工具中检测 ctrl + 滚动。我正在尝试执行一些动态布局代码,我需要检测不同分辨率下的屏幕宽度,我通过使用 setTimeout() 的计时器实现了这
我有一部分html代码:
我想控制 RichTextBox 滚动,但在控件中找不到任何方法来执行此操作。 这样做的原因是我希望当鼠标光标位于 RichTextBox 控件上时鼠标滚轮滚动有效(它没有事件焦点:鼠标滚轮事件由表单
我是一名优秀的程序员,十分优秀!