2014-12-13 68 views
6

我有,我想能够从任何视图控制器在iOS版+斯威夫特项目中使用下面的方法(例如):我应该把常见的实用功能在iOS的8 +斯威夫特

func initializeBlurEffectOnGivenUIView(UIViewToBeBlurred: UIView) { 

    let UIViewToBeBlurredHeight = UIViewToBeBlurred.frame.size.height 
    let UIViewToBeBlurredWidth = UIViewToBeBlurred.frame.size.width 
    let UIViewToBeBlurredX = UIViewToBeBlurred.frame.origin.x 
    let UIViewToBeBlurredY = UIViewToBeBlurred.frame.origin.y 

    let blurEffect:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 
    let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect) 

    let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect) 
    let blurEffectView:UIVisualEffectView = UIVisualEffectView(effect: blurEffect) 
    blurEffectView.frame = CGRectMake(UIViewToBeBlurredX, UIViewToBeBlurredY, UIViewToBeBlurredWidth, UIViewToBeBlurredHeight) 
    blurEffectView.contentView.addSubview(vibrancyEffectView) 

    UIViewToBeBlurred.addSubview(blurEffectView) 
} 

我有更多的常见帮手,例如那些我想提供的帮助。我如何构建应用程序来实现这一目标?

回答

7

使用的延伸,就像这样:

extension UIView { 
    func initializeBlurEffect() { 

     let height = self.frame.size.height 
     let width = self.frame.size.width 
     let x = self.frame.origin.x 
     let y = self.frame.origin.y 

     let blurEffect:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 
     let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect) 

     let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect) 
     let blurEffectView:UIVisualEffectView = UIVisualEffectView(effect: blurEffect) 
     blurEffectView.frame = CGRectMake(x, y, width, height) 
     blurEffectView.contentView.addSubview(vibrancyEffectView) 

     self.addSubview(blurEffectView) 
    } 
} 

然后调用它像这样:

myview.initializeBlurEffect() 
+0

这与我所说的没有什么不同。 – matt 2014-12-13 20:31:33

+0

我喜欢扩展的想法。而且,你会在什么位置放置这个扩展? – 2014-12-13 23:03:08

+0

没关系。我发现这个:https://www.youtube.com/watch?v = ENs97dVN_uk。谢谢! – 2014-12-13 23:17:10

18

如果该函数是一个自由的全局函数,则将该函数放在任何文件的顶层。 (只是不要不小心重复自己,并将相同的功能放在所有文件的顶层)。例如,我用我在这里描述的delay实用程序功能做什么:dispatch_after - GCD in swift?

在特定情况下你给的例子,但是,我可能会把它放在UIView的扩展。我会将其重写为一个实例方法,以免模糊另一个视图,我们将从现有视图开始并模糊self

+0

感谢您的有用答案。由于示例代码,我正在标记Mike的答案。 – 2014-12-13 23:48:30

+2

这应该是正确的答案。这个问题本身提到了常见的效用函数。 – ChrisH 2015-07-16 15:51:17

2

如果要访问此功能,在全球范围,那么你可以把它放在任何文件的顶部或系统地创建一个普通的swift文件并将其添加到那里。

OR 如果您要访问它UIViews或视图控制器,你可以(通过@Mike的建议)创建的UIView扩展

0

您可以为如下创建通用类:

class Utils { 

//MARK:- STRING FROM DICT 
func getStringFromDictionary(dict:Any) -> String{ 
    var strJson = "" 
    do { 
     let data = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted) 
     strJson = String(data: data, encoding: String.Encoding.utf8)! 
    } catch let error as NSError { 
     print("json error: \(error.localizedDescription)") 
    } 

    return strJson 
} 



//MARK:- ALERT 
func showAlertWithTitleFromVC(vc:UIViewController, title:String, andMessage message:String, buttons:[String], completion:((_ index:Int) -> Void)!) -> Void { 

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
    for index in 0..<buttons.count { 

     //  alertController.setValue(NSAttributedString(string: title, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 15),NSForegroundColorAttributeName : BLACK_COLOR]), forKey: "attributedTitle") 
     // 
     //  alertController.setValue(NSAttributedString(string: message, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 13),NSForegroundColorAttributeName : APP_COLOR_BLUE_1]), forKey: "attributedMessage") 


     let action = UIAlertAction(title: buttons[index], style: .default, handler: { 
      (alert: UIAlertAction!) in 
      if(completion != nil){ 
       completion(index) 
      } 
     }) 

     //  action.setValue(APP_COLOR_BLUE_2, forKey: "titleTextColor") 
     alertController.addAction(action) 
    } 
    vc.present(alertController, animated: true, completion: nil) 
} 


//MARK:- ACTION SHEET 
func showActionSheetWithTitleFromVC(vc:UIViewController, title:String, andMessage message:String, buttons:[String],canCancel:Bool, completion:((_ index:Int) -> Void)!) -> Void { 

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet) 

    alertController.setValue(NSAttributedString(string: title, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 15),NSForegroundColorAttributeName : BLACK_COLOR]), forKey: "attributedTitle") 
    alertController.setValue(NSAttributedString(string: message, attributes: [NSFontAttributeName : UIFont.appFont_OpenSans_Regular(fontSize: 13),NSForegroundColorAttributeName : APP_COLOR_BLUE_1]), forKey: "attributedMessage") 


    for index in 0..<buttons.count { 

     let action = UIAlertAction(title: buttons[index], style: .default, handler: { 
      (alert: UIAlertAction!) in 
      if(completion != nil){ 
       completion(index) 
      } 
     }) 

     action.setValue(APP_COLOR_BLUE_2, forKey: "titleTextColor") 
     alertController.addAction(action) 
    } 

    if(canCancel){ 
     let action = UIAlertAction(title: "Cancel", style: .cancel, handler: { 
      (alert: UIAlertAction!) in 
      if(completion != nil){ 
       completion(buttons.count) 
      } 
     }) 
     action.setValue(APP_COLOR_BLUE_2, forKey: "titleTextColor") 
     alertController.addAction(action) 
    } 

    vc.present(alertController, animated: true, completion: nil) 
} 

//MARK:- INTERNET CHECK 
func IS_INTERNET_AVAILABLE() -> Bool{ 

    return AIReachabilityManager.shared.isInternetAvailableForAllNetworks() 
} 


let INTERNET_MESSAGE:String = "Oops! It seems that you're not connected to the Internet, connect and try again"//"No internet connection, Please try later" 
func SHOW_INTERNET_ALERT(){ 
    showAlertWithTitleFromVC(vc: (appDelegate.window?.rootViewController)!, title: APP_NAME, andMessage: INTERNET_MESSAGE, buttons: ["Dismiss"]) { (index) in 
    } 
} 



//MARK:- CUSTOM LOADER 

func SHOW_CUSTOM_LOADER(){ 
    SHOW_CUSTOM_LOADER_WITH_TEXT(text: "") 
} 

func SHOW_CUSTOM_LOADER_WITH_TEXT(text:String){ 

    SVProgressHUD.setDefaultStyle(.custom) 
    SVProgressHUD.setDefaultMaskType(.custom) 
    SVProgressHUD.setDefaultAnimationType(.flat) 

    SVProgressHUD.setBackgroundColor(UIColor.clear) 
    SVProgressHUD.setRingRadius(30) 
    SVProgressHUD.setRingThickness(5) 
    SVProgressHUD.setForegroundColor(WHITE_COLOR) 

    if(text.characters.count > 0){ 
     SVProgressHUD.show(withStatus: text) 
    }else{ 
     SVProgressHUD.show() 
    } 
} 

func HIDE_CUSTOM_LOADER(){ 
    SVProgressHUD.dismiss() 
} 

//MARK:- USER DEFAULTS 

func setUserDefaultsFor(object:AnyObject, with key:String) { 
    UserDefaults.standard.set(object, forKey: key) 
    UserDefaults.standard.synchronize() 
} 

func getUserDefaultsForKey(key:String) -> AnyObject? { 
    return UserDefaults.standard.object(forKey: key) as AnyObject? 
} 

func removeUserDefaultsFor(key:String) { 
    UserDefaults.standard.removeObject(forKey: key) 
    UserDefaults.standard.synchronize() 
} 



//MARK:- PROPORTIONAL SIZE 
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height 
let SCREEN_WIDTH = UIScreen.main.bounds.size.width 

func GET_PROPORTIONAL_WIDTH (width:CGFloat) -> CGFloat { 
    return ((SCREEN_WIDTH * width)/750) 
} 
func GET_PROPORTIONAL_HEIGHT (height:CGFloat) -> CGFloat { 
    return ((SCREEN_HEIGHT * height)/1334) 
} 



//MARK:- NETWORK ACTIVITY INDICATOR 

func SHOW_NETWORK_ACTIVITY_INDICATOR(){ 
    UIApplication.shared.isNetworkActivityIndicatorVisible = true 
} 

func HIDE_NETWORK_ACTIVITY_INDICATOR(){ 
    UIApplication.shared.isNetworkActivityIndicatorVisible = false 
} 


//Check IsiPhone Device 
func IS_IPHONE_DEVICE()->Bool{ 
    let deviceType = UIDevice.current.userInterfaceIdiom == .phone 
    return deviceType 
} 

//Check IsiPad Device 
func IS_IPAD_DEVICE()->Bool{ 
    let deviceType = UIDevice.current.userInterfaceIdiom == .pad 
    return deviceType 
} 

//iPhone 4 OR 4S 
func IS_IPHONE_4_OR_4S()->Bool{ 
    let SCREEN_HEIGHT_TO_CHECK_AGAINST:CGFloat = 480 
    var device:Bool = false 

    if(SCREEN_HEIGHT_TO_CHECK_AGAINST == SCREEN_HEIGHT) { 
     device = true 
    } 
    return device 
} 

func proportionalFontSize() -> CGFloat { 

    var sizeToCheckAgainst = self 

    if(IS_IPAD_DEVICE()) { 
     sizeToCheckAgainst += 12 
    } 
    else { 
     if(IS_IPHONE_6P_OR_6SP()) { 
      sizeToCheckAgainst += 1 
     } 
     else if(IS_IPHONE_6_OR_6S()) { 
      sizeToCheckAgainst += 0 
     } 
     else if(IS_IPHONE_5_OR_5S()) { 
      sizeToCheckAgainst -= 1 
     } 
     else if(IS_IPHONE_4_OR_4S()) { 
      sizeToCheckAgainst -= 2 
     } 
    } 
    return sizeToCheckAgainst 
} 

//iPhone 5 OR OR 5C OR 4S 
func IS_IPHONE_5_OR_5S()->Bool{ 
    let SCREEN_HEIGHT_TO_CHECK_AGAINST:CGFloat = 568 
    var device:Bool = false 
    if(SCREEN_HEIGHT_TO_CHECK_AGAINST == SCREEN_HEIGHT) { 
     device = true 
    } 
    return device 
} 

//iPhone 6 OR 6S 
func IS_IPHONE_6_OR_6S()->Bool{ 
    let SCREEN_HEIGHT_TO_CHECK_AGAINST:CGFloat = 667 
    var device:Bool = false 

    if(SCREEN_HEIGHT_TO_CHECK_AGAINST == SCREEN_HEIGHT) { 
     device = true 
    } 
    return device 
} 

//iPhone 6Plus OR 6SPlus 
func IS_IPHONE_6P_OR_6SP()->Bool{ 
    let SCREEN_HEIGHT_TO_CHECK_AGAINST:CGFloat = 736 
    var device:Bool = false 

    if(SCREEN_HEIGHT_TO_CHECK_AGAINST == SCREEN_HEIGHT) { 
     device = true 
    } 
    return device 
} 

//MARK:- DEVICE ORIENTATION CHECK 
func IS_DEVICE_PORTRAIT() -> Bool { 
    return UIDevice.current.orientation.isPortrait 
} 

func IS_DEVICE_LANDSCAPE() -> Bool { 
    return UIDevice.current.orientation.isLandscape 
} 


//MARK:- SYSTEM VERSION CHECK 
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool { 
    return UIDevice.current.systemVersion.compare(version, 
                options: NSString.CompareOptions.numeric) == ComparisonResult.orderedSame 
} 

func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool { 
    return UIDevice.current.systemVersion.compare(version, 
                options: NSString.CompareOptions.numeric) == ComparisonResult.orderedDescending 
} 

func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool { 
    return UIDevice.current.systemVersion.compare(version, 
                options: NSString.CompareOptions.numeric) != ComparisonResult.orderedAscending 
} 

func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool { 
    return UIDevice.current.systemVersion.compare(version, 
                options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending 
} 

func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool { 
    return UIDevice.current.systemVersion.compare(version, 
                options: NSString.CompareOptions.numeric) != ComparisonResult.orderedDescending 
} 

}