How to highlight one part of a tableview cell in swift? - swift

I have two clicks of gesture recognizer in my tableview cell to access different storyboards.
My second click is in the extreme right of my cell and I want when I click on it to just highlight that button and not the whole cell which includes the other button
So my question is : how to separate highlights of different clicks in a tableview cell ?

First you need to set the
tableView.allowsSelection = false
You can add two views in the cell and add tap
func setAccessoryViewTapGestures() {
var tapGestureforLeftAccesoryView = UITapGestureRecognizer()
tapGestureforLeftAccesoryView = UITapGestureRecognizer(target: self, action: #selector(leftAccessoryViewTapped(_:)))
tapGestureforLeftAccesoryView.numberOfTapsRequired = 1
tapGestureforLeftAccesoryView.numberOfTouchesRequired = 1
leftAccessoryView.addGestureRecognizer(tapGestureforLeftAccesoryView)
leftAccessoryView.isUserInteractionEnabled = true
var tapGestureforRightAccesoryView = UITapGestureRecognizer()
tapGestureforRightAccesoryView = UITapGestureRecognizer(target: self, action: #selector(rightAccessoryViewTapped(_:)))
tapGestureforRightAccesoryView.numberOfTapsRequired = 1
tapGestureforRightAccesoryView.numberOfTouchesRequired = 1
rightAccessoryView.addGestureRecognizer(tapGestureforRightAccesoryView)
rightAccessoryView.isUserInteractionEnabled = true
}
Here rightAccessoryView and leftAccessoryView are the view in the cell
#objc private func leftAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
#objc private func rightAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
I think this helps

Related

tableview custom cell button single selection/deselction

i have been coding for a while i am stuck with tableview single selection/deselection.
I have a tableview with custom cell which has a button. if i click on one button another button in bottom gets selected as well.For example when i click on button with index 2 another button gets click in the button.
it should be like when i click on one button other buttons should be deselected.
Thanks!
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected == true {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.isSelected = false
}
else {
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
cell.EventReviewQuickeReviewBtn.isSelected = true
}
}
Cells get reused. You need to ensure the exact same set of properties are set/reset in both conditions.
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.white // or whatever
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.red, for: .normal) // or whatever
} else {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.black // or whatever
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.green // or whatever
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 3 // or whatever
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
}
cell.EventReviewQuickeReviewBtn.isSelected = !cell.EventReviewQuickeReviewBtn.isSelected
}
when you select any button then you should have mark isSelected property to false
assuming you have a list of models for your table view like
class Example{
...
var isSelected: Bool
...
}
(in ViewController)
let list: [Example] = [example1, example2, example3]
on tap of any button deselect all then selected desire button like
list.forEach { (example) in
example.isSelected = false
}
list[selectedButtonIndex].isSelected = true
and then reload your tableView.

swift4 UISwipeGestureRecognizer Viewcontroller with large title and cells not working

I have a ViewController with large titles. In my ViewController are 2 cells. UISwipeGestureRecognizer is not working by swiping down. I want to call a searchController with this swipe-action in the titleView. Could the reason be the large title ?( with swipe down the title goes down and becomes greater, thats OK ). But my swipe action will not be recognized. Any ideas?
my code: - function called in viewDidLoad() in ViewController
func swipeSetup() {
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(funcSwipedDown(sender:)))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}
#objc func funcSwipedDown(sender: UISwipeGestureRecognizer){
Utils.displayAlert(title: "Info", message: "swipe down")
self.addNavigationbar() // func for adding search bar
}

Display menu list on Double Tap gesture - Swift3

I'm new to IOS and I'm trying to display list of buttons on double tap of UIView. I have the below code to capture the double tap gesture:
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.minimumPressDuration = 0.0
menuTap.numberOfTapsRequired = 2
menuTap.delaysTouchesBegan = true
self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
}
I need to design a panel to show a list of buttons such as start, stop and pause. Can anybody guide me on how to design a panel on the tapped position?
You need to use UITapGestureRecognizer for that not the UILongPressGestureRecognizer. Set UITapGestureRecognizer with numberOfTapsRequired to 2.
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.numberOfTapsRequired = 2
//No need to set isUserInteractionEnabled to true because by default it is true for `UIView`
//self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
Add action method of tapGesture like this.
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
let point = recognizer.location(in: self.view)
//Get your view from nib
let view = CustomView()
//set its origin to this point
view.frame.origin = point
//add your view in self.view
self.view.addSubview(view)
}

Swift: Re-activating Tap Gesture on Disclosure Indicators

I have a Swift project that has a Table View Controller with multiple Static Cell Sections. Some cells have UITextFields and others have Accessory: Disclosure Indicators. I’ve implemented the following Swift code to dismiss the keyboard when the background is tapped:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
This works great for releasing the keyboard on tapping the background, but it also removed the tap gesture for the Disclosure Indicators (swipe still works). Does anyone know how to re-activate the tap gesture for the Disclosure Indicators cells after implementing this hideKeyboard() function?
Keep a reference to the gesture recognizer and remove it from the view when you hide the keyboard.
class YourController: UITableViewController {
let tapGesture: UITapGestureRecognizer = {
let tg = UITapGestureRecognizer(target: self, action: "hideKeyboard")
tg.cancelsTouchesInView = true
return tg
}()
override func viewDidLoad() {
super.viewDidLoad()
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
tableView.removeGestureRecognizer(tapGesture)
}
}

How do I implement a UIGestureRecognizer in my app in Swift?

Lets say I have an image of a tree and when I tap on the screen it changes to an image of a car how would I do this? This is the code I have so far but its not working. (I have three images named "gp1", "gp2", and "gp3" and all of them have different number of taps to change the image.)
I'm in Spritekit and using Swift.
class GameScene: SKScene, SKPhysicsContactDelegate {
//Create Touch
createTouch()
}
override func didMoveToView(view: SKView) {
func createTouch() {
gestureRecognizer.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
gestureRecognizer == UITapGestureRecognizer(target: self, action: "gp2")
gestureRecognizer2 == UITapGestureRecognizer(target: self, action: "gp3")
gestureRecognizer3 == UITapGestureRecognizer(target: self, action: "gp1")
}
}
As Mundi said, for each UIImageView, make sure userInteractionEnabled = true. Otherwise, users won't be able to tap the image. Then, create the gestureRecognizer. Set the action to the method that will handle the gesture. Next, add the gestureRecognizer to the UIImageView. Finally, write the method to handle the gesture.
#IBOutlet var gp1: UIImageView!
#IBOutlet var gp2: UIImageView!
#IBOutlet var gp3: UIImageView!
func createTouch() {
// maker sure userInteractionEnabled is true
gp1.userInteractionEnabled = true;
gp2.userInteractionEnabled = true;
gp3.userInteractionEnabled = true;
// create the gesture recognizers
let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer3 = UITapGestureRecognizer(target: self, action: "tapGesture:")
// set the number of taps
gestureRecognizer1.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
// add the gesture recognizer to the UIImageViews
gp1.addGestureRecognizer(gestureRecognizer1)
gp2.addGestureRecognizer(gestureRecognizer2)
gp3.addGestureRecognizer(gestureRecognizer3)
}
Since these are all tap gestures, I've decided to use one method and a switch statement. You could, however, create different methods for each gesture if you wanted to.
func tapGesture(gesture: UITapGestureRecognizer) {
switch gesture.numberOfTapsRequired {
case 1:
gp1.image = UIImage(named: "car")
case 2:
gp2.image = UIImage(named: "car")
case 3:
gp3.image = UIImage(named: "car")
default:
break
}
}
You need to add the recognizer to the view and implement the action handlers gp2 etc.
For UIImageView you should also make sure userInteractionEnabled is set to true.