【问题标题】:Objective-c: view background patternObjective-c:查看背景图案
【发布时间】:2011-06-06 01:48:09
【问题描述】:

视图中是否可以有一个可以重复 x 或 y 或两者的模式(比如网格)?有点像css:

背景图片: url(pattern.png); 背景重复:重复; //或repeat-x,repeat-y,不重复;

【问题讨论】:

    标签: objective-c view background


    【解决方案1】:

    以下代码将平铺背景图像:

    - (void) viewDidLoad {
        [[self view] setBackgroundColor: [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"background.png"]]];
        [super viewDidLoad];
    }
    

    【讨论】:

      【解决方案2】:

      是的。您可以将图像模式加载为颜色 ([NSColor withPatternImage:[NSImage imageNamed:@"pattern"]]),然后像绘制常规颜色一样绘制它:

      - (void)drawRect:(NSRect)dirtyRect {
          ...
          // Load the image through NSImage and set it as the current color.
          [[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern"]] set];
      
          // Fill the entire view with the image.
          [NSBezierPath fillRect:[self bounds]];
          ...
      }
      

      此代码将在整个视图中重复该模式,但您可以简单地对其进行 x-repeat 或 y-repeat 并稍作修改。


      查看NSColor Class ReferenceNSView Class Reference 了解更多信息。

      【讨论】:

        猜你喜欢
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 2020-05-03
        • 2021-01-09
        • 2012-09-15
        • 2019-05-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多