iOS气泡效果(swift3)

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
  }
}
iOS气泡效果(swift3)_第1张图片
1.png
bubble-left.png

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

你可能感兴趣的:(iOS气泡效果(swift3))