進行旋轉(zhuǎn)
-(CABasicAnimation *)getTransformAnimation{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; //指定對transform.rotation屬性做動畫
animation.duration = 2.0f; //設(shè)定動畫持續(xù)時間
animation.byValue = @(M_PI*2); //設(shè)定旋轉(zhuǎn)角度盾碗,單位是弧度
animation.fillMode = kCAFillModeForwards;//設(shè)定動畫結(jié)束后再扭,不恢復初始狀態(tài)之設(shè)置一
animation.repeatCount = 1000;//設(shè)定動畫執(zhí)行次數(shù)
animation.removedOnCompletion = NO;//設(shè)定動畫結(jié)束后,不恢復初始狀態(tài)之設(shè)置二
return animation;
}
將 data 轉(zhuǎn) 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() {
// 關(guān)閉圖形上下文
UIGraphicsEndImageContext()
return newImg
}
// 關(guān)閉圖形上下文
UIGraphicsEndImageContext()
}
}
return nil
}
設(shè)置 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)
}