Iphone 在iOS中绘制矩形

Iphone 在iOS中绘制矩形,iphone,objective-c,draw,rectangles,Iphone,Objective C,Draw,Rectangles,我的应用程序的目标是让用户能够通过滑动iPhone屏幕的左侧和右侧,对不同的日程安排选项进行排序。当用户通过这些不同的调度选项进行排序时,如何绘制和删除矩形 我有一个UIViewController.h、UIViewController.m和UIViewController.xib文件要处理。我需要单独的UIView类吗?如果是这样,如何将UIView类连接到.xib文件中的视图?首先创建一个自定义视图 #import <UIKit/UIKit.h> @interface MyVi

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


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

首先创建一个自定义视图

#import <UIKit/UIKit.h>

@interface MyView : UIView

@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;
}


UIView
恰巧是以矩形的形状绘制的,因此如果您只需要更改矩形的颜色,请将UIView的
backgroundColor
属性设置为您想要的颜色

空UIView将执行以下操作:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
view.backgroundColor = [UIColor redColor];
如果是从interface builder执行此操作,请将新视图从库拖到画布上,并在视图的属性中设置背景色

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

//首先将QuartzCore.framework添加到项目的“链接二进制文件与库”中。
// 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];
//第二次导入.m文件中的标题(目标视图控制器的): #进口 //第三种内部-(无效)视图加载方法: //创建UIBezierPath(用所需的坐标替换坐标): UIBezierPath*路径=[UIBezierPath bezierPath]; [path-moveToPoint:CGPointMake(10.0,10.0)]; //#具有10px白框的入口点 [path-moveToPoint:CGPointMake(10.0,10.0)]; //#将iPhone的450保持在y轴上的10px帧 [path addLineToPoint:CGPointMake(10.0450.0)]; //#x轴上的帧减去10px,y轴上移动450 [path addLineToPoint:CGPointMake(310.0450.0)]; //#向上移动到第1步10px线,在x轴上移动310px [path addLineToPoint:CGPointMake(310.0,10.0)]; //#返回入口点 [path addLineToPoint:CGPointMake(10.0,10.0)]; //第四,创建一个使用UIBezierPath的CAShapeLayer: CAShapeLayer*shapeLayer=[CAShapeLayer层]; shapeLayer.path=[path CGPath]; shapeLayer.strokeColor=[[UIColor blueColor]CGColor]; shapeLayer.lineWidth=3.0; shapeLayer.fillColor=[[UIColor clearColor]CGColor]; 将该层添加到视图的层: //第五,在图层视图中将shapeLayer添加为子图层 [self.view.layer addSublayer:shapeLayer];
哇。。。这么多复杂的解决方案,以下是您需要的:

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

没有别的了。。。不必为实现而疯狂。

您可以使用以下方法绘制矩形:

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

XCode是一个应用程序(IDE)。它没有画任何东西。Objective-C是iOS的编程语言。你可能想看看这个网站上的教程,尤其是这本:(尽管再看一眼你的问题似乎与绘画没有什么关系。也许你想
UIScrollView
)我知道我需要编写objective-c代码,但我说我在x-code中作为参考框架。我的理解是,UIScrollView只是决定了您是否可以滚动以及滚动的距离。如果我在屏幕上画了一个长方形,但是有UIScrollView,它被定义为能够滚动那么远,我最终可以看到它。基于这一前提,我只需要知道如何使用UIViewController及其.xib文件根据用户输入绘制矩形来确定坐标。我所需要的是一种方法,该方法将接受用户确定的输入,并根据坐标、高度、,和用户定义的宽度。这是我以前见过的建议,但问题是我需要能够在应用程序运行时在屏幕上的任何坐标处放置一个任意大小的矩形,因为用户将输入坐标值以及高度和宽度。您可以在运行时动态更新任何视图的框架。但您需要在代码中这样做。更新框架时,它将更改矩形的位置和大小。它可以是“任意大小”和“任意坐标”:(这包括视图的坐标吗?这还需要能够有一个未定义数量的矩形,一个
CGRect
指定原点(坐标)和大小。视图的框架是一个
CGRect
。如果我需要一个未定义数量的矩形,你可以随时执行[UIBezierPath bezierpath withrect:(CGRect)rect];
UIView *myBox  = [[UIView alloc] initWithFrame:CGRectMake(180, 35, 10, 10)];
myBox.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myBox];
 UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)];
 [UIColor.grayColor setFill];
 [rectanglePath fill];