先看图片

这里是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上面的控件。

这句就可以完成偏移量。

macOS NSScrollView简单使用的更多相关文章

  1. MacOS SVN简单入门

    背景:MacOS内置了SVN的客户端和服务器端的软件,下边所使用到的目录需要结合自己电脑的具体情况进行设置,并不是很困难. MacOS SVN简单入门 第一部分,创建本地的SVN测试仓库,并修改相应的 ...

  2. macos下简单的socket服务器+客户端

    TCP客户端服务器编程模型: 服务器: 调用socket函数创建套接字 调用bind绑定本地IP和端口 调用listen启动监听(准备好接收客户端链接的队列) 调用accept从已连接队列中提取第一个 ...

  3. 免费版对象存储【minIO】CentOS部署实践记录 2021

    好久没写,记录一下 1.背景 之前一直用的七牛,不过是收费的,然后有些定制化需求,可能比较看重预算,然后就有了这篇开源方式:minio 2.简介 官方文档:http://docs.minio.org. ...

  4. MacOS下对postgresql的简单管理操作

    如何安装在另一篇blog中有述,这里不再赘述.本篇简单说一下安装完postgresql之后的一些管理和查询操作. 首先安装完postgresql之后需要初始化数据库: initdb /usr/loca ...

  5. [macOS开发.NET Core] 一个简单的WEB程序

    上一篇咱们提到了在macOS下选进行开发. 咱们已经把工具准备完成了. 现在咱们做一个简单的DEMO 创建WEB程序  之前咱们已经创建过WEB程序,并且成功的运行过数据 现在咱们创建一个页面,并显 ...

  6. MongoDB在MacOS上的客户端Robo 3T 的简单使用(二)

    最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. 本篇文章只做简单介绍,能够使用起来 ...

  7. MacOS系统下简单安装以及配置MongoDB数据库(一)

    最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. 项目一共四部分: 1.MacOS下 ...

  8. ASP.NET Core 新建项目 - macOS 环境 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 新建项目 - macOS 环境 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 新建项目 - macOS 环境 对于任何语言和 ...

  9. ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core macOS 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计.本章节我 ...

  10. 分享CCNTFS小工具,在 macOS 中完全读写、修改、访问Windows NTFS硬盘的文件,无须额外的驱动(原生驱动)更稳定,简单设置即可高速传输外接NTFS硬盘文件

    CCNTFS [ 下载 ] 在 macOS 中完全读写.修改.访问Windows NTFS硬盘的文件,无须额外的驱动(原生驱动)更稳定,安装后进行简单设置即可高速传输外接NTFS硬盘文件,可全程离线使 ...

随机推荐

  1. a^b(位运算&快速幂)

    题目链接 题目: 题解:很简单.经典的的一道快速幂的题 注意一下用LL型就ok. 代码: 1 #include <map> 2 #include <set> 3 #includ ...

  2. net core 添加cors,解决跨域问题

    ConfigureServices //允许跨域 services.AddCors(options => { options.AddPolicy("any", builder ...

  3. array copy() 的简单使用

    源码: public static native void arraycopy(Object src, int srcPos, Object dest, int destPos,int length) ...

  4. 回归分析-2.X 简单线性回归

    2.1 简单线性回归模型 y与x之间的关系假设 \(y=\beta_0+\beta_1x+\varepsilon\) \(E(\varepsilon|x)=0\) \(Var(\varepsilon| ...

  5. CLIP 读书笔记

    论文:Learning Transferable Visual Models From Natural Language Supervision CLIP: Contrastive Language- ...

  6. k8s之 pod调度

    案例:确保Pod分配到具有SSD硬盘的节点上 第一步:给节点添加标签 格式:kubectl label nodes <node-name> <label-key>=<la ...

  7. Parsing error: missing-whitespace-between-attributes

    给如下位置添加空格即可 在文件末尾添加一空白行即可

  8. vscode开发vue3+ts环境搭建

    一.开发环境准备 1.nvm 2.vscode 3.node 4.chome 5.git 6.npm install -g yarn tyarn 7.tyarn global add commitiz ...

  9. ABAP学习(35):常用Function

    ABAP Function ABAP Coding过程中比较好用的Function整理. 1.获取Domain信息 代码实例: "****************************** ...

  10. ASP.NET Core MVC中调用Json()时默认使用Newtonsoft.Json返回数据

    1.需要安装两个包 Newtonsoft.Json Microsoft.AspNetCore.Mvc.NewtonsoftJson 2. Startup 类中的 ConfigureServices 方 ...