tableView刷新周期
CATransaction.begin()
CATransaction.setCompletionBlock {
self.tableView.reloadData()
}
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [(NSIndexPath(row: idx, section: 0) as IndexPath)], with: .left)
self.tableView.endUpdates()
CATransaction.commit()
UITextField 限制字数
@objc func textFieldDidChange(_ textfield: UITextField) {
guard let _ = textfield.markedTextRange else {
let cursorPostion = textfield.offset(from: textfield.endOfDocument, to: textfield.selectedTextRange!.end)
if textfield.text!.count > textfield.tag {
textfield.text = String(textfield.text!.prefix(textfield.tag))
}
let targetPosion = textfield.position(from: textfield.endOfDocument, offset: cursorPostion)!
textfield.selectedTextRange = textfield.textRange(from: targetPosion, to: targetPosion)
return
}
}
图片添加图片水印
extension UIImage{
enum WaterMarkCorner{
case TopLeft
case TopRight
case BottomLeft
case BottomRight
}
func waterMarkedImage(waterMarkImage: UIImage,
corner: WaterMarkCorner = .BottomRight,
margin: CGPoint = CGPoint(x: 30, y: 47),
alpha: CGFloat = 1,
size: CGSize) -> UIImage {
var markFrame = CGRect(x:0, y: 0, width: size.width, height: size.height)
let imageSize = self.size
switch corner{
case .TopLeft:
markFrame.origin = margin
case .TopRight:
markFrame.origin = CGPoint(x: imageSize.width - waterMarkImage.size.width - margin.x,
y: margin.y)
case .BottomLeft:
markFrame.origin = CGPoint(x: margin.x,
y: imageSize.height - waterMarkImage.size.height - margin.y)
case .BottomRight:
markFrame.origin = CGPoint(x: imageSize.width - waterMarkImage.size.width - margin.x,
y: imageSize.height - waterMarkImage.size.height - margin.y)
}
UIGraphicsBeginImageContext(imageSize)
self.draw(in: CGRect(x: 0, y:0, width: imageSize.width, height: imageSize.height))
waterMarkImage.draw(in: markFrame, blendMode: .normal, alpha: alpha)
let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return waterMarkedImage!
}
}
绘制虚线
extension UIView {
/// 绘制虚线
func drawDashLine(_ strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5) {
let shapeLayer:CAShapeLayer = CAShapeLayer()
shapeLayer.bounds = self.bounds
shapeLayer.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = strokeColor.cgColor
shapeLayer.lineWidth = lineWidth
shapeLayer.lineJoin = CAShapeLayerLineJoin.round
shapeLayer.lineDashPhase = 0
shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
let path:CGMutablePath = CGMutablePath()
path.move(to: CGPoint(x: 0, y: 10))
path.addLine(to: CGPoint(x: self.frame.width, y: 10))
shapeLayer.path = path
self.layer.addSublayer(shapeLayer)
}
}
Double 小数点后保存几位
extension Double {
func truncate(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return Double(Int(self * divisor)) / divisor
}
}
颜色 转 image
/// 颜色 转 image
/// - Parameters:
/// - color: 颜色
/// - viewSize: size
func imageFromColor(color: UIColor, viewSize: CGSize) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
UIGraphicsBeginImageContext(rect.size)
let context: CGContext = UIGraphicsGetCurrentContext()!
context.setFillColor(color.cgColor)
context.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsGetCurrentContext()
return image!
}
禁止右划返回怎么都不管用试试这个
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let target = self.navigationController?.interactivePopGestureRecognizer?.delegate
let pan = UIPanGestureRecognizer(target: target, action: nil)
self.view.addGestureRecognizer(pan)
}
网友评论