先看图片

这里是NSScrollView配合其他控件使用,我们这里只讲NSScrollView。

复制以下代码即可以运行

@interface ViewControl ()

/** 滚动显示 */
@property (nonatomic, strong) NSScrollView *scrollView; @end @implementation ViewControl
#pragma mark - 懒加载
- (NSScrollView*)scrollView{
if(!_scrollView){
_scrollView = [[NSScrollView alloc]init];
[self.view addSubview:self.scrollView];
[_scrollView setBorderType:NSNoBorder]; _scrollView.hasHorizontalScroller = true;//水平滚动
_scrollView.hasVerticalScroller = false;//禁止垂直滚动 _scrollView.horizontalScrollElasticity = NSScrollElasticityAllowed; //水平的弹性属性
_scrollView.verticalScrollElasticity = NSScrollElasticityNone; //垂直的弹性属性 [_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];//自适应出现或隐藏滑动条 }
return _scrollView;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
[self setLocationScrollView];//设定滑动位置,这里可以用NSButton的点击事件或其他控件的触发事件来设定 [self layoutSubViews];//设置UI
//监听滑动位置
[[self.scrollView contentView] setPostsBoundsChangedNotifications: YES];//发起通知
//找个合适的地儿,注册通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter] ;
[center addObserver: self
selector: @selector(boundsDidChangeNotification:)
name: NSViewBoundsDidChangeNotification
object: [self.scrollView contentView]]; } #pragma mark -UI布局
- (void)layoutSubViews{
self.scrollView.frame = CGRectMake(100,100,200,44);//位置,或者用下面的
//7.左右滚动条
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(30);
make.left.offset(5);
make.width.equalTo(ws.mas_width).offset(-10);
make.height.equalTo(ws.mas_height).offset(-35);
}]; } #pragma mark - 监听滚动条事件
- (void) boundsDidChangeNotification: (NSNotification *) notification{
// 在这里进行处理
NSClipView *changedContentView = [notification object]; // get the origin of the NSClipView of the scroll view that
// we're watching NSPoint changedBoundsOrigin = [changedContentView documentVisibleRect].origin;
//判断滚动条移动高度变化确认是否移动拉动滚动到底
NSLog(@"滑动了:%f",changedBoundsOrigin.y);
if (changedBoundsOrigin.y > 560) {
// self.argeeButton.enabled = YES;
}else{
// self.argeeButton.enabled = NO;
} } #pragma mark 设定滑动条位置
-(void)setLocationScrollView{ float ScrollLocation = 0.0;//往右偏移量
float MaxScroll = 0.0;//往右最大偏移量 [[_collectionView enclosingScrollView] setLineScroll:0.0f];//NSView的enclosingScrollView属性可以获得视图的滚动条,如果视图没有滚动条则enclosingScrollView为nil。 [[_collectionView enclosingScrollView] setPageScroll:0.0f];//NSView的enclosingScrollView属性可以获得视图的滚动条,如果视图没有滚动条则enclosingScrollView为nil。 ScrollLocation = [[[_collectionView enclosingScrollView] contentView] bounds].origin.y; MaxScroll = [[[_collectionView enclosingScrollView] documentView] bounds].size.height - [[_collectionView enclosingScrollView] documentVisibleRect].size.height; ScrollLocation += 100; if(ScrollLocation < 0) { ScrollLocation = 0; } else if(ScrollLocation > MaxScroll) { ScrollLocation = MaxScroll; } [_collectionView scrollPoint:NSMakePoint(ScrollLocation, 0)];//此处才是最为关键直接触发偏移量,_collectionView是处于self.scrollView上面的控件。
 }

按钮自己添加。触发

[_collectionView scrollPoint:NSMakePoint(ScrollLocation, 0)];//此处才是最为关键直接触发偏移量,_collectionView是处于self.scrollView上面的控件。

这句就可以完成偏移量。

最新文章

  1. PHP关于web页面交互内容
  2. C算法编程题(一)扑克牌发牌
  3. 十DHT11
  4. chrome下input[type=text]的placeholder不垂直居中的问题解决
  5. Json(2)-DataContractJsonSerializer
  6. linux mount / umount 命令的基本用法
  7. virsh 基于xml create VMs虚机
  8. 使用Open Live Writer 的代码高亮插件体验
  9. python filter内建函数
  10. 13.java.lang.NoSuchFiledException
  11. IS2009制作Oracle 静默安装包(二) 感谢空白先生特许授权
  12. win10 uwp 判断文件存在
  13. 复制粘贴之不带插件的jquery
  14. Entry的验证
  15. Python args kwargs 技巧
  16. 让DOM从页面中消失的方法
  17. chrome用type=file获取图片路径并转base64字符串
  18. HDU - 2604 Queuing(递推式+矩阵快速幂)
  19. 第一篇:初识ASP.NET控件开发_第一节:控件类及其继承关系
  20. mysql reset password重置密码

热门文章

  1. placeholder 颜色修改
  2. python批量修改一个文件夹下含多个文件夹中的所有图片名称
  3. python flask后端request获取参数的几种方式整理
  4. android 投屏
  5. pdf.js 跨域完美解决!
  6. javaSE学习四
  7. C#整合ActiveMQ与SpringBoot整合ActiveMQ数据间交互
  8. P1002 [NOIP2002 普及组] 过河卒
  9. 通过java代码向mysql数据库插入记录,中文乱码处理
  10. linux 清空catalina.out日志 不需要重启tomcat(五种方法)