Set UIView Background Color to UITableViewSelectionStyle Default - swift

I have created a UIView that is not in a UITableView or UITableViewCell. I'd like to be able to highlight this view within the view's touchesBegan function and the backgroundColor of this UIView the same color as this cell. Below is the code that I'm using. Unfortunately, it seems like .lightgray is not the same color used for selectionStyle = .default. Is there another way that I can set this view to the same color used by the UITableViewCells?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.backgroundView.backgroundColor = .lightGray
super.touchesBegan(touches, with: event)
}

Related

Trigger UIPanGestureRecognizer immediately when it's added

Can a UIPanGestureRecognizer be added while the user is touching and immediately start recognizing the pan gesture? I can only get it to work if I lift up my finger, touch again and start dragging.
From another answer, I've tried to subclass UIPanGestureRecognizer and override the touch event:
import UIKit.UIGestureRecognizerSubclass
class InstantPanGestureRecognizer: UIPanGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if (self.state == UIGestureRecognizer.State.began) { return }
super.touchesBegan(touches, with: event)
self.state = UIGestureRecognizer.State.began
}
}
This doesn't solve my problem though, probably because they already had their UIPanGestureRecognizer added, and I want to add mine while the user is already touching the screen and have it work at that point.
I'm switching between adding swipe and pan gestures depending on where the user the is interacting on an image. The require(tofail:UIGestureRecognizer) function hasn't worked for me.

Swift Image Switching for full screen

I am creating an app in Swift and have all of the screen I need already mapped out in Png files. I simply want to import each image and then when the user clicks, just switch the image. I have never used Swift before and know that this is not traditional development, but how would I approach this to begin? Is there a way that I can link the saved data from NS storage to Healthkit?
So basically you want a simple controller that has an image has background and when the user tap on the view it changes the image ?
If this is what you want just link your imageView from the storyboard in your viewController then :
override func viewDidLoad(){
self.yourImageView.image = UIImage(named: "yourfirstimage.png")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.yourImageView.image = UIImage(named: "yournewimage.png")
}

touchesBegan in static tableView not being called

I have read several posts here about same problem, but could not understood them mostly because I am quite new in IOS development and I use swift. Also I couldn't even found a definition for term "subclassing", maybe it is obj-c only?
Anyway, I have a tableview controller with static cells, and a textfield in one cell. I need to dismiss the keyboard when the user taps on a different area while editing the textfield. After reading posts here, I changed my touchesBegan content as following :
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
self.nextResponder()?.touchesBegan(touches, withEvent: event)
super.touchesBegan(touches, withEvent: event)
}
I still don't get the touches in tableviewcontroller which is defined as :
class addNew: UITableViewController, UITextFieldDelegate {
I had a similar issue on Swift 4 just to endEditing and hiding the keyboard with touchesBegan. I couldn't find a way around at first.
These two steps worked for me :
To add gesture recogniser to table view in :
override func viewDidLoad() {
super.viewDidLoad()
tableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideKeyboard)))
}
Ending editing in two ways in
#objc func hideKeyboard() {
view.endEditing(true)
//textField.resignFirstResponder() /* This line also worked fine for me */
}
You can add a tapGestureRecognizer on top of you tableview.
Connect the tapGestureRecgonizer to a function/method.
Within that method, check if the textfield, is the firstresponder. If yes, then ask the textfield to resign first responder.

Fastest way to detect touchesmoved moves over an UIView?

I'm making a customkeyboard, it has almost 30 buttons.
And i'm using multi-touch event to check which button is being selected or touch over.
My way is detect current finger position and compare with button position.
But it seems too slow because i have to use for loop to check every single button coordinates.
Any faster way to check touchesmoved touches over 1 UIView or UIButton?
For your keyboard buttons, you need to override the UIView method touchesBegan and touchesEnded. Touches began will be called when a touch enters the buttons frame, and touches ended will be called when a touch exits the buttons frame. No comparing of coordinates needed.
class MyButton: UIButton {
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
//Button presses
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
//Button unpresses
}
}
Note that there is a method known as touchesMoved which can be overriden in any UIView. This function is called any time touches move. You don't necessarily need to check any time a touch moves within the button, only when a touch begins and exits a button, so you shouldn't use the touchesMoved method in this case.

Long tap recognizer in Sprite Kit with swift

Hello I'm planning a game and an essential part of the game is to move left and right by pressing the right side of the screen or the left side.
But how can I detect a long press?
Many Thanks! :)
It is not called long tap. A tap can't be long. It is called UILongPressGestureRecognizer. You can take a look at the documents here
if you want to do something while the user is holding down on the screen, you could use the touchesBegan and touchesEnded methods to detect the hold.
var touching = false
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = true
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}