I have the following method (for instance) that I'd like to be able to use from any ViewController in an iOS + Swift project:
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)
}
I have more common helpers such as there that I'd like to make available as well. How do I structure the application to achieve this?
To use a function, you “call” that function with its name and pass it input values (known as arguments) that match the types of the function's parameters. A function's arguments must always be provided in the same order as the function's parameter list.
A utility function that describes a preference for one bundle of goods (Xa) vs another bundle of goods (Xb) is expressed as U(Xa, Xb). Where there are perfect complements, the utility function is written as U(Xa, Xb) = MIN[Xa, Xb], where the smaller of the two is assigned the function's value.
To do this, write a dash then a right angle bracket after your function's parameter list, then tell Swift what kind of data will be returned. Inside your function, you use the return keyword to send a value back if you have one.
If the function is to be a free global, put the function at the top level of any file. (Just don't accidentally repeat yourself and put the same function at the top level of all files.) That, for example, is what I do with the delay
utility function I describe here: dispatch_after - GCD in swift?
In the particular case of the example you gave, however, I'd probably put it in an extension on UIView. I'd rewrite it as an instance method, so that instead of blurring another view, we'd start with an existing view and blur self
.
Use an extension, like this:
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)
}
}
Then call it like this:
myview.initializeBlurEffect()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With