【问题标题】:Drawing Rectangles in iOS在 iOS 中绘制矩形
【发布时间】:2013-02-09 05:08:04
【问题描述】:

我的应用程序的目标是让用户能够通过在 iPhone 屏幕的左右滑动来对不同的调度选项进行排序。当用户对这些不同的调度选项进行排序时,我将如何绘制和删除矩形?

我有一个 UIViewController.h、UIViewController.m 和 UIViewController.xib 文件要操作。我需要一个单独的 UIView 类吗?如果是这样,我如何将该 UIView 类连接到我的 .xib 文件中的视图?

【问题讨论】:

  • XCode 是一个应用程序(IDE)。它不画任何东西。 Objective-C 是 iOS 的编程语言。您可能想查看本网站上的教程 - 特别是这个:raywenderlich.com/2033/…(尽管再看一遍您的问题似乎与绘图无关。也许您想要UIScrollView
  • 我知道我需要编写objective-c 代码,但我说我在x-code 中作为参考框架。我的理解是 UIScrollView 只是确定您是否可以滚动以及您可以滚动多远。如果我在屏幕上画了一个矩形,但 UIScrollView 被定义为能够滚动那么远,我最终可以查看它。基于这个前提,我只需要知道如何使用 UIViewController 及其 .xib 文件根据用户输入绘制矩形以确定坐标。
  • 我只需要一个方法,该方法将接受用户确定的输入并根据用户定义的坐标、高度和宽度绘制一个矩形。

标签: iphone objective-c draw rectangles


【解决方案1】:

哇...这么多复杂的解决方案,这就是你需要的:

UIView *myBox  = [[UIView alloc] initWithFrame:CGRectMake(180, 35, 10, 10)];
myBox.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myBox];

没有别的......不需要为实现而疯狂。

【讨论】:

    【解决方案2】:
    // 1st Add QuartzCore.framework to your project's "Link Binary with Libraries".
    
    // 2nd Import the header in your .m file (of targeted view controller):
    
    #import <QuartzCore/QuartzCore.h>
    
    // 3rd Inside - (void)viewDidLoad method:
    // Create a UIBezierPath (replace the coordinates with whatever you want):
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(10.0, 10.0)];
    // # Entry Point with 10px white frame
    [path moveToPoint:CGPointMake(10.0, 10.0)];
    // # Keeping 10px frame with iPhone's 450 on y-axis
    [path addLineToPoint:CGPointMake(10.0, 450.0)];
    // # Substracting 10px for frame on x-axis, and moving 450 in y-axis
    [path addLineToPoint:CGPointMake(310.0, 450.0)];
    // # Moving up to 1st step 10px line, 310px on the x-axis
    [path addLineToPoint:CGPointMake(310.0, 10.0)];
    // # Back to entry point
    [path addLineToPoint:CGPointMake(10.0, 10.0)];
    
    // 4th Create a CAShapeLayer that uses that UIBezierPath:
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
    shapeLayer.lineWidth = 3.0;
    shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    Add that CAShapeLayer to your view's layer:
    
    // 5th add shapeLayer as sublayer inside layer view
    [self.view.layer addSublayer:shapeLayer];
    

    【讨论】:

    • 你总是可以做 [UIBezierPath bezierPathWithRect:(CGRect)rect];
    【解决方案3】:

    你可以用这个来画一个矩形:

     UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)];
     [UIColor.grayColor setFill];
     [rectanglePath fill];
    

    【讨论】:

      【解决方案4】:

      首先制作一个自定义视图。

      #import <UIKit/UIKit.h>
      
      @interface MyView : UIView
      
      @end
      

      #import "MyView.h"
      
      @implementation MyView
      
      - (id)initWithFrame:(CGRect)frame
      {
          self = [super initWithFrame:frame];
          if (self) {
              // Initialization code
          }
          return self;
      }
      
      
      // Only override drawRect: if you perform custom drawing.
      // An empty implementation adversely affects performance during animation.
      - (void)drawRect:(CGRect)rect
      {
          // Drawing Rect
          [[UIColor redColor] setFill];               // red
          UIRectFill(CGRectInset(self.bounds, 100, 100));
      }
      
      
      @end
      

      你测试。链接到您的 AppDelegate 或 ViewController

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
      {    
          MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)];
          view.backgroundColor = [UIColor blackColor];
          [window addSubview:view];
      
          [self.window makeKeyAndVisible];
      
          return YES;
      }
      

      - (void)viewDidLoad
      {
          MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)];
          view.backgroundColor = [UIColor blackColor];
          [self.view addSubview:view];
      
          [super viewDidLoad];
      }
      

      【讨论】:

        【解决方案5】:

        UIView 恰好绘制成矩形的形状,所以如果您只需要更改矩形的颜色,请将 UIView 的 backgroundColor 属性设置为您想要的颜色。

        一个空的 UIView 就可以了:

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        view.backgroundColor = [UIColor redColor];
        

        如果您是从界面生成器中执行此操作,请将新视图从库中拖到画布上,并在视图的属性中设置背景颜色。

        对于简单案例,您可能不需要自定义视图。您还可以在“矩形”视图中添加其他项目,例如用于显示文本的 UILabel。

        【讨论】:

        • 这是我以前见过的建议,但问题是我需要能够在应用程序运行时在屏幕上的任何坐标上放置一个任意大小的矩形,因为用户将输入坐标值以及高度和宽度。
        • 您可以在运行时动态更新任何视图的框架。但是您需要在代码中执行此操作。当您更新框架时,它将更改矩形的位置和大小。它可以是“字面上任何大小”和“任何坐标”:)
        • 是否包含视图的坐标?这还需要能够具有未定义数量的矩形
        • 是的,CGRect 指定原点(坐标)和大小。视图的框架是CGRect
        • 如果我需要未定义数量的矩形怎么办
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多