SHARE
TWEET

Untitled

a guest Nov 18th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  JLActiveSearchBar.swift
  3. //  JLActiveSearchBar
  4. //
  5. //  Created by 刘业臻 on 16/5/3.
  6. //  Copyright © 2016年 luiyezheng. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. @IBDesignable
  12. public class AnimatedSearchBar: UISearchBar, UIGestureRecognizerDelegate {
  13.    
  14.     private lazy var tapRecognizer: UITapGestureRecognizer! = {
  15.         let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(AnimatedSearchBar.drawoutline))
  16.         tapRecognizer.delegate = self
  17.         return tapRecognizer
  18.        
  19.     }()
  20.    
  21.     private var searchIconImageView: UIImageView!
  22.    
  23.     public var searchField: UITextField!
  24.    
  25.     private var pathLayer: CAShapeLayer!
  26.    
  27.     private var searchImageOne: UIImage? = UIImage(named: "search")
  28.     private var searchImageTwo: UIImage? = UIImage(named: "search1")
  29.    
  30.     @IBInspectable public var duration: CGFloat = 0.45
  31.     @IBInspectable public var outlineColor: UIColor? = .black
  32.    
  33.     init() {
  34.         super.init(frame: .zero)
  35.     }
  36.    
  37.     init(frame: CGRect, duration: CGFloat, outlineColor: UIColor) {
  38.         super.init(frame: frame)
  39.        
  40.         self.duration = duration
  41.         self.outlineColor = outlineColor
  42.     }
  43.    
  44.     override init(frame: CGRect) {
  45.         super.init(frame: frame)
  46.        
  47.         self.frame = frame
  48.        
  49.     }
  50.    
  51.     required public init?(coder aDecoder: NSCoder) {
  52.         super.init(coder: aDecoder)
  53.        
  54.     }
  55.    
  56.     override public func draw(_ rect: CGRect) {
  57.         if let index = indexOfSearchFieldInSubviews() {
  58.             searchBarStyle = .minimal
  59.             isTranslucent = true
  60.             backgroundColor = .clear
  61.             tintColor = .darkGray
  62.            
  63.             //MARK: -
  64.             //MARK: setup searchFied
  65.             searchField = (subviews[0] ).subviews[index] as! UITextField
  66.             searchField.frame = CGRect(x: 5.0, y: 5.0, width: frame.size.width, height: frame.size.height)
  67.             searchField.backgroundColor = .clear
  68.             searchField.placeholder = "Search..."
  69.             searchField.isHidden = true
  70.            
  71.         }
  72.        
  73.         //MARK: -
  74.         //MARK: setup searchIconImageView
  75.        
  76.         searchIconImageView = UIImageView(frame: CGRect(x: self.bounds.maxX - 40, y: self.bounds.minY , width: 18, height: 18))
  77.         searchIconImageView.center.y = bounds.maxY / 2
  78.         searchIconImageView.image = searchImageOne
  79.         searchIconImageView.addGestureRecognizer(tapRecognizer)
  80.         searchIconImageView.isUserInteractionEnabled = true
  81.         searchIconImageView.contentMode = .scaleToFill
  82.         addSubview(searchIconImageView)
  83.        
  84.         super.draw(rect)
  85.     }
  86.    
  87.     func indexOfSearchFieldInSubviews() -> Int! {
  88.         var index: Int!
  89.         let searchBarView = subviews[0]
  90.        
  91.         for i in 0 ..< searchBarView.subviews.count {
  92.             if searchBarView.subviews[i].isKind(of: UITextField.self) {
  93.                 index = i
  94.                 break
  95.             }
  96.         }
  97.         return index
  98.     }
  99.    
  100.     @objc func drawoutline() {
  101.        
  102.         //MARK: -
  103.         //MARK: draw outline
  104.         let path = UIBezierPath()
  105.         path.move(to: CGPoint(x: bounds.maxX, y: bounds.minY))
  106.         path.addLine(to: CGPoint(x: bounds.minX + 5, y: bounds.minY))
  107.         path.addLine(to: CGPoint(x: bounds.minX + 5, y: bounds.maxY))
  108.         path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
  109.         path.close()
  110.        
  111.         let shapeLayer = CAShapeLayer()
  112.         shapeLayer.path = UIBezierPath(roundedRect: path.bounds, cornerRadius: 20.0).cgPath
  113.         shapeLayer.strokeColor = outlineColor!.cgColor
  114.         shapeLayer.fillColor = nil
  115.         shapeLayer.lineWidth = 1.0
  116.         shapeLayer.lineJoin = CAShapeLayerLineJoin.bevel
  117.         self.pathLayer = shapeLayer
  118.         layer.addSublayer(self.pathLayer)
  119.        
  120.         let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
  121.         pathAnimation.duration = CFTimeInterval(duration)
  122.         pathAnimation.fromValue = 0.0
  123.         pathAnimation.toValue = 1.0
  124.         self.pathLayer.add(pathAnimation, forKey: "strokeEnd")
  125.        
  126.         //MARK: -
  127.         //MARK: setup searchBar state and animate
  128.        
  129.         searchField.isHidden = false
  130.         becomeFirstResponder()
  131.         self.searchIconImageView.isHidden = true
  132.         self.setImage(searchImageTwo, for: .search, state: .normal)
  133.        
  134.         let currentAdjustment = self.positionAdjustment(for: .search)
  135.         UIView.animate(withDuration: TimeInterval(duration), animations: {() -> Void in
  136.             self.setPositionAdjustment(UIOffset(horizontal: -self.bounds.width, vertical: currentAdjustment.vertical), for: .search)
  137.         }, completion: {(finished: Bool) -> Void in
  138.             self.setPositionAdjustment(currentAdjustment, for: .search)
  139.            
  140.         })
  141.        
  142.         UIView.animate(withDuration: TimeInterval(duration)) {
  143.             self.layoutIfNeeded()
  144.         }
  145.        
  146.     }
  147.    
  148.     //reset the searchBar state
  149.     public func reSet() {
  150.         self.pathLayer.removeFromSuperlayer()
  151.         self.setImage(nil, for: .search, state: .normal)
  152.         self.searchIconImageView.isHidden = false
  153.         searchField.isHidden = true
  154.         resignFirstResponder()
  155.     }
  156.     //MARK: -
  157.     //MARK: prepareForInterfaceBuilder
  158.    
  159.     override public func prepareForInterfaceBuilder() {
  160.         let bundle = Bundle(for: AnimatedSearchBar.self)
  161.         self.searchImageOne = UIImage(named: "search", in: bundle, compatibleWith: self.traitCollection)
  162.         self.searchImageTwo = UIImage(named: "search1", in: bundle, compatibleWith: self.traitCollection)
  163.        
  164.         let path = UIBezierPath()
  165.        
  166.         path.move(to: CGPoint(x: bounds.maxX, y: bounds.minY))
  167.         path.addLine(to: CGPoint(x: bounds.minX + 5, y: bounds.minY))
  168.         path.addLine(to: CGPoint(x: bounds.minX + 5, y: bounds.maxY))
  169.         path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
  170.         path.close()
  171.        
  172.         let shapeLayer = CAShapeLayer()
  173.         shapeLayer.path = UIBezierPath(roundedRect: path.bounds, cornerRadius: 20.0).cgPath
  174.         shapeLayer.strokeColor = outlineColor!.cgColor
  175.         shapeLayer.fillColor = nil
  176.         shapeLayer.lineWidth = 1.0
  177.         shapeLayer.lineJoin = CAShapeLayerLineJoin.bevel
  178.         self.pathLayer = shapeLayer
  179.         layer.addSublayer(self.pathLayer)
  180.        
  181.        
  182.     }
  183.    
  184. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top