123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- //
- // UIView+Extension.swift
- //
- //
- // Created by LXF on 16/3/29.
- // Copyright © 2016年 LXF. All rights reserved.
- //
- import Foundation
- import UIKit
- extension UIView {
- //MARK: - storyboard上添加属性 可以直接配置 省得每次都要代码实现
- // border 宽度
- @IBInspectable var borderWidth: CGFloat {
- get {
- return self.layer.borderWidth
- }
- set {
- self.layer.borderWidth = newValue
- }
- }
- // border的颜色
- @IBInspectable var borderColor: UIColor {
- get {
- return UIColor(cgColor: self.layer.borderColor!)
- }
- set {
- self.layer.borderColor = newValue.cgColor
- }
- }
- // 圆角
- @IBInspectable var cornerRadius: CGFloat {
- get {
- return self.layer.cornerRadius
- }
- set {
- self.layer.cornerRadius = newValue
- self.layer.masksToBounds = newValue > 0
- }
- }
-
-
-
- func setAnchorConstraintsFullSizeTo(view: UIView, padding: CGFloat = 0) {
- self.translatesAutoresizingMaskIntoConstraints = false
- self.topAnchor.constraint(equalTo: view.topAnchor, constant: padding).isActive = true
- self.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -padding).isActive = true
- self.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding).isActive = true
- self.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding).isActive = true
- }
-
- func createLineView(_ frame:CGRect,_ color:UIColor) -> UIView {
- self.frame = frame
- self.backgroundColor = color
- return self
- }
-
- /// 裁剪 view 的圆角
- func clipRectCorner(direction: UIRectCorner, cornerRadius: CGFloat) {
- let cornerSize = CGSize(width: cornerRadius, height: cornerRadius)
- let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: direction, cornerRadii: cornerSize)
- let maskLayer = CAShapeLayer()
- maskLayer.frame = bounds
- maskLayer.path = maskPath.cgPath
- layer.addSublayer(maskLayer)
- layer.mask = maskLayer
- }
-
- public var x: CGFloat{
- get{
- return self.frame.origin.x
- }
- set{
- var r = self.frame
- r.origin.x = newValue
- self.frame = r
- }
- }
- public var y: CGFloat{
- get{
- return self.frame.origin.y
- }
- set{
- var r = self.frame
- r.origin.y = newValue
- self.frame = r
- }
- }
- /// 右边界的x值
- public var rightX: CGFloat{
- get{
- return self.x + self.width
- }
- set{
- var r = self.frame
- r.origin.x = newValue - frame.size.width
- self.frame = r
- }
- }
- /// 下边界的y值
- public var bottomY: CGFloat{
- get{
- return self.y + self.height
- }
- set{
- var r = self.frame
- r.origin.y = newValue - frame.size.height
- self.frame = r
- }
- }
- public var centerX : CGFloat{
- get{
- return self.center.x
- }
- set{
- self.center = CGPoint(x: newValue, y: self.center.y)
- }
- }
- public var centerY : CGFloat{
- get{
- return self.center.y
- }
- set{
- self.center = CGPoint(x: self.center.x, y: newValue)
- }
- }
-
-
-
- // MARK: - UIView 圆角
- /// 切圆角
- ///
- /// - Parameter cornerRadius: 圆角半径
- func roundedCorners(cornerRadius: CGFloat) {
- roundedCorners(cornerRadius: cornerRadius, borderWidth: 0, borderColor: nil)
- }
-
- /// 圆角边框设置
- ///
- /// - Parameters:
- /// - cornerRadius: 圆角半径
- /// - borderWidth: 边款宽度
- /// - borderColor: 边款颜色
- func roundedCorners(cornerRadius: CGFloat?, borderWidth: CGFloat?, borderColor: UIColor?) {
- self.layer.cornerRadius = cornerRadius!
- self.layer.borderWidth = borderWidth!
- self.layer.borderColor = borderColor?.cgColor
- self.layer.masksToBounds = true
- }
-
- /// 设置指定角的圆角
- ///
- /// - Parameters:
- /// - cornerRadius: 圆角半径
- /// - rectCorner: 指定切圆角的角
- func roundedCorners(cornerRadius: CGFloat?, rectCorner: UIRectCorner?) {
- let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: rectCorner!, cornerRadii: CGSize(width: cornerRadius!, height: cornerRadius!))
- let layer = CAShapeLayer()
- layer.frame = self.bounds
- layer.path = path.cgPath
- self.layer.mask = layer
- }
-
- /// EZSwiftExtensions
- public func setCornerRadius(radius: CGFloat) {
- self.layer.cornerRadius = radius
- self.layer.masksToBounds = true
- }
-
- //画线
- private func drawBorder(rect:CGRect,color:UIColor){
- let line = UIBezierPath(rect: rect)
- let lineShape = CAShapeLayer()
- lineShape.path = line.cgPath
- lineShape.fillColor = color.cgColor
- self.layer.addSublayer(lineShape)
- }
-
- //设置右边框
- public func rightBorder(width:CGFloat,borderColor:UIColor){
- let rect = CGRect(x: 0, y: self.frame.size.width - width, width: width, height: self.frame.size.height)
- drawBorder(rect: rect, color: borderColor)
- }
- //设置左边框
- public func leftBorder(width:CGFloat,borderColor:UIColor){
- let rect = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
- drawBorder(rect: rect, color: borderColor)
- }
- //设置上边框
- public func topBorder(width:CGFloat,borderColor:UIColor){
- let rect = CGRect(x: 0, y: 0, width: self.frame.size.width, height: width)
- drawBorder(rect: rect, color: borderColor)
- }
- //设置底边框
- public func buttomBorder(width:CGFloat,borderColor:UIColor){
- let rect = CGRect(x: 0, y: self.frame.size.height-width, width: self.frame.size.width, height: width)
- drawBorder(rect: rect, color: borderColor)
- }
- }
- // MARK: Gesture Extensions
- extension UIView {
-
- /// EZSwiftExtensions
- public func addShadow(offset: CGSize, radius: CGFloat, color: UIColor, opacity: Float, cornerRadius: CGFloat? = nil) {
- self.layer.shadowOffset = offset
- self.layer.shadowRadius = radius
- self.layer.shadowOpacity = opacity
- self.layer.shadowColor = color.cgColor
- if let r = cornerRadius {
- self.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: r).cgPath
- }
- }
-
- /// http://stackoverflow.com/questions/4660371/how-to-add-a-touch-event-to-a-uiview/32182866#32182866
- /// EZSwiftExtensions
- public func addTapGesture(tapNumber: Int = 1, target: AnyObject, action: Selector) {
- let tap = UITapGestureRecognizer(target: target, action: action)
- tap.numberOfTapsRequired = tapNumber
- addGestureRecognizer(tap)
- isUserInteractionEnabled = true
- }
-
- /// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
- public func addTapGesture(tapNumber: Int = 1, action: ((UITapGestureRecognizer) -> Void)?) {
- let tap = BlockTap(tapCount: tapNumber, fingerCount: 1, action: action)
- addGestureRecognizer(tap)
- isUserInteractionEnabled = true
- }
-
- /// EZSwiftExtensions
- public func addSwipeGesture(direction: UISwipeGestureRecognizer.Direction, numberOfTouches: Int = 1, target: AnyObject, action: Selector) {
- let swipe = UISwipeGestureRecognizer(target: target, action: action)
- swipe.direction = direction
-
- #if os(iOS)
-
- swipe.numberOfTouchesRequired = numberOfTouches
-
- #endif
-
- addGestureRecognizer(swipe)
- isUserInteractionEnabled = true
- }
-
- /// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
- public func addSwipeGesture(direction: UISwipeGestureRecognizer.Direction, numberOfTouches: Int = 1, action: ((UISwipeGestureRecognizer) -> Void)?) {
- let swipe = BlockSwipe(direction: direction, fingerCount: numberOfTouches, action: action)
- addGestureRecognizer(swipe)
- isUserInteractionEnabled = true
- }
-
- /// EZSwiftExtensions
- public func addPanGesture(target: AnyObject, action: Selector) {
- let pan = UIPanGestureRecognizer(target: target, action: action)
- addGestureRecognizer(pan)
- isUserInteractionEnabled = true
- }
-
- /// EZSwiftExtensions - Make sure you use "[weak self] (gesture) in" if you are using the keyword self inside the closure or there might be a memory leak
- public func addPanGesture(action: ((UIPanGestureRecognizer) -> Void)?) {
- let pan = BlockPan(action: action)
- addGestureRecognizer(pan)
- isUserInteractionEnabled = true
- }
-
-
- public func removeSubviews() {
- for subview in subviews {
- subview.removeFromSuperview()
- }
- }
- }
|