My swift code below places 2 different image views on a uiview controller. When the user hits a imageivew I want that specific imageview to change color. I dont know how to apply the method to multiple image views. I think you would use the sender method.
import UIKit
class ViewController: UIViewController {
var slider = UISlider()
var image1 = UIImageView()
var image2 = UIImageView()
var with = 80
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[slider,image1,image2].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = .systemOrange
}
slider.frame = CGRect(x: view.center.x-115, y: view.center.y+200, width: CGFloat(with), height: 30)
image1.frame = CGRect(x: view.center.x-115, y: view.center.y, width: CGFloat(with), height: 30)
image2.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: CGFloat(with), height: 30)
slider.minimumValue = 10
slider.maximumValue = 150
image1.isUserInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image1.addGestureRecognizer(gestureRecognizer)
image2.addGestureRecognizer(gestureRecognizer)
}
#objc func imageViewTapped(sender: UITapGestureRecognizer) {
if let imageView = sender.view as? UIImageView {
imageView.backgroundColor = .yellow
}
}
}
I tried your code:
add below two lines to enable the user interaction as mentioned by #matt:
image1.isUserInteractionEnabled = true
image2.isUserInteractionEnabled = true
and create two separate objects for gesture:
let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image1.addGestureRecognizer(gestureRecognizer1)
let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image2.addGestureRecognizer(gestureRecognizer2)
Output:
Happy coding...
A UIGestureRecognizer is to be used with a single view. So, you need to create two seperate UITapGestureRecognizer object for two different views.
For example:-
let image1GestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image1.addGestureRecognizer(image1GestureRecognizer)
let image2GestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image2.addGestureRecognizer(image2GestureRecognizer)
Related
I have the following function I use to customize the navigation bar across almost all the apps view controllers and table view controllers - instead of replicating the code numerous times I am looking for way to easily call the function on those view controllers needing it.
I have tried wrapping in extension UIViewController { } but run into a selector issue saying the following:
Argument of '#selector' cannot refer to local function
'Tapped(tapGestureRecognizer:)'
Code:
func navBar(){
// Profile Image
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.image = UIImage(url: URL(string: "test.com"))
imageView.contentMode = UIView.ContentMode.scaleAspectFit
imageView.layer.cornerRadius = 20
imageView.layer.masksToBounds = true
containView.addSubview(imageView)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
#objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
print("Profile Tapped")
}
How can this UIImage be seen in the navigation bar across various view controller without needing to rewrite the same code across all.
Lot a way to do it. I'll usually istance and personalize an UIViewController and use it around the whole app.
class baseController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//call your navBar here
navbar()
}
func navBar(){
// Profile Image
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.image = UIImage(url: URL(string: "test.com"))
imageView.contentMode = UIView.ContentMode.scaleAspectFit
imageView.layer.cornerRadius = 20
imageView.layer.masksToBounds = true
containView.addSubview(imageView)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
}
Now instance this class whenever you want and your controller will get your navBar() every time with this
class mineController:baseController {
//your code here...
}
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!)
}
My swift code below has a button and when touch places a imageview on the viewcontroller. The only thing I want to do is when a individual imageview not all of the imageview just the one select is touch I would like to change the color from purple to blue only if touched and one at a time.
import UIKit
class ViewController: UIViewController {
var count: Int = 0
var ht = -90
var ww = 80
var moveCounter = 0
var counter = 0
var arrTextFields = [UIImageView]()
var b7 = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[b7].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = .systemOrange
}
b7.frame = CGRect(x: view.center.x-115, y: view.center.y + 200, width: 70, height: 40)
b7.addTarget(self, action: #selector(addBOx), for: .touchUpInside)
}
//func that adds imageview.
#objc func addBOx() {
let subview = UIImageView()
subview.isUserInteractionEnabled = true
arrTextFields.append(subview)
view.addSubview(subview)
subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ht), width: CGFloat(ww), height: 35)
subview.backgroundColor = .purple
subview.tag = count
count += 1
ht += 50
arrTextFields.append(subview)
}}
You already set the isUserInteractionEnabled property of the image view to true. To respond to touch events, create a UITapGestureRecognizer and add it to the image view like so:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
subview.addGestureRecognizer(gestureRecognizer)
Then inside of the imageViewTapped method you can extract the image view from the sender argument:
#objc func imageViewTapped(sender: UITapGestureRecognizer) {
if let imageView = sender.view as? UIImageView {
// Change the imageView's background here
}
}
I have got a textfield in my Swift code
let PassINP: UITextField = {
let textField = UITextField()
textField.rightViewMode = .always
var imageR = UIImageView(frame: CGRect(x:7,y:4,width:24,height:27))
imageR.image = #imageLiteral(resourceName: "eye")
var paddingR = UIView(frame: CGRect(x: 0, y: 0, width:40, height: 32))
paddingR.addSubview(imageR)
textField.rightView = paddingR
textField.isSecureTextEntry = true
return textField
}()
And i want to call a function when rightView is clicked.How can i achieve that?
I have tried adding this code
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.rightViewTapped))
paddingR.addGestureRecognizer(tapGesture)
But it was not working
For that you need to add TapGesture to rightView of your UITextField.
//Add UITapGestureRecognizer to your PassINP's rightView `viewDidLoad`
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(favoriteButtonPressed))
PassINP.rightView?.addGestureRecognizer(tapGesture)
Now add rightViewTapped method to handle the tap on the rightView
func rightViewTapped(_ gesture: UITapGestureRecognizer) {
print("rightViewTapped")
}
I have no idea why my code isn't working :
let profileImageView: UIImageView = {
let image = UIImageView()
image.image = UIImage(named: "gameofthrones_splash")
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFill
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView))
image.addGestureRecognizer(tapGesture)
image.isUserInteractionEnabled = true
return image
}()
This imageView is loaded in the ViewDidLoad (I can see it)
func handleSelectProfileImageView() {
print("123")
}
But nothing get returned in the console when I'm tapping on it ! WTF?
It's in the target. This works when place in the viewDidLoad():
profileImageView.frame = CGRect(x: 60, y: 60, width: 70, height: 70)
view.addSubview(profileImageView)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView))
profileImageView.addGestureRecognizer(tapGesture)
The target is "self", which is the UIViewController (not the UIImageView). In most cases (I'm sure there are outliers) you want to record a gesture in a view, but work with the gesture in a view controller.
Okay the solution was quite simple actually. You have to make it a lazy var instead of a let.
lazy var profileImageView: UIImageView = {
let image = UIImageView()
image.image = UIImage(named: "gameofthrones_splash")
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFill
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView))
image.addGestureRecognizer(tapGesture)
image.isUserInteractionEnabled = true
return image
}()
Swift 3.0:
There was some error I removed it -
Put this code in viewdidLoad
override func viewDidLoad() {
super.viewDidLoad()
var profileImageView:UIImageView {
var image = UIImageView()
image.backgroundColor = UIColor.red;
image.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFill
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView))
image.addGestureRecognizer(tapGesture)
image.isUserInteractionEnabled = true
return image
}
view.addSubview(profileImageView);
// Do any additional setup after loading the view.
}
func handleSelectProfileImageView() {
print("123")
}
Just to test I removed the image, you can add it. But it's working fine.
I fixed this issue using an UIButton ->
let profileImageView: UIButton = {
let iv = UIButton()
let image = UIImage(named: "r2d2")
iv.setImage(image, for: .normal)
iv.translatesAutoresizingMaskIntoConstraints = false
iv.addTarget(self, action: #selector(selectProfileImage), for: .touchUpInside)
iv.isUserInteractionEnabled = true
return iv
}()
#objc func selectProfileImage(){
print("Hello")
}