I have used the code as below. But it only blur one position. Now I want to blur multiple positions at once
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btn_blur(_ sender: Any) {
// 1
let darkBlur = UIBlurEffect(style: .dark)
// 2
let blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = CGRect(x: 20, y: 30, width: 50, height: 50)
// 3
imageView.addSubview(blurView)
}
}
You could store an array of frames in your view controller, and fill that with your server response. Then use those frames to create the blur views.
import UIKit
class ViewController: UIViewController {
var frames = [CGRect]() // load this from your server data
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btn_blur(_ sender: Any) {
let darkBlur = UIBlurEffect(style: .dark)
for frame in frames {
let blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = frame
imageView.addSubview(blurView)
}
}
}
Related
I was creating animations for buttons using extension in swift but when i call the animation function it generates error.
import UIKit
extension UIButton{
func wiggle() {
let wiggleAnim = CABasicAnimation(keyPath: "psoition")
wiggleAnim.duration = 0.05
wiggleAnim.repeatCount = 5
wiggleAnim.autoreverses = true
wiggleAnim.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
wiggleAnim.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
layer.add(wiggleAnim, forKey: "position")
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var colorizeBtn: UIButton!
#IBOutlet weak var wiggleBtn: UIButton!
#IBOutlet weak var dimBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func colorizeBtnWasPressed(_ sender: Any) {
}
#IBAction func wiggleBtnWasPressed(_ sender: Any) {
wiggleBtn.wiggle()
}
#IBAction func dimBtnwasPressed(_ sender: Any) {
}
}
View Controller
Your wiggleBtn is of type UIView but you write an extension to UIButton.
Either change the extension to UIView or change the type of wiggleBtn to UIButton.
I have UICollectionView with a lot of cells. The cells have images on them that need to be blurred. That's why I am adding UIVisualEffectView with blur effect to the image views. The cells are like pages - every cell takes up the whole screen.
The problem is with the second cell. When it appears the blurred view is added visibly and it's ugly. The rest of the cells have the blurred view already when they become visible on the screen and are okay. This is the code I am using to add the blur view:
class ImageCell: UICollectionViewCell {
#IBOutlet weak var mainImageView: UIImageView!
#IBOutlet weak var backgroundImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
let regularBlur = UIBlurEffect(style: .regular)
let blurView = UIVisualEffectView(effect: regularBlur)
blurView.frame = backgroundImageView.bounds
blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
backgroundImageView.addSubview(blurView)
}
}
Modifying your code:
class ImageCell: UICollectionViewCell {
#IBOutlet weak var mainImageView: UIImageView!
#IBOutlet weak var backgroundImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
backgroundImageView.isHidden = true
let regularBlur = UIBlurEffect(style: .regular)
let blurView = UIVisualEffectView(effect: regularBlur)
blurView.frame = backgroundImageView.bounds
blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
backgroundImageView.addSubview(blurView)
backgroundImageView.isHidden = false
}
}
You can code like this:
class ImageCell: UICollectionViewCell {
#IBOutlet weak var mainImageView: UIImageView!
#IBOutlet weak var backgroundImageView: UIImageView!
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .regular))
override func awakeFromNib() {
super.awakeFromNib()
blurView.frame = backgroundImageView.bounds
blurView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
backgroundImageView.addSubview(blurView)
}
override func prepareForReuse() {
blurView.isHidden = true
}
}
I spent around 5 hours searching on StackOverFlow about how to implement this and I still not found a solution yet!
I have created a new iOS Swift app just to explain you what exactly I am trying to resolve.
First, I have a UIButton on main storyboard and when you click on it, it creates a new object from class WebViewAsPopup which creates programmatically a new WebView and a background as UIView with alpha 0.5, add some styling and then it add them to the main view controller ViewController as a popup.
Secondly, I couldn't set a delegate to those WebView's to handle UIWebViewDelegate and listen to both webViewDidStartLoad and webViewDidFinishLoad
Here's my code:
ViewController.swift
//
// ViewController.swift
// MySimpleApp
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var showPopupBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showPopupTapped(_ sender: Any) {
let popup = WebViewAsPopup()
popup.showWebViewPopup(on: self)
}
}
WebViewAsPopup.swift
//
// WebViewAsPopup.swift
// MySimpleApp
//
import WebKit
class WebViewAsPopup: NSObject, UIWebViewDelegate {
func showWebViewPopup(on controller: UIViewController) {
// Popup background
let bg = UIView()
bg.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
bg.layer.backgroundColor = UIColor.black.withAlphaComponent(0.5).cgColor
// Webview sizing
let width = UIScreen.main.bounds.width - 60
let height = UIScreen.main.bounds.height - 200
let x = UIScreen.main.bounds.width/2 - width/2
let y = UIScreen.main.bounds.height/2 - height/2
// Webview stuff
let webView = UIWebView()
webView.frame = CGRect(x: x, y: y, width: width, height: height)
let url = URL(string: "https://google.com")
let request = URLRequest(url: url!)
// webView.delegate = self << it crash here
webView.loadRequest(request)
// Styling webview popup
webView.layer.borderWidth = 1
webView.layer.borderColor = UIColor.black.cgColor
webView.layer.masksToBounds = true
webView.layer.cornerRadius = 10
// Add bg then webview to main view controller
controller.view.addSubview(bg)
controller.view.addSubview(webView)
}
func webViewDidStartLoad(_ webView: UIWebView)
{
print("#webViewDidStartLoad!") // << it doesn't fire :(
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
print("#webViewDidFinishLoad!") // << it doesn't fire :(
}
}
As I noted in my comments, you need to save the popup object somewhere in order to retain it. If not (as in your example code), then it will be deallocated as soon as the action method showPopupTapped(_:) is completed.
Try this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var showPopupBtn: UIButton!
let popup = WebViewAsPopup()
#IBAction func showPopupTapped(_ sender: Any) {
self.popup.showWebViewPopup(on: self)
}
}
Bonus Note:
If you're wondering why you don't need to do the same to the background view and web view in your popup object, that's because views automatically retain their subviews.
So, once you add your background view and web view as subviews to a parent view, their retain count goes up and will survive their current scope.
Picture of my code
import UIKit
class ViewController: UIViewController {
#IBOutlet var addItemView: UIView!
#IBOutlet weak var visualEffectView: UIVisualEffectView!
var effect: UIVisualEffect!
override func viewDidLoad() {
super.viewDidLoad()
effect = visualEffectView.effect
visualEffectView.effect = nil
addItemView.layer.cornerRadius = 5
}
func animateIn(){
self.view.addSubview(addItemView)
addItemView.center = self.view.center
addItemView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
addItemView.alpha = 0
UIView.animate(withDuration: 0.4){
self.visualEffectView.effect = self.effect
self.addItemView.alpha = 1
self.addItemView.transform = CGAffineTransform.identity
}
}
#IBAction func sourcesButton(_ sender: Any) {
animateIn()
}
#IBAction func sourceDone(_ sender: Any) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am trying to make a blurry modal that pops up when the user clicks a button, but for some reason this code is not executing. I was following a tutorial on youtube (Youtube Link) I would appreciate the help.
Create blur view like this instead:
var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
view.addSubview(blurEffectView)
And after it is created you can set alpha to 0 and animate it to 1.
Please note that animation with UIBlurEffect looks horrible on the device :p
I'm working on a project to have hidden images appear when the button is pressed. My only problem is that when I click on the button the image appears. I then have to click on the button again for it to show up in the correct area of my view controller. Can someone help point me in the right direction please?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.hidden = true
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.hidden = false
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
}
This may or may not help, but i would recommend that instead of using the image.hidden property, set the alpha value of the image. So image.alpha = 0 for hidden and image.alpha = 1 for visible. This would also allow you to then fade the image in and out if you wanted. Additionally I would set the image in viewDidLoad() instead of the Action. Here's how I would redo your code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.alpha = 0
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.alpha = 1
}
}
I also believe setting the frame twice isn't useful as the frame will be set in viewDidLoad()