ええと、答えは簡単で、それらほど肥大化しているわけではありません。とにかく、それらはダブルタップ拡大では機能しません。
これはそうです。それだけで機能します。必要に応じて調整をカスタマイズすることもできます。
@implementationでは、constraintBoundsRectのオーバーライドを実装するだけで済みます。
- (NSRect)constrainBoundsRect:(NSRect)proposedClipViewBoundsRect {
NSRect constrainedClipViewBoundsRect = [super constrainBoundsRect:proposedClipViewBoundsRect];
// Early out if you want to use the default NSClipView behavior.
if (self.centersDocumentView == NO) {
return constrainedClipViewBoundsRect;
}
NSRect documentViewFrameRect = [self.documentView frame];
// If proposed clip view bounds width is greater than document view frame width, center it horizontally.
if (proposedClipViewBoundsRect.size.width >= documentViewFrameRect.size.width) {
// Adjust the proposed origin.x
constrainedClipViewBoundsRect.origin.x = centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(proposedClipViewBoundsRect.size.width, documentViewFrameRect.size.width);
}
// If proposed clip view bounds is hight is greater than document view frame height, center it vertically.
if (proposedClipViewBoundsRect.size.height >= documentViewFrameRect.size.height) {
// Adjust the proposed origin.y
constrainedClipViewBoundsRect.origin.y = centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension(proposedClipViewBoundsRect.size.height, documentViewFrameRect.size.height);
}
return constrainedClipViewBoundsRect;
}
CGFloat centeredCoordinateUnitWithProposedContentViewBoundsDimensionAndDocumentViewFrameDimension
(CGFloat proposedContentViewBoundsDimension,
CGFloat documentViewFrameDimension )
{
CGFloat result = floor( (proposedContentViewBoundsDimension - documentViewFrameDimension) / -2.0F );
return result;
}
@interface では、1 つのプロパティを追加するだけです。これにより、センタリングを使用しないことができます。ご想像のとおり、場合によってはセンタリングをオフにしたい条件付きロジックがあるかもしれません。
@property BOOL centersDocumentView;
また、必ずこれをに設定BOOL
するYES
かNO
、オーバーライドしてください
initWithFrame
とinitWithCoder:
そのため、既知のデフォルト値から作業できます。
(子供たちを覚えておい initWithCoder:
てください。必要なことを行い、ペン先にビューのクラスを設定できます。自分のものの前にスーパーを呼び出すことを忘れないでください!)
もちろん、10.9 より前のものをサポートする必要がある場合は、他のものを実装する必要があります。
(おそらく他の人が持っているほどではありませんが...)
EDIT:他の人が指摘したように、AppleはSwiftのサンプルコードを持っています(2016年からなので、現在のSwiftではないかもしれません:D)https://developer.apple.com/library/archive/samplecode/Exhibition/Listings/Exhibition_CenteringClipView_swift .html