我希望我的应用程序用户可以通过在iPhone屏幕左右滑动来筛选不同的日程安排选项。当用户浏览这些不同的日程安排选项时,我如何绘制和删除矩形?
我有一个UIViewController.h、UIViewController.m和UIViewController.xib文件可供操作。我需要单独的UIView类吗?如果是这样,我该如何将该UIView类连接到我的.xib文件中的视图?
我希望我的应用程序用户可以通过在iPhone屏幕左右滑动来筛选不同的日程安排选项。当用户浏览这些不同的日程安排选项时,我如何绘制和删除矩形?
我有一个UIViewController.h、UIViewController.m和UIViewController.xib文件可供操作。我需要单独的UIView类吗?如果是这样,我该如何将该UIView类连接到我的.xib文件中的视图?
哇... 这么多复杂的解决方案,这里是你需要的:
UIView *myBox = [[UIView alloc] initWithFrame:CGRectMake(180, 35, 10, 10)];
myBox.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myBox];
没有其他的要求...不需要在实现上过度纠结。
// 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];
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)];
[UIColor.grayColor setFill];
[rectanglePath fill];
首先创建一个自定义视图。
#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];
}
UIView实际上以矩形的形式绘制,因此如果您只需要更改矩形的颜色,请将UIView的backgroundColor
属性设置为所需的颜色。
一个空的UIView就能满足您的需求:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
view.backgroundColor = [UIColor redColor];