class ViewController: UIViewController {
let chatV1: UIImageView = {
let iv = UIImageView()
iv.frame = CGRect(x: 0, y: 50, width: 200, height: 170)
iv.image = UIImage(named: "twg")
return iv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.chatV1.bubble()
view.addSubview(self.chatV1)
}
}
extension UIView {
enum BubbleDirection {
case left, right
}
func bubble(_ direction: BubbleDirection = .left) {
let iv = UIImageView()
iv.frame = CGRect(origin: .zero, size: bounds.size)
let imageDirName = direction == .left ? "bubble-left" : "bubble-right"
iv.image = UIImage(named: imageDirName)?
.resizableImage(withCapInsets: UIEdgeInsetsMake(3, 3, 3, 3),
resizingMode: .stretch)
self.layer.mask = iv.layer
}
}
之前这个红色气泡大小是512*512
的,被我改成了50*50
。
气泡原图特别大,而需要被气泡效果的图片比它小,则会出现严重的畸形。