How to add transition on UITapGestureRecognizer in Swift 3? - swift

I'm trying to make the image view full screen when I click on the small image. But the image show very fast when I clicked on the image. Can I add some transition time to delay the full screen image showing?
#IBAction func uploadImgAction(_ sender: Any) {
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(DeleteJobViewController.tap))
tapgesture.numberOfTapsRequired = 1
reasonImg.addGestureRecognizer(tapgesture)
}
func tap(){
print("tap")
let newImageView = UIImageView(image: reasonImg.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.alpha = 1
UIView.animate(withDuration: 1.5, animations: {
sender.view?.alpha = 0
sender.view?.removeFromSuperview()
})
}

Try replacing the self.view.addSubview(newImageView) line with:
DispatchQueue.main.async {
UIView.transition(with: self.view, duration:0.6, options: UIViewAnimationOptions.showHideTransitionViews, animations: {
self.view.addSubview(self.newImageView)
}, completion: nil)
}
You can also try setting newImageView.alpha = 0 before running this code:
UIView.animate(withDuration: 1.5, animations: {
self.newImageView.alpha = 1.0
})
For animating the disappear effect, in the code above, replace the addSubview statement with newImageView.removeFromSuperview().
And similarly you can set this after you have set the newImageView.alpha = 1:
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
UIView.animate(withDuration: 1.5, animations: {
self.newImageView.alpha = 0
})
}
EDIT:
Please add newImageView as a property at the top of your view controller.
Then in your tap function set this instead of let newImageView:
self.newImageView = UIImageView(image: reasonImg.image)
Now if you run the code it should compile.

yourImageView.alpha = 0
*your addSubview in the middle
UIView.animate(withDuration: 0.5, animations: {
yourImageView.alpha = 1
})
First set your image to be invisible, then animate (in my example for 0.5 sec) to be visible.

func tap() {
print("tap")
let newImageView = UIImageView(image: reasonImg.image)
newImageView.frame = self.view.frame
newImageView.transform = CGAffineTransform.init(scaleX: 0.0, y: 0.0)
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
UIView.animate(withDuration: 0.5, animations: {
newImageView.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
})
}

Related

I want to make an if statement that each image would be full screen when the user is tap

here is the code that I check if they images are tapped but only the 3 image is apply full screen. As you can see I set the image is tapped to statusImageView to call the function of zoomImage So I want fixed for all images.
func imageTapped() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
if detailsImage[0].tag == 0 {
detailsImage[0].isUserInteractionEnabled = true
detailsImage[0].addGestureRecognizer(tapGestureRecognizer)
self.statusImageView = detailsImage[0]
}
let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
detailsImage[1].isUserInteractionEnabled = true
detailsImage[1].addGestureRecognizer(tapGestureRecognizer1)
self.statusImageView = detailsImage[1]
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageZoom(tapGestureRecognizer:)))
detailsImage[2].isUserInteractionEnabled = true
detailsImage[2].addGestureRecognizer(tapGestureRecognizer2)
self.statusImageView = detailsImage[2]
}
let blackBackgroundColor = UIView()
let tappedImage = UIImageView()
var statusImageView: UIImageView?
let navigationBarView = UIView()
#objc func imageZoom(tapGestureRecognizer: UITapGestureRecognizer) {
annimationImage(detailsImage: statusImageView!)
}
And then I call the annihilationImage function which is takes the statusImageView as input
func annimationImage(detailsImage: UIImageView) {
if let startingFrame = statusImageView?.superview?.convert(statusImageView!.frame, to: nil) {
statusImageView!.alpha = 0
blackBackgroundColor.frame = self.view.frame
blackBackgroundColor.backgroundColor = UIColor.black
blackBackgroundColor.alpha = 0
view.addSubview(blackBackgroundColor)
navigationBarView.frame = CGRect(x: 0, y: 0, width: 1000, height: 100)
navigationBarView.backgroundColor = UIColor.black
navigationBarView.alpha = 0
if let keyWindow = UIApplication.shared.keyWindow {
keyWindow.addSubview(navigationBarView)
}
let tappedImage = UIImageView()
tappedImage.backgroundColor = .gray
tappedImage.frame = startingFrame
tappedImage.contentMode = .scaleToFill
tappedImage.isUserInteractionEnabled = true
tappedImage.image = statusImageView?.image
tappedImage.clipsToBounds = true
view.addSubview(tappedImage)
UIView.animate(withDuration: 0.75, animations: {
tappedImage.frame = CGRect(x: 0, y: y, width: self.view.frame.size.width, height: 300)
})
}
}
I want to make an if statement that while check which image is tapped
You can use tapGestureRecognizer.view for getting a view that is assigned a tap gesture. And by this, you can also get the view tag (your view is corresponding to the image view in your code)
#objc func imageZoom(tapGestureRecognizer: UITapGestureRecognizer) {
let senderView = tapGestureRecognizer.view // Here you get a view that is associated with the gesture
let tag = senderView?.tag // Here you get a tag that is assigned to all image
// Add your condition here by tag
annimationImage(detailsImage: statusImageView!)
}

swipe between UIView with segemented control programmatically

hi fellow developer I want to swipe between uiview with scrollView while changing the the index and small indicator move left and right. but is not responding while I try to swipe. this is my picture and my setup to scroll between uiview. can you tell me where I am missing?
let segmentedControl = UISegmentedControl()
let containerView = UIView()
let indicatorView = UIView()
let leftView = UIView()
let rightView = UIView()
let scrollView = UIScrollView()
var slides = [UIView]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
slides.append(leftView)
slides.append(rightView)
segmentedControl.insertSegment(withTitle: "Harian", at: 0, animated: true)
segmentedControl.insertSegment(withTitle: "Mata Pelajaran", at: 1, animated: true)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: #selector(handleTap), for: .valueChanged)
setupSlideScrollView()
}
func setupSlideScrollView() {
view.addSubview(scrollView)
scrollView.isPagingEnabled = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
for i in 0..<slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(slides[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
segmentedControl.selectedSegmentIndex = Int(pageIndex)
}
#objc func handleTap(_ sender: UISegmentedControl) {
indicatorView.frame.origin.x = (segmentedControl.frame.width / CGFloat(segmentedControl.numberOfSegments)) * CGFloat(segmentedControl.selectedSegmentIndex)
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
self.indicatorView.transform = CGAffineTransform(scaleX: 0.7, y: 1)
}) { (finish) in
UIView.animate(withDuration: 0.4, animations: {
self.indicatorView.transform = CGAffineTransform.identity
})
}
}

UIButton resize animation is not animating

I'm trying to make that my button double its size using the UIView animation, but for some reason it is not working it the size goes right, but no animated.
The function that should animate the button resizing
Irrelevant code above
#objc func createButtonPressed(){
//Removes the bottom stack with buttons
if let stackButton = self.view.viewWithTag(50){
stackButton.removeFromSuperview()
}
//Add the button back with half ares size
bottomHolder.addSubview(rightButton)
rightButton.setImage(nil, for: .normal)
rightButton.addTarget(self, action: #selector(anima), for: .touchUpInside)
rightButton.topAnchor.constraint(equalTo: bottomHolder.topAnchor).isActive = true
rightButton.trailingAnchor.constraint(equalTo: bottomHolder.trailingAnchor).isActive = true
rightButton.bottomAnchor.constraint(equalTo: bottomHolder.bottomAnchor).isActive = true
}
#objc func anima(){
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.rightButton.leadingAnchor.constraint(equalTo: self.bottomHolder.leadingAnchor).isActive = true
self.rightButton.layoutIfNeeded()
}, completion: nil)
}
Irrelevant code below
try this one:
let buttonFinalWidth = UIScreen.main.bounds.width
DispatchQueue.main.async {
UIView.animate(withDuration: 5.0) {
self.rightButton.widthAnchor.constraint(equalToConstant: buttonFinalWidth).isActive = true
self.view.layoutIfNeeded()
}
}

UIView alpha does not update after first call to change it. (Swift 4.2)

I have a UIView on which I want to change the alpha from 0 to 0.5 when the user opens a slide out menu. When the user taps the darkened area the alpha should go back to 0. Currently, when the menu button is tapped the alpha changes to 0.5 adding a dimming effect to the view. However, a breakpoint and print statement show that when tapping the UIView the line to change the alpha back to 0 runs, but the UI still shows a 0.5 alpha. Everywhere I have looked the code is exactly the same, so I am unsure what I am doing wrong.
let dimView = UIView()
func setupMenuButton() {
let menuButton = UIBarButtonItem(title: "Menu", style: .plain, target: self, action: #selector(showMenu))
navigationItem.rightBarButtonItem = menuButton
}
#objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
let dimView = UIView()
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
#objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
print("dim view is not transparent")
})
}
The dimView created in showMenu is not the same dimView created in the first line. You are creating a brand new dimView in showMenu.
One way to fix this is to not create a new dimView in showMenu, and use the one declared outside instead:
#objc func showMenu() {
//TODO: present menu and dim background
if let window = UIApplication.shared.keyWindow {
// notice I deleted a line here
dimView.backgroundColor = UIColor.black
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDimView)))
window.addSubview(dimView)
dimView.frame = window.frame
dimView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
dimView.alpha = 0.5
})
}
}
#objc func dismissDimView() {
UIView.animate(withDuration: 0.5, animations: {
self.dimView.alpha = 0
// here I remove the dimView from the window so that it can be added back in the next time showMenu is called
}, completion: { [weak self] _ in self?.dimView.removeFromSuperView() })
}

Add Gesture Recogniser

For some reason the gesture recogniser does not work, it doesn't make the background normal again. How do I fix it so the selector (self.dissmissMenu) works?
In View controller:
let menu = Menu()
#IBAction func menuButton(sender: AnyObject) {
menu.runMenu()
}
In Menu:
import UIKit
class Menu: NSObject {
let dimming = UIView()
public func runMenu(){
if let window = UIApplication.sharedApplication().keyWindow{
dimming.frame = window.frame
dimming.backgroundColor = UIColor(white: 0, alpha: 0.5)
dimming.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector(self.dissmissMenu())))
window.addSubview(dimming)
UIView.animateWithDuration(0.5, animations: {
self.dimming.alpha = 1
})
}
}
public func dissmissMenu(){
UIView.animateWithDuration(0.5, animations: {
self.dimming.alpha = 0
})
}
}
Try to rewrite the call to addGestureRecognizer like this:
dimming.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dissmissMenu())))