仅更改部分UIView的颜色

1 colors objective-c uiview ios

我有一些UIView,我想只对它的一部分应用一些效果,所以例如让我坐在我有这个条UIView:

UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
Run Code Online (Sandbox Code Playgroud)

比我想要只在其中的一部分改变颜色 CGRectMake(250, 0, 50, 50)];

有没有办法做到这一点 ?

*我不想在另一种颜色上面放置另一个矩形因为这种效果是动态的并且动态变化.

谢谢 .

Avt*_*Avt 5

你应该实现自己的UIView方法

- (void)drawRect:(CGRect)rect
Run Code Online (Sandbox Code Playgroud)

文档:

此方法的默认实现不执行任何操作.使用Core Graphics和UIKit等技术绘制视图内容的子类应重写此方法并在其中实现其绘图代码

你应该收到类似的东西

- (void)drawRect:(CGRect)rect
{
    CGRect frame = self.bounds;
    // Set the background color
    [[UIColor redColor] set];
    UIRectFill(frame);
    // Set the second color
    [[UIColor greenColor] set];
    UIRectFill(CGRectMake(250, 0, 50, 50));
}
Run Code Online (Sandbox Code Playgroud)