10

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

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

4

5 回答 5

24

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

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

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

于 2014-08-01T00:09:00.183 回答
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];
于 2013-11-22T22:35:37.087 回答
6

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

 UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)];
 [UIColor.grayColor setFill];
 [rectanglePath fill];
于 2015-02-28T02:50:26.777 回答
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];
}
于 2013-02-09T05:36:01.303 回答
3

AUIView恰好以矩形的形状绘制,因此如果您只需要更改矩形的颜色,请将backgroundColorUIView 的属性设置为您想要的颜色。

一个空的 UIView 会做:

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

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

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

于 2013-02-09T05:42:25.590 回答