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
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 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)
}
}
}
I want to know whether I can create any type of animation with the same UIImageView. Whenever the user clicks a button, it will change the photo using a fade in animation.
import UIKit
class ViewController: UIViewController {
var maximum: Int = 2
var number: Int = 0
var imageNames = ["0", "1"]
#IBOutlet weak var mainImage: UIImageView!
#IBOutlet weak var nextButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Gradient
nextButton.setGradientBackground(colorOne: UIColor(red:0.00, green:0.78, blue:1.00, alpha:1.0), colorTwo: UIColor(red:0.00, green:0.45, blue:1.00, alpha:1.0))
// Drop Shadows
nextButton.layer.shadowColor = UIColor.black.cgColor
nextButton.layer.shadowOffset = CGSize(width: 1, height: 1)
nextButton.layer.shadowRadius = 25
nextButton.layer.shadowOpacity = 0.10
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func changeImages(_ sender: Any) {
number = Int(arc4random_uniform(UInt32(maximum)))
mainImage.image = UIImage(named: "\(number).jpg")
}
}
There is no attempt of me trying to create an animation.
I was just hoping someone could help me create one.
you need to animate alpha value. make a fade in and fade out function like this :
func fadeIn(){
UIView.animate(withDuration: 3) {
self.my.alpha = 0.0
}
}
func fadeOut(){
UIView.animate(withDuration: 3.0) {
self.my.alpha = 1.0
}
}
Then use it when you change your image:
fadeIn()
changeImages() /* your change image logic without animation */
fadeOut()
You can try to animate the alpha
#IBAction func changeImages(_ sender: Any) {
number = Int(arc4random_uniform(UInt32(maximum)))
mainImage.image = UIImage(named: "\(number).jpg")
mainImage.alpha = 0
UIView.animate(withDuration: 1.5, animations: {
self.mainImage.alpha = 1
}
}
I wanted to create a blur effect on an UIImageView with the code below. The problem is that I can see the BlurEffect when I run it in the Simulator but not when I connect my iPhone, here I can only see a gray background....any ideas?
Here is the code I used:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.myImageView.image = UIImage(named: "Desert.png")
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame.size = CGSize(width: 375, height: 667)
blurView.center = myImageView.center
self.myImageView.addSubview(blurView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am running on Xcode 6.1.1 (also tried Xcode 6.2!) and my iPhone OS is 8.1.2
Check if the user hasn't disabled transparency effects:
if !UIAccessibilityIsReduceTransparencyEnabled() {
//your code for blur
} else {
//do something else, add a solid color, etc
}
I have an image view and I needed the user swipe this image down but i don't know how to do this. I put a log to know if the swiping its working and its working well, i want that the image moves when I swipe. I'm very new in iOS. Can you help me??
Here is my ViewController class:
class MainScreenViewController: UIViewController {
#IBOutlet var buttonMenuEntry: UIButton
#IBOutlet var buttonMenuExit: UIButton
#IBOutlet var mainscreenEntryView: UIImageView
#IBOutlet var mainscreenCard: UIImageView
override func viewDidLoad() {
super.viewDidLoad()
var swipeCardDown = UISwipeGestureRecognizer(target: self, action: "respondToGesture:")
swipeCardDown.direction = UISwipeGestureRecognizerDirection.Down
self.mainscreenCard.addGestureRecognizer(swipeCardDown)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonMenuClicked(sender: UIButton){
buttonMenuEntry.hidden = true
buttonMenuExit.hidden = false
}
func respondToGesture(gesture: UIGestureRecognizer){
println("Swiped down")
}
}
Thanks.
Simplest way would be:
func respondToGesture(gesture: UIGestureRecognizer){
UIView.animateWithDuration(0.35, animations: {
mainscreenCard.frame = CGRectMake(mainscreenCard.frame.origin.x,
self.view.frame.height,
mainscreenCard.frame.size.width,
mainscreenCard.frame.size.height)
})
}
Adjust your timing as you wish