2013-02-09 128 views
10

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

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

+0

Xcode是一个应用程序(IDE)。它没有画任何东西。 Objective-C是iOS的编程语言。你可能想看看这个网站上的教程 - 特别是这个教程:http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients(尽管再看看你的问题似乎与绘图没什么关系,也许你想'UIScrollView') – 2013-02-09 05:32:43

+0

据我所知,我需要编写Objective-C代码,但我说我是用X代码作为参照系。我的理解是UIScrollView只是决定你是否可以滚动,你可以滚动多远。如果我从屏幕上画了一个矩形,但是UIScrollView被定义为可以滚动到最远,我最终可以查看它。基于这个前提,我只需要知道如何使用UIViewController及其.xib文件根据用户输入绘制矩形以确定坐标。 – 2013-02-09 05:38:27

+0

我需要的是一种方法,它将接受用户确定的输入,并根据用户定义的坐标,高度和宽度绘制矩形。 – 2013-02-09 05:40:39

回答

23

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

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

没有别的......不需要发疯的实施。

5

首先制作一个CustomView。

#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或视图控制器

- (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发生在一个长方形的形状绘制,所以如果你需要的是改变矩形的颜色,设置的backgroundColor财产UIView到你想要的颜色。

一个空的UIView会做:

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

如果从界面生成器做这件事,从库中拖出一个新的视图到画布上,并设置在查看其属性的背景色。

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

+0

这是一个我以前见过的建议,但问题是我需要能够在应用程序运行时在屏幕上的任意坐标处放置一个任意大小的矩形,因为用户将输入坐标值和高度和宽度。 – 2013-02-09 05:47:25

+0

您可以在运行时动态更新任何视图的框架。但是你需要在代码中这样做。当你更新框架时,它将改变矩形的位置和大小。它可以是“从字面上任意大小”和“在任何坐标上”:) – Kekoa 2013-02-09 05:50:05

+0

是否包含视图的坐标?这也需要能够具有未定义数量的矩形 – 2013-02-09 05:52:27

11
// 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]; – 2014-07-14 20:50:59

3

您可以使用此画一个矩形:

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