在iOS中绘制矩形

Bry*_*otz 10 iphone objective-c rectangles draw

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

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

小智 23

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

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

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


Jer*_*ith 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];
Run Code Online (Sandbox Code Playgroud)

  • 你总是可以做[UIBezierPath bezierPathWithRect:(CGRect)rect]; (4认同)

bit*_*com 5

首先制作CustomView.

#import <UIKit/UIKit.h>

@interface MyView : UIView

@end
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)

你测试一下 链接到您的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;
}
Run Code Online (Sandbox Code Playgroud)
- (void)viewDidLoad
{
    MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];

    [super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)


Tej*_*uri 5

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

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