在iOS中绘制矩形

时间:2013-02-09 05:08:05

标签: iphone objective-c draw rectangles

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

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

5 个答案:

答案 0 :(得分:23)

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

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

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

答案 1 :(得分: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];

答案 2 :(得分: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或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 :(得分:4)

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

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

答案 4 :(得分:3)

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

空UIView将执行:

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

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

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