在iOS中绘制矩形

10

我希望我的应用程序用户可以通过在iPhone屏幕左右滑动来筛选不同的日程安排选项。当用户浏览这些不同的日程安排选项时,我如何绘制和删除矩形?

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


XCode是一个应用程序(集成开发环境)。它不会绘制任何东西。Objective-C是iOS的编程语言。您可能想查看此网站上的教程 - 特别是这个:http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients(尽管再次查看您的问题似乎与绘图无关。也许您想要`UIScrollView`)。 - spring
我知道我需要编写Objective-C代码,但我的意思是,我以Xcode作为参考框架。我的理解是UIScrollView只是确定您是否可以滚动以及可以滚动多远。如果我在屏幕外绘制一个矩形,但定义了可以滚动那么远的UIScrollView,我最终可以查看它。基于这个前提,我只需要知道如何使用UIViewController和它的.xib文件根据用户输入来确定坐标来绘制矩形。 - Bryce Langlotz
我所需要的是一个方法,它将接受用户确定的输入,并根据用户定义的坐标、高度和宽度绘制一个矩形。 - Bryce Langlotz
5个回答

24

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

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

没有其他的要求...不需要在实现上过度纠结。


12
// 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];

4
你可以使用[UIBezierPath bezierPathWithRect:(CGRect)rect];来完成。 - rolling_codes

6
您可以使用以下代码绘制一个矩形:
 UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)];
 [UIColor.grayColor setFill];
 [rectanglePath fill];

5

首先创建一个自定义视图。

#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];
}

3

UIView实际上以矩形的形式绘制,因此如果您只需要更改矩形的颜色,请将UIView的backgroundColor属性设置为所需的颜色。

一个空的UIView就能满足您的需求:

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

如果您是通过界面构建器进行操作,可以从库中拖动一个新视图到画布上,并在该视图的属性中设置背景颜色。
在简单情况下,您可能不需要自定义视图。您还可以向“矩形”视图添加其他项目,例如用于显示文本的UILabel。

这是我之前看到过的建议,但问题在于我需要能够在应用程序运行时在屏幕上的任何坐标放置一个大小为任意的矩形,因为用户将输入坐标值、高度和宽度。 - Bryce Langlotz
您可以在运行时动态更新任何视图的框架。但是,您需要在代码中执行此操作。当您更新框架时,它将更改矩形的位置和大小。它可以是“任何尺寸”和“任何坐标” :) - Kekoa
这是否包括视图的坐标?此外,它还需要能够具有未定义数量的矩形。 - Bryce Langlotz
如果我需要一个未定义数量的矩形,怎么办? - Bryce Langlotz
您需要逐个在代码中添加它们。您可以添加任意数量的项目,但是如果添加太多(数百或数千个),可能会出现一些性能问题。 - Kekoa
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接