8

NSView で繰り返し背景画像を描画しようとしていますが、今までこれを持っています:

// INIT
- (id)initWithFrame:(NSRect)frame {
  if (self = [super initWithFrame:frame]) {
    self.backgroundImage = [NSImage imageNamed:@"progressBackground.pdf"];
  }

  return self;
}

// DRAW
- (void)drawRect:(NSRect)dirtyRect {
  // Draw the background
  [backgroundImage drawInRect:[self bounds]
                     fromRect:NSMakeRect(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height)
                    operation:NSCompositeSourceAtop
                     fraction:1.0f];
  NSLog(@"%dx%d", backgroundImage.size.width, backgroundImage.size.height);
}

ただし、ビューは画像を引き伸ばしてそれ自体を埋めます。代わりに画像を繰り返したい。

代替テキスト (黒いストロークはすでに修正されています)

また、コンソールには画像のサイズが等しいと表示されるため、奇妙なことが起こります-2109897792x0が、画像は実際には32x32です! なんてこと?!

誰か助けてくれませんか?ありがとう。

4

3 に答える 3

22

+[NSColor colorWithPatternImage:]でパターンの色を作成し、背景の四角形をその「色」で塗りつぶすことができます。それはあなたが達成したいことをするはずです。

于 2010-10-10T13:09:19.153 に答える
17

ここで答えを見つけて、パターンが下から描画され、ウィンドウのサイズが変更されたときに奇妙な効果が得られるのを防ぎます。

http://www.mere-mortal-software.com/blog/details.php?d=2007-01-08

基本的に、drawRect で行う必要があるのは、グラフィック コンテキストの状態を保存し、メソッドを呼び出し、パターンをペイントして、状態を復元することだけです。

メソッドは次のとおりです。

- (void)drawRect:(NSRect)dirtyRect {
    NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
    [theContext saveGraphicsState];
    [[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(0,[self frame].size.height)];
    [self.customBackgroundColour set];
    NSRectFill([self bounds]);
    [theContext restoreGraphicsState]; 
}

init メソッドで背景色パターンを初期化しました。

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.customBackgroundColour = [NSColor colorWithPatternImage:[NSImage imageNamed:@"light-linen-texture.png"]];
    }

    return self;
}
于 2011-09-14T02:01:08.270 に答える
0

RMSkinnedViewGithubでチェックしてください!

EDIT:RMSkinnedViewは、Interface Builderのユーザー定義ランタイム属性NSViewを介して直接、角丸、背景色、背景画像パターンなどを含む複数のオプションを設定できるサブクラスです。

于 2013-12-29T16:43:54.813 に答える