关于小控件以及小细节UI效果实现整理

2016-08-05  本文已影响0人  旧雨伞时

开发中总是避免不了自己封装一些小工具, 虽然一般不是自己完全自定义的, 但里面的部分效果还是需要自己琢磨修改的.
比如常用的datePicker, 中间两行的分割线颜色是可以修改的. 目前最合理的修改方案如下:

- (void)changeSeparatorWithView:(UIView *)view
{
    if(view.subviews != 0)
    {
        if(view.bounds.size.height < 5)
        {
            view.backgroundColor = [UIColor colorWithRed:74 / 255.0 green:181 / 255.0 blue:243 / 255.0 alpha:1];
        }
        [view.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
            [self changeSeparatorWithView:obj];
        }];
    }
}

关于使用"贝塞尔曲线"画图,
在代码中我们直接在.m中实现"-(void)drawRect:(CGRect)rect"这个方法即可, 无需在声明及其它接口方法中调用.
但是要明确一点, 就是视图frame一定要提前设置好, 不然图形画不出来, 是不走这个方法的.
下面就记录一段绘制三角形的代码:

-(void)drawRect:(CGRect)rect
{
    // Drawing code  通过贝塞尔曲线绘制三角形
    
    //定义画图的path
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    //path移动到开始画图的位置
    [path moveToPoint:CGPointMake(rect.origin.x, rect.origin.y)];
    //从开始位置画一条直线到(rect.origin.x + rect.size.width, rect.origin.y)
    [path addLineToPoint:CGPointMake(rect.origin.x + rect.size.width, rect.origin.y)];
    //再从rect.origin.x + rect.size.width, rect.origin.y))画一条线到(rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height)
    [path addLineToPoint:CGPointMake(rect.origin.x + rect.size.width/2, rect.origin.y + rect.size.height)];
    
    //关闭path
    [path closePath];
    
    //三角形内填充颜色
    [[UIColor colorWithRed:74 / 255.0 green:181 / 255.0 blue:243 / 255.0 alpha:1] setFill];
    
    [path fill];
    // //三角形的边框为红色
    // [[UIColor clearColor] setStroke];
    // [path stroke];
}
上一篇下一篇

猜你喜欢

热点阅读