UIViewExtension:圆角以及添加手势

1. 圆角
// MARK: - 圆角
extension UIView {
    
    /**
     Rounds the given set of corners to the specified radius
     
     - parameter corners: Corners to round
     - parameter radius:  Radius to round to
     */
    func round(corners: UIRectCorner, radius: CGFloat) {
        _ = _round(corners: corners, radius: radius)
    }
    
    /**
     Rounds the given set of corners to the specified radius with a border
     
     - parameter corners:     Corners to round
     - parameter radius:      Radius to round to
     - parameter borderColor: The border color
     - parameter borderWidth: The border width
     */
    func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        let mask = _round(corners: corners, radius: radius)
        addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
    }
    
    /**
     Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border
     
     - parameter diameter:    The view's diameter
     - parameter borderColor: The border color
     - parameter borderWidth: The border width
     */
    func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        layer.masksToBounds = true
        layer.cornerRadius = diameter / 2
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor;
    }
    
}

private extension UIView {
    
    @discardableResult func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
        return mask
    }
    
    func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
        let borderLayer = CAShapeLayer()
        borderLayer.path = mask.path
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.lineWidth = borderWidth
        borderLayer.frame = bounds
        layer.addSublayer(borderLayer)
    }
    
}
2. 圆角使用
// 仅右上侧和右下侧圆角
view.round(corners: [.topRight, .bottomRight], radius: 6)

// 所有圆角
view.round(corners: .allCorners, radius: 6)


3. 手势扩展(Rx版)
// MARK: - 手势扩展
extension UIView {
    func addTapAction(target: Any, action: Selector) {
        self.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: target, action: action)
        self.addGestureRecognizer(tap)
    }
    
    func addTapAction() -> Driver<UITapGestureRecognizer> {
        self.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer()
        self.addGestureRecognizer(tap)
        return tap.rx.event.asDriver()
    }
    
    func addSwipeAction(direction: UISwipeGestureRecognizerDirection = .up) -> Driver<UISwipeGestureRecognizer> {
        self.isUserInteractionEnabled = true
        let recognizer = UISwipeGestureRecognizer()
        recognizer.direction = direction
        addGestureRecognizer(recognizer)
        return recognizer.rx.event.asDriver()
    }
}
4. 手势扩展使用(Rx版)
view.addTapAction()
            .drive(onNext: { _ in
                print("view tap")
            })
            .disposed(by: self.disposeBag)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,029评论 3 119
  • 进入人生的再一次转折,有意识知道,又要一个新的开始,当在飞机上看着某茶油的眼睛觉察到干涸两个字时,要丰盛的流...
    听月福州阅读 285评论 0 0
  • 上次去海边,用iPhoneX拍了几张照片。被人夸了几句,人马上就膨胀了,觉得自己可以买点设备了。 在朋友推荐下,入...
    Falmouth阅读 3,756评论 2 3
  • 也不知道什么时候开始,我这个理科生竟然偏离了自己的轨道,悉心研究创作。 就目前个人水平来说,创作倒是谈不上,随笔总...
    墨若雨涵阅读 360评论 2 4
  • 九个月12天x 布丁和妈妈在外婆家,发烧好了一些。 希望已经完全好了。
    hs双儿阅读 103评论 0 0