ForgetSou | Blog

❤ 武统台湾 刻不容缓 ❤

0%

macOS开发-NSView

一.简介

NSView用于在应用程序中渲染、打印以及处理事件的基础容器,多数功能由AppKit自动调用。

NSView 继承自NSResponder

@interface NSView : NSResponder <NSAnimatablePropertyContainer, NSUserInterfaceItemIdentification, NSDraggingDestination, NSAppearanceCustomization, NSAccessibilityElement, NSAccessibility>

通常我们不直接使用NSView,而是创建子类或派生对象实现我们的需求。

@property (nullable, readonly, assign) NSWindow *window; // 返回NSWindow

二.核心API

1.创建对象

- (instancetype)initWithFrame:(NSRect)frameRect NS_DESIGNATED_INITIALIZER; // 初始化对象
- (void)prepareForReuse API_AVAILABLE(macos(10.7)); // 重置对象

2.视图结构

@property (nullable, readonly, assign) NSWindow *window;  // 获取视图NSWindow对象
@property (nullable, readonly, assign) NSView *superview; // 获取父视图
@property (copy) NSArray<__kindof NSView *> *subviews; // 获取所有自视图
/*! 判定视图是否在其父视图的视图层中 */
- (BOOL)isDescendantOf:(NSView *)view;
/*! 返回该视图和另一个指定视图共享的最接近的祖先 */
- (nullable NSView *)ancestorSharedWithView:(NSView *)view;
/*! 视图最接近的不透明祖先 */
@property (nullable, readonly, assign) NSView *opaqueAncestor;
/*! 添加子视图 */
- (void)addSubview:(NSView *)view;
- (void)addSubview:(NSView *)view positioned:(NSWindowOrderingMode)place relativeTo:(nullable NSView *)otherView;
/*! 添加子视图后的回调,可以在子类中重写该方法,进行其他操作 */
- (void)didAddSubview:(NSView *)subview;
/*! 从父视图中移除 */
- (void)removeFromSuperview;
/*! 子视图将会被移除 */
- (void)willRemoveSubview:(NSView *)subview;

/*! 替换子视图 */
- (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
/*! 使用比较器函数对子视图进行排序 */
- (void)sortSubviewsUsingFunction:(NSComparisonResult (NS_NOESCAPE *)(__kindof NSView *, __kindof NSView *, void * _Nullable))compare context:(nullable void *)context;

/*! 通知视图正在将其添加到指定窗口对象的视图层级结构 */
- (void)viewWillMoveToWindow:(nullable NSWindow *)newWindow;
/*! 通知视图已经将其添加到视图结构上 */
- (void)viewDidMoveToWindow;
/*! 通知视图正在将其添加到父视图上 */
- (void)viewWillMoveToSuperview:(nullable NSView *)newSuperview;
/*! 通知父视图已经做更改 */
- (void)viewDidMoveToSuperview;

3.frame & bounds

- (void)setFrameOrigin:(NSPoint)newOrigin;
- (void)setFrameSize:(NSSize)newSize;
@property NSRect frame;
@property CGFloat frameRotation;
@property CGFloat frameCenterRotation API_AVAILABLE(macos(10.5));

- (void)setBoundsOrigin:(NSPoint)newOrigin;
- (void)setBoundsSize:(NSSize)newSize;
@property CGFloat boundsRotation;
- (void)translateOriginToPoint:(NSPoint)translation;
- (void)scaleUnitSquareToSize:(NSSize)newUnitSize;
- (void)rotateByAngle:(CGFloat)angle;
@property NSRect bounds;

3.简单示例

NSView *view = [[NSView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[view prepareForReuse];
// 背景色
view.wantsLayer = YES;
view.layer.backgroundColor = NSColor.redColor.CGColor;
// 圆角
view.layer.cornerRadius = 100;
// 边框
view.layer.borderColor = NSColor.greenColor.CGColor;
view.layer.borderWidth = 3;
NSClickGestureRecognizer *gesture = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(viewClick:)];
[view addGestureRecognizer:gesture];
[self.view addSubview:view];

- (void)viewClick:(NSGestureRecognizer *)gesture {
NSLog(@"touch view");
}
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!

欢迎关注我的其它发布渠道