Skip to content

Instantly share code, notes, and snippets.

@pocketkk
Last active March 6, 2017 03:26
Show Gist options
  • Save pocketkk/6998a0789ad502263682 to your computer and use it in GitHub Desktop.
Save pocketkk/6998a0789ad502263682 to your computer and use it in GitHub Desktop.
Swift - UIBlurEffect - Modal with Dismiss
import UIKit
import QuartzCore
class ViewController: UIViewController {
var eView: UIVisualEffectView?
@IBAction func blurOut(sender: UIVisualEffectView) {
//eView?.removeFromSuperview()
var removeTransition = CATransition()
removeTransition.type = "moveIn"
removeTransition.subtype = "fromBottom"
eView!.layer.addAnimation(removeTransition, forKey: kCATransitionFade)
eView!.removeFromSuperview()
}
@IBAction func blurUp(){
let offsetFromCenter : Float = 90
let offsetFromSides : Float = 0
let borderColor = UIColor.whiteColor().colorWithAlphaComponent(1).CGColor
let borderWidth : Float = 2
// Create blur background
var blur: UIBlurEffect = UIBlurEffect(style: .Light)
var effectView: UIVisualEffectView = UIVisualEffectView(effect: blur)
effectView.frame = view.frame
// Create vibrant background for text
var vibrancyEffect = UIVibrancyEffect(forBlurEffect: blur)
var vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
vibrancyEffectView.frame = effectView.frame
// Label for vibrant text
var vibrantLabel = UILabel()
vibrantLabel.text = "Vibrant"
vibrantLabel.font = UIFont.systemFontOfSize(72.0)
vibrantLabel.sizeToFit()
vibrantLabel.center = CGPoint(x: view.center.x, y: 72)
// Button for dismiss view
var vibrantButton = UIButton()
vibrantButton.setTitle("Dismiss", forState: .Normal)
vibrantButton.frame = CGRectMake(0,0,100,30)
vibrantButton.sizeToFit()
vibrantButton.center = CGPoint(x: view.center.x, y: view.center.y + view.center.y - 30)
vibrantButton.addTarget(self, action: "blurOut:", forControlEvents: .TouchUpInside)
// Image for view
var image: UIImage = UIImage(named: "4965200407_07f7220c18_q.jpg")
var imageView = UIImageView(image: image)
imageView.sizeToFit()
imageView.center = CGPoint(x: view.center.x, y: view.center.y - offsetFromCenter)
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.clipsToBounds = true
imageView.layer.borderWidth = borderWidth
imageView.layer.borderColor = borderColor
// View for additional content
var conView: UIView = UIView()
conView.frame = CGRectMake(0,0, view.bounds.width - offsetFromSides, view.bounds.height / 2)
conView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
conView.center = CGPoint(x: view.center.x, y: view.center.y + conView.frame.height/2 - offsetFromCenter )
// conView.layer.borderColor = borderColor
// conView.layer.borderWidth = borderWidth
// conView.layer.cornerRadius = 10
// Label for additional content
var conLabel: UILabel = UILabel(frame: conView.bounds)
conLabel.text = "Content Text!!"
conLabel.textAlignment = .Center
conLabel.textColor = UIColor.whiteColor()
conLabel.font = UIFont.systemFontOfSize(12)
conLabel.adjustsFontSizeToFitWidth = true
conLabel.center = conView.center
// Add transition effect
var effectTransition = CATransition()
effectTransition.type = "moveIn"
effectTransition.subtype = "fromTop"
// Add label to the vibrancy view
vibrancyEffectView.contentView.addSubview(vibrantLabel)
// Add buttton to the vibrancy view
vibrancyEffectView.contentView.addSubview(vibrantButton)
// Add the vibrancy view to the blur view
effectView.contentView.addSubview(vibrancyEffectView)
// Add content view to the vibrancy view
effectView.contentView.addSubview(conView)
// Add label to content subview
effectView.contentView.addSubview(conLabel)
// Add image to the vibrancy view
effectView.contentView.addSubview(imageView)
// Add the view to window
effectView.layer.addAnimation(effectTransition, forKey: kCATransitionReveal)
eView = effectView
view.addSubview(eView)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment