在任意View 上添加一個(gè)可移動的虛線框

調(diào)用示例

在StoryBoard 上需要添加虛線框的視圖上拖一個(gè)UIView 這樣方便加 constrain
繼承 DashedBoardView
在需要添加框框的view 的視圖控制器里面

class aViewController: UIViewController {

  @IBOutlet weak var dashedBorderView: DashedBorderView!
  @IBOutlet weak var dashedViewX: NSLayoutConstraint! // 虛線框的x軸約束
  @IBOutlet weak var dashedViewY: NSLayoutConstraint! // 虛線框的y軸約束

  var touchPointCache: CGPoint?
  
  override func viewDidLoad() {
      super.viewDidLoad()
      // 添加拖動手勢
      let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(_:)))
      dashedBorderView.addGestureRecognizer(panGesture)
    }
}

在其它控件的響應(yīng)方法中改變 DashedBoardView 的屬性
比如scrollView 的縮放結(jié)束響應(yīng)方法中

  func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
  // 博主這里做的是當(dāng)UIScrollView 縮放到0.25 的比例的時(shí)候框框顏色發(fā)生改變
     dashedBorderView.textOnLabel = "當(dāng)前縮放比例 \(scrollView.zoomScale) "
     if scrollView.zoomScale == 0.25 {
         dashedBorderView.border.strokeColor = UIColor.whiteColor().CGColor
     } else {
         dashedBorderView.border.strokeColor = UIColor.grayColor().CGColor
     }
  } 

源碼

DashedBoardView

//
//  DashedBoardView.swift
//  
//
//  Created by 王俊碩 on 16/3/22.
//  Copyright ? 2016年 王俊碩. All rights reserved.
//

import UIKit

class DashedBorderView: UIView {

  var border:CAShapeLayer!
  var warnLabel: UILabel?
  var textOnLabel: String {
      get {
          return "please set a value"
      }
      set(currentPixel) {
          warnLabel?.attributedText = NSAttributedString(string: "\(currentPixel)*\(currentPixel)", attributes: [NSFontAttributeName: UIFont.systemFontOfSize((9))])
      }
  }

  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
    
      warnLabel = UILabel(frame: CGRect(x: 10, y: 4, width: 100, height: 12))
      warnLabel?.textColor = UIColor.whiteColor()
      self.addSubview(warnLabel!)
    
      border = CAShapeLayer();
    
      border.strokeColor = UIColor.grayColor().CGColor;
      border.fillColor = nil;
      border.lineDashPattern = [4, 4];
      self.layer.addSublayer(border);
    
  }

  func addDashedFrame(inView view: UIView)  {
      border = CAShapeLayer();
    
      border.strokeColor = UIColor.whiteColor().CGColor;
      border.fillColor = nil;
      border.lineDashPattern = [4, 4];
      view.layer.addSublayer(border);
      border.path = UIBezierPath(roundedRect: self.bounds, cornerRadius:8).CGPath;
      border.frame = self.bounds;
    
  }

  func addLabel(currentPixel currentPixel: CGFloat) {

      let label = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
      label.text = "Current Pixel: \(currentPixel) * \(currentPixel)"
}

拖動響應(yīng)

  func handlePanGesture(gesture: UIPanGestureRecognizer) {
    switch (gesture.state) {
    case UIGestureRecognizerState.Began:
        print("Moving bega")
        touchPointCache = gesture.locationInView(self.view)
    case UIGestureRecognizerState.Changed:
        print("Touch point changed")
        let touchPoint = gesture.locationInView(self.view)
        let deltaX = (touchPointCache?.x)! - touchPoint.x
        let deltaY = (touchPointCache?.y)! - touchPoint.y
        dashedViewX.constant -= deltaX // 刷新坐標(biāo)
        dashedViewY.constant -= deltaY // 刷新坐標(biāo)
        touchPointCache = touchPoint
        print(touchPoint)
    case UIGestureRecognizerState.Ended:
        print("User has ended the touch")
        print("x :  \(dashedViewX) y: \(dashedViewY)")
        touchPointCache = nil
    default:
        print("What's happend")
    }
    print("dashedBoard frmae: \(dashedBorderView)")
    
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容