macOS NSScrollView简单使用
2024-10-21 09:09:35
先看图片
这里是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上面的控件。
这句就可以完成偏移量。
最新文章
- PHP关于web页面交互内容
- C算法编程题(一)扑克牌发牌
- 十DHT11
- chrome下input[type=text]的placeholder不垂直居中的问题解决
- Json(2)-DataContractJsonSerializer
- linux mount / umount 命令的基本用法
- virsh 基于xml create VMs虚机
- 使用Open Live Writer 的代码高亮插件体验
- python filter内建函数
- 13.java.lang.NoSuchFiledException
- IS2009制作Oracle 静默安装包(二) 感谢空白先生特许授权
- win10 uwp 判断文件存在
- 复制粘贴之不带插件的jquery
- Entry的验证
- Python args kwargs 技巧
- 让DOM从页面中消失的方法
- chrome用type=file获取图片路径并转base64字符串
- HDU - 2604 Queuing(递推式+矩阵快速幂)
- 第一篇:初识ASP.NET控件开发_第一节:控件类及其继承关系
- mysql reset password重置密码