macOS NSScrollView简单使用
先看图片
这里是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上面的控件。
这句就可以完成偏移量。