这个类背后的想法是摆脱每次想要做简单、快捷或基于块状动态的事情时都需要创建一个 UIView 子类的需求。有了这个小工具,您可以跳过这个步骤,并且可以像您喜欢的那样灵活或静态,最重要的是,可以非常自定义 — 而不会离开您正在工作的类。
// You may want to switch the drawrect block of the view at runtime...
- (void)updateBlockChoiceFromSegmentedController
{
BMDrawRectBlock firstBlock = ^(CGRect rect)
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor colorWithRed:0.401 green:1.000 blue:0.695 alpha:1.000].CGColor);
CGContextStrokeEllipseInRect(ctx, rect);
};
BMDrawRectBlock secondBlock = ^(CGRect rect)
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0.401 green:0.5 blue:0.695 alpha:1.000].CGColor);
CGContextFillEllipseInRect(ctx, rect);
};
_storyboardCreatedView.drawRectBlock = (_segmentedControl.selectedSegmentIndex ? secondBlock : firstBlock);
}
// Or just create it NOW to muck about with an idea:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[BMDrawRectView viewWithFrame:self.view.bounds drawRectBlock:^(CGRect rect)
{
// Your CGContext/drawRect magic here!
}];
}
要运行示例项目,请克隆仓库,然后首先在 Example 目录中运行 pod install
。如果您对它进行一些操作,示例项目看起来会很漂亮 — Reveal 会显示它这样
提高自己的意愿。除此之外,没有别的。
Adam Iredale,@iosengineer
BMDrawRectView 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。