UIBezierPath的一些应用

UIBezierPath的一些应用

引导页

在项目中有见到使用UIBezierPath来做一些用户引导说明,效果大致如下图所示,参考iOS: 首次使用App时,显示半透明新手指引

用户引导

使用了UIBezierPathbezierPathByReversingPath方法,参照bezierPathByReversingPath,试了下,区别如下:

- (void)drawRect:(CGRect)rect {


    UIBezierPath *thePath = [UIBezierPath bezierPath];
    UIBezierPath *revPath = [UIBezierPath bezierPath];


    [thePath appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50,50,150,100)]];
    [[UIColor redColor] set];


    [revPath appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(60,60,100,100)]];
    //revPath = [revPath bezierPathByReversingPath];

    [thePath appendPath:revPath];
    [[UIColor redColor] set];

    [thePath fill];

}

注释掉revPath = [revPath bezierPathByReversingPath];,效果如下:

这里写图片描述

取消注释revPath = [revPath bezierPathByReversingPath];,效果如下:

这里写图片描述

下面通过一个mask来说一下UIBezierPath在做用户引导上的应用,步骤如下:

a.在控制器的view上添加一个imageview,控制器的view的背景为洋红色

a

b.使用UIBezierPath创建一个圆形曲线,并做mask

    CGRect rect = CGRectMake((self.imageView.frame.size.width-200) / 2, (self.imageView.frame.size.height-200) / 2, 200, 200);

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];

    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.path = path.CGPath;

    self.imageView.layer.mask = shapeLayer;

做遮罩后的效果如下:

b

c.path之后再拼接一个矩形的曲线,如下:

[path appendPath:[UIBezierPath bezierPathWithRect:CGRectInset(rect, 50, 50)]] ;

效果如下,貌似没什么改变的:

c

d.使用bezierPathByReversingPath,使用如下的代码替换第c步的代码:

[path appendPath:[[UIBezierPath bezierPathWithRect:CGRectInset(rect, 50, 50)] bezierPathByReversingPath]];

显示效果如下:

d

所以,在window添加个透明的view,做一些遮罩,就可实现类似的用户引导效果

要注意的一点是,如果在控制器的viewDidLoad方法中调用:

    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window addSubview:bgView];

此时bgView被控制器的view所覆盖,如下所示(品红色为bgView,白色为控制器的view):

这里写图片描述

可参考KeyWindow add subView, did not shows in the front

You run into this issue, is caused by you do not know the difference between the viewDidLoad method and the viewDidAppear: method.

viewWillAppear:

Called before the view is added to the windows’ view hierarchy

viewDidAppear:

Called after the view is added to the view hierarchy

The controller’s view add to the window’s view hierarchy is betweenviewWillAppear: and viewDidAppear:, the viewDidLoad is in front of viewWillAppear:, so you can not do that. you should in viewDidAppear: add your view.

特殊形状的页面

可参考:放肆地使用UIBezierPath和CAShapeLayer画各种图形

如下:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {

        CGSize finalSize = CGSizeMake(CGRectGetWidth(self.frame), 200);
        CGFloat layerHeight = finalSize.height * 0.2;
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        UIBezierPath *bezier = [[UIBezierPath alloc] init];
        [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];
        [bezier addLineToPoint:CGPointMake(0, finalSize.height - 1)];
        [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - 1)];
        [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];

        [bezier addQuadCurveToPoint:CGPointMake(0,finalSize.height - layerHeight)
                       controlPoint:CGPointMake(finalSize.width / 2, (finalSize.height - layerHeight) - 40)];

        layer.path = bezier.CGPath;
        layer.fillColor = [UIColor blackColor].CGColor;
        [self.layer addSublayer:layer];

    }
    return self;
}

效果如下:

效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍如何使用UIBezierPath画聊天气泡。 首先,我们需要创建一个UIView作为聊天气泡的容器,并设置它的背景色和圆角。 然后,我们可以创建一个UIBezierPath对象,使用move(to:)方法将笔触移动到气泡的左下角,然后使用addLine(to:)方法画出气泡的左侧、顶部和右侧的线条。接着,我们可以使用addArc(withCenter:radius:startAngle:endAngle:clockwise:)方法在气泡的右下角添加一个圆角。 最后,我们可以使用CAShapeLayer将UIBezierPath对象渲染到UIView上,实现聊天气泡的绘制。 以下是示例代码: ```swift let bubbleView = UIView() bubbleView.backgroundColor = UIColor(red: 0.93, green: 0.93, blue: 0.93, alpha: 1) bubbleView.layer.cornerRadius = 10 bubbleView.clipsToBounds = true let bubblePath = UIBezierPath() bubblePath.move(to: CGPoint(x: 10, y: bubbleView.bounds.height - 10)) bubblePath.addLine(to: CGPoint(x: 10, y: 10)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 30, y: 10)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 20, y: 0)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 10, y: 10)) bubblePath.addLine(to: CGPoint(x: bubbleView.bounds.width - 10, y: bubbleView.bounds.height - 10)) bubblePath.addArc(withCenter: CGPoint(x: bubbleView.bounds.width - 20, y: bubbleView.bounds.height - 10), radius: 10, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: true) bubblePath.addArc(withCenter: CGPoint(x: bubbleView.bounds.width - 20, y: bubbleView.bounds.height - 20), radius: 10, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: true) bubblePath.addArc(withCenter: CGPoint(x: bubbleView.bounds.width - 30, y: bubbleView.bounds.height - 10), radius: 10, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 3 / 2, clockwise: true) bubblePath.close() let shapeLayer = CAShapeLayer() shapeLayer.path = bubblePath.cgPath bubbleView.layer.mask = shapeLayer ``` 以上代码将创建一个灰色的聊天气泡,带有圆角和三角形尾巴。您可以根据需要调整气泡的颜色、形状和大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值