常用方法
2018-01-31 本文已影响0人
kled_net
进行旋转
-(CABasicAnimation *)getTransformAnimation{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; //指定对transform.rotation属性做动画
animation.duration = 2.0f; //设定动画持续时间
animation.byValue = @(M_PI*2); //设定旋转角度,单位是弧度
animation.fillMode = kCAFillModeForwards;//设定动画结束后,不恢复初始状态之设置一
animation.repeatCount = 1000;//设定动画执行次数
animation.removedOnCompletion = NO;//设定动画结束后,不恢复初始状态之设置二
return animation;
}
将 data 转 String
var s = String(format: "%@", deviceToken as CVarArg)
s = s.replacingOccurrences(of: " ", with: "")
s = s.replacingOccurrences(of: "<", with: "")
s = s.replacingOccurrences(of: ">", with: "")
圆形图片的切割
func circleImage(_ orign_image:UIImage?) -> UIImage? {
if let image = orign_image {
UIGraphicsBeginImageContext(image.size)
// 获得上下文
if let ctx = UIGraphicsGetCurrentContext() {
// 矩形框
let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
// 添加一个圆
ctx.addEllipse(in: rect)
// 裁剪(裁剪成刚才添加的图形形状)
ctx.clip()
// 往圆上面画一张图片
image.draw(in: rect)
// 获得上下文中的图片
if let newImg = UIGraphicsGetImageFromCurrentImageContext() {
// 关闭图形上下文
UIGraphicsEndImageContext()
return newImg
}
// 关闭图形上下文
UIGraphicsEndImageContext()
}
}
return nil
}
设置 UIBarButtonItem
func barButtonItems(withImage image:String,hightLightImage:String,target:Any,action:Selector) -> UIBarButtonItem {
let btn = UIButton(type: .custom)
btn.setImage(UIImage(named:image), for: .normal)
btn.setImage(UIImage(named:hightLightImage), for: .highlighted)
btn.sizeToFit()
btn.addTarget(target, action: action, for: .touchUpInside)
let view = UIView(frame: btn.frame)
view.addSubview(btn)
return UIBarButtonItem(customView: view)
}
func barButtonItems(withTitle title:String,target:Any,action:Selector) -> UIBarButtonItem {
let btn = UIButton(type: .custom)
btn.setTitle(title, for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.sizeToFit()
btn.addTarget(target, action: action, for: .touchUpInside)
let view = UIView(frame: btn.frame)
view.addSubview(btn)
return UIBarButtonItem(customView: view)
}
找当前控制器
func findPresentingVC() -> (UIViewController){
var window = UIApplication.shared.keyWindow
if window?.windowLevel != UIWindowLevelNormal {
let windows = UIApplication.shared.windows
for win in windows {
if win.windowLevel == UIWindowLevelNormal {
window = win
break
}
}
}
var resulet = window?.rootViewController
while ((resulet?.presentedViewController) != nil) {
resulet = resulet?.presentedViewController
}
if (resulet?.isKind(of: UITabBarController.self)) ?? false {
resulet = (resulet as? UITabBarController)?.selectedViewController
}
if (resulet?.isKind(of: UINavigationController.self)) ?? false {
resulet = (resulet as? UINavigationController)?.topViewController
}
guard let vc = resulet else {
return UIViewController()
}
return vc
}
常用的正则
/// 手机号
func validateMobile( _ str : String ) -> Bool {
let Regex = "^((13[0-9])|(15[012356789])|(17[678])|(18[0-9])|(14[57]))\\d{8}$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}
/// 车牌
func validateCarNo( _ str : String ) -> Bool {
let Regex = "[\u{4e00}-\u{9fa5}]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u{4e00}-\u{9fa5}]$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}
/// 用户名
func validateUserName( _ str : String ) -> Bool {
let Regex = "^[A-Za-z0-9]{6,20}+$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}
/// 密码
func validatePassword( _ str : String ) -> Bool {
let Regex = "^[a-zA-Z0-9]{6,16}+$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}
/// 昵称
func validateNickname( _ str : String ) -> Bool {
let Regex = "^[\u{4e00}-\u{9fa5}]{4,8}$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}
/// 身份证号
func validateIdentityCard( _ str : String ) -> Bool {
var flag : Bool = true
if (str as NSString).length <= 0 {
flag = false
return flag
}
let Regex = "^(\\d{14}|\\d{17})(\\d|[xX])$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}
/// 昵称
func validataURL( _ str : String ) -> Bool {
let Regex = "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"
let Test = NSPredicate(format: "SELF MATCHES %@", Regex)
return Test.evaluate(with: str)
}