美文网首页
[Swift] 气泡 | 镂空

[Swift] 气泡 | 镂空

作者: 巨馍蘸酱 | 来源:发表于2022-07-21 11:16 被阅读0次
image.png
// 气泡图片拉伸
let bgImgView1 = UIImageView(frame: CGRect(x: 20, y: 200, width: 100, height: 100))
bgImgView1.backgroundColor = UIColor.systemBlue
let iconImgView = UIImageView(frame: bgImgView1.bounds)
iconImgView.image = UIImage(named: "home_star")?.resizableImage(withCapInsets: UIEdgeInsets(top: 12, left: 14, bottom: 10, right: 6))
bgImgView1.layer.mask = iconImgView.layer
self.view.addSubview(bgImgView1)

// 气泡图片拉伸
let bgImgView2 = UIImageView(frame: CGRect(x: 150, y: 200, width: 100, height: 100))
bgImgView2.backgroundColor = UIColor.systemBlue
let iconShape = CAShapeLayer()
iconShape.frame = bgImgView2.bounds
iconShape.contents = UIImage(named: "home_star")?.cgImage
iconShape.contentsCenter = CGRect(x: 0.7, y: 0.7, width: 0.1, height: 0.1)
iconShape.contentsScale = UIScreen.main.scale
bgImgView2.layer.mask = iconShape
self.view.addSubview(bgImgView2)

// 镂空
let bgView = UIView(frame: CGRect(x: 20, y: 330, width: 100, height: 100))
bgView.backgroundColor = UIColor.systemBlue
let bezier = UIBezierPath(arcCenter: CGPoint(x: 50, y: 50), radius: 20, startAngle: 0, endAngle: CGFloat.pi*2, clockwise: true)
let mask = UIBezierPath(arcCenter: CGPoint(x: 30, y: 30), radius: 20, startAngle: 0, endAngle: CGFloat.pi*2, clockwise: true)
bezier.append(mask)
let shape = CAShapeLayer()
shape.fillRule = .evenOdd // 奇偶层显示规则, (奇数显示, 偶数镂空)
shape.path = bezier.cgPath
bgView.layer.mask = shape
self.view.addSubview(bgView)

// 画气泡
let arrowTop: CGFloat = 10
let arrowWidth: CGFloat = 10
let arrowHeight: CGFloat = 10
let radius: CGFloat = 20
let width: CGFloat = 200 - arrowWidth
let height: CGFloat = 100

let bubble = UIView(frame: CGRect(x: 150, y: 330, width: 200, height: 100))
bubble.backgroundColor = UIColor.systemBlue

let bubbleBezier = UIBezierPath()
bubbleBezier.move(to: CGPoint(x: 0, y: radius + arrowTop + arrowHeight / 2)) // 箭头
bubbleBezier.addLine(to: CGPoint(x: arrowWidth, y: radius + arrowTop)) // 箭头右上
//bubbleBezier.addLine(to: CGPoint(x: arrowWidth, y: radius)) // 左上圆角
bubbleBezier.addArc(withCenter: CGPoint(x: arrowWidth+radius, y: radius), radius: radius, startAngle: CGFloat.pi, endAngle: CGFloat.pi*1.5, clockwise: true) // 左上圆角
bubbleBezier.addLine(to: CGPoint(x: arrowWidth+radius, y: 0)) // 左上
bubbleBezier.addLine(to: CGPoint(x: width-radius, y: 0)) // 右上
//bubbleBezier.addLine(to: CGPoint(x: width, y: radius)) // 右上圆角
bubbleBezier.addArc(withCenter: CGPoint(x: width-radius, y: radius), radius: radius, startAngle: CGFloat.pi*1.5, endAngle: CGFloat.pi*2, clockwise: true) // 右上圆角
bubbleBezier.addLine(to: CGPoint(x: width, y: height-radius)) // 右下
//bubbleBezier.addLine(to: CGPoint(x: width-radius, y: height)) // 右下圆角
bubbleBezier.addArc(withCenter: CGPoint(x: width-radius, y: height-radius), radius: radius, startAngle: 0, endAngle: CGFloat.pi*0.5, clockwise: true) // 右下圆角
bubbleBezier.addLine(to: CGPoint(x: arrowWidth+radius, y: height)) // 左下
//bubbleBezier.addLine(to: CGPoint(x: arrowWidth, y: height-radius)) // 左下圆角
bubbleBezier.addArc(withCenter: CGPoint(x: arrowWidth+radius, y: height-radius), radius: radius, startAngle: CGFloat.pi*0.5, endAngle: CGFloat.pi, clockwise: true) // 左下圆角
bubbleBezier.addLine(to: CGPoint(x: arrowWidth, y: radius + arrowTop + arrowHeight)) // 箭头右下
bubbleBezier.close()
let bubbleShape = CAShapeLayer()
bubbleShape.path = bubbleBezier.cgPath
bubbleShape.lineWidth = 2
bubbleShape.strokeColor = UIColor.systemGreen.cgColor
bubbleShape.fillColor = UIColor.clear.cgColor
bubble.layer.addSublayer(bubbleShape)
self.view.addSubview(bubble)

相关文章

  • 2018-03-09

    swift 高德地图 自定义弹出气泡 1、CalloutView import UIKit class Custo...

  • Swift 镂空遮罩层的实现

    本篇所实现的是拍照时候中间一个框,边上都是半透明黑色的效果。 关键 如果叠加的层是偶数个,则不显示叠加部分。如果是...

  • Swift 自定义带气泡 可渐变色 seekbar

    代码仅供参考 全部在文末 语法为 swift 5 无图无真相! 需求分析 温度在 [37 , 57] 区间 ,气泡...

  • iOS气泡效果(swift3)

    之前这个红色气泡大小是512*512的,被我改成了50*50。气泡原图特别大,而需要被气泡效果的图片比它小,则会出...

  • Swift点击区域弹出选择气泡

    ///这部分才是重点,主要是贝塞尔曲线画气泡 具体使用: 协议回调 }

  • Swift 聊天气泡图片拉伸

    1. 聊天气泡图片: 设置UIImageView的contentMode为scaleToFill 拉伸图片2种方式...

  • 茶余饭后,来欣赏下古玉的纹饰制作

    (一) 镂空 1、新石器时代晚期 新石器时代晚期玉器的镂空,大体上分为砣具镂空、线具镂空等。红山文化玉器镂空属于“...

  • 一行代码实现 UIView 镂空效果

    这是一种实现 UIView 镂空效果的方案,可以快速实现任意形状的镂空、文字的镂空、带镂空的毛玻璃效果等。本质上是...

  • 镂空

    只有一个目的简单到纯粹简单到令人发指简单到让人毛骨悚然 除了安静利齿沙沙作响 脉络是清晰的镂空,用精雕细琢的方式将...

  • 幻灯技能解析04|镂空遮罩+动画,用过的学员都说好!

    镂空的物件在生活中十分常见,比如:镂空的玄关、镂空的衣服、镂空的饰品,这种工艺本是用于雕刻上,虽穿透物体却不破坏其...

网友评论

      本文标题:[Swift] 气泡 | 镂空

      本文链接:https://www.haomeiwen.com/subject/wdcpirtx.html