How add gesture into the tableviewcell? - ios

everyone!
I am trying to add gesture to a UIImageView into the TableViewCell.
When click on the ImageView - the color changes.
class StocksCellView: UITableViewCell {
#IBOutlet weak var logoImageView: UIImageView!
#IBOutlet weak var tickerLabel: UILabel!
#IBOutlet weak var favouriteImageView: UIImageView!{ didSet {
let panGesture = UITapGestureRecognizer(target: self,
action: #selector(gestureAction))
favouriteImageView.addGestureRecognizer(panGesture)
}
}
#IBOutlet weak var companyNameLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var deltaLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
#objc func gestureAction() {
favouriteImageView.tintColor = UIColor.yellow
}
}
But nothing works.
If i change favouriteImageView.addGestureRecognizer(panGesture)
on addGestureRecognizer(panGesture)
Then, by clicking on the cell, the color will change.
But I want by clicking on the picture.
enter image description here

First of all you must check and change like below:
favouriteImageView.isUserInteractionEnabled = true

Related

Hierarchy Problem with presenting Viewcontrollers, can someone help me?

[![enter image description here][2]][2]i want to programm an App with two ViewControllers
The first one performs a segue to the second VC.
The Second VC has a Scrollview with different Views.
I did everything with Autolayout not with code
When i run the Application i get always the error "whose view is not in the window hierarchy!" and can't scroll to the bottom. I tried to find some solutions but nothing helped.
Hope someone can help me with the problem.
my first VC runs a normal performSegue("identifier",sender )
my code for the second VC is
import UIKit
class FragenViewController: UIViewController {
var fragen = [Question]()
var highscore = 0
var questionNumber = 0
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var answerButton1: UIButton!
#IBOutlet weak var answerButton2: UIButton!
#IBOutlet weak var answerButton3: UIButton!
#IBOutlet weak var answerButton4: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var currentQuestionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func answerButton_Tapped(_ sender: UIButton) {
}
}
the rest is with Main Storyboard Drag and drop
The Code from the first VC is
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var highscoreLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Layout
startButton.backgroundColor = UIColor.systemFill
startButton.layer.cornerRadius = 20
startButton.titleLabel?.font = UIFont(name: "Arial", size: 20)
startButton.layer.borderWidth = 2
startButton.tintColor = UIColor.systemBlue
highscoreLabel.font = UIFont(name: "Arial", size: 18)
}
#IBAction func start_Button(_ sender: Any) {
performSegue(withIdentifier: "startGame", sender: sender)
}
}

How to get tag of an imageview in a stackview

I have a horizontal uistackview and i have 5 imageviews in it. I need to get id of the imageview that is tapped. I tried adding tap gesture identifier and get sender's id, but i am not getting it.
What i have tried till now is as follows
#IBOutlet weak var userRateViewStar1ImageOutlet : UIImageView!
#IBOutlet weak var userRateViewStar2ImageOutlet : UIImageView!
#IBOutlet weak var userRateViewStar3ImageOutlet : UIImageView!
#IBOutlet weak var userRateViewStar4ImageOutlet : UIImageView!
#IBOutlet weak var userRateViewStar5ImageOutlet : UIImageView!
#IBOutlet weak var userRateViewTextAreaOutlet : UITextView!
#IBOutlet var starOutletCollectionOutlet : [UIImageView]!
#IBAction func starRatingAction(_ sender: UITapGestureRecognizer)
{
var tag = sender.view!.tag
print(tag)
}
Basically i am trying to implement rate feature.
So can someone suggest me a way to implement this?
Thanks in advance
An easier way might be to create an IBOutletCollection e.g:
//1. Create An Array Of Your ImageViews
#IBOutlet var ratingStarImageViews: [UIImageView]!
Then assign a tag and the gesture to them e.g:
//2. Assign Tags To The Them
var tag = 0
for view in ratingStarImageViews{
let ratingGesture = UITapGestureRecognizer(target: self, action: #selector(starRatingAction(_:)))
view.addGestureRecognizer(ratingGesture)
view.tag = tag
tag += 1
}
Hope it helps.

Why viewWithTag is returning nil

activite1Label as the tag 1
class StatsViewController: UIViewController {
#IBOutlet weak var activite1Label: UILabel!
#IBOutlet weak var activite2Label: UILabel!
#IBOutlet weak var activite3Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
activite1Label.text = activite[0]
activite2Label.text = activite[1]
activite3Label.text = activite[2]
miseAjourTotal()
}
func miseAjourTotal() {
let leLabel = view.viewWithTag(1) as! UILabel
print("leLabel: \(leLabel.text)")
}
}
Nothing in your code tells that the label has the tag 1. You should go to your storyboard and check if the label does have tag 1 or set the tag programmatically

Changing the image of a button based on control state

I am trying to change the image of a button when clicked, they are stars like a rating system. when clicked the image remains the same and does not change when clicked. I set the default image in story board.
class DetailViewController: UIViewController {
#IBOutlet weak var detailTitleViewLbl: UILabel!
#IBOutlet weak var detailDescription: UILabel!
#IBOutlet weak var yearLabel: UILabel!
#IBOutlet weak var star1: UIButton!
#IBOutlet weak var star2: UIButton!
#IBOutlet weak var star3: UIButton!
#IBOutlet weak var star4: UIButton!
#IBOutlet weak var star5: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func star1Pressed() {
star1.setImage(#imageLiteral(resourceName: "favorite (1).png"), for: UIControlState.selected)
}
func star2Pressed() {}
func star3pressed() {}
func star4pressed() {}
}
A button doesn't change to the selected state when clicked. You need to write code that sets the button's selected flag to true when it's clicked.
Note that you can connect more than one IBAction to a button, so you could have one action that sets the selected flag to true, and another that does custom "stuff", so you don't have to duplicate the "boilerplate" code to set the selected state in the IBAction of every such button.

Generate buttons randomly (Swift)

I am making an app where you click a button, causing a new button to appear, and the button you clicked to disappears. I want the buttons to appear randomly, and I also want it to always be 4 buttons visible all the time. I can't really figure out how to do this, thanks!
Here's an image of how the start screen looks like:
I've been trying to do this using UIView's hidden property, but it doesn't work so well.
Heres my code so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var blankButton1: UIButton!
#IBOutlet weak var blankButton2: UIButton!
#IBOutlet weak var blankButton3: UIButton!
#IBOutlet weak var blankButton4: UIButton!
#IBOutlet weak var blankButton5: UIButton!
#IBOutlet weak var blankButton6: UIButton!
#IBOutlet weak var blankButton7: UIButton!
#IBOutlet weak var blankButton8: UIButton!
#IBOutlet weak var blankButton9: UIButton!
#IBOutlet weak var bluebutton1: UIButton!
#IBOutlet weak var bluebutton2: UIButton!
#IBOutlet weak var bluebutton3: UIButton!
#IBOutlet weak var bluebutton4: UIButton!
#IBOutlet weak var bluebutton5: UIButton!
#IBOutlet weak var bluebutton6: UIButton!
#IBOutlet weak var bluebutton7: UIButton!
#IBOutlet weak var bluebutton8: UIButton!
#IBOutlet weak var bluebutton9: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
bluebutton1.hidden = true
bluebutton2.hidden = true
bluebutton3.hidden = true
bluebutton4.hidden = true
bluebutton6.hidden = true
}
#IBAction func FirstHidden(sender: AnyObject) {
bluebutton1.hidden = true
bluebutton2.hidden = false
}
#IBAction func SecondHidden(sender: AnyObject) {
bluebutton2.hidden = true
bluebutton9.hidden = false
}
#IBAction func ThirdHidden(sender: AnyObject) {
bluebutton3.hidden = true
bluebutton8.hidden = false
}
#IBAction func FourthHidden(sender: AnyObject) {
bluebutton4.hidden = true
bluebutton5.hidden = false
}
#IBAction func FiftHidden(sender: AnyObject) {
bluebutton5.hidden = true
bluebutton6.hidden = false
}
#IBAction func SixthHidden(sender: AnyObject) {
bluebutton6.hidden = true
bluebutton4.hidden = false
}
#IBAction func SeventhHidden(sender: AnyObject) {
bluebutton7.hidden = true
bluebutton5.hidden = false
}
#IBAction func EightHidden(sender: AnyObject) {
bluebutton8.hidden = true
bluebutton3.hidden = false
}
#IBAction func NinethHidden(sender: AnyObject) {
bluebutton9.hidden = true
bluebutton7.hidden = false
}
}
Start by putting all the buttons in an array (use IBOutletCollection). Now you can use your random number function to index into the array.
Initially, hide all the buttons in the array.
Take a mutable copy of the array and iterate for how many buttons you want to show. On each iteration generate a random index, show that button and remove it from the mutable copy.
Repeat...

Resources