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")
}
Related
I am trying to catch mouse down events on some of my controls in the (Cocoa with Storyboards) application window.
If I override mouseDown(with event: NSEvent) in my ViewController class, I face two issues:
It is not possible to identify (directly) which exactly control in the window has been clicked. To do so, I use the following:
Swift 4:
override func mouseDown(with event: NSEvent)
{
let point: NSPoint = event.locationInWindow
let view: NSView = self.view.hitTest(point)!
if type(of:view) == NSTextField.self // with tags is also fine
{
// do something with the control (in the example NSTextField)
// (view as! NSTextField).backgroundColor = NSColor.systemPink
}
}
I am a little bit puzzled, why for such a basic GUI operation, there isn't a "native" event handler, as provided for the mouse click (by creating an #IBAction).
Am I missing something, or this is the way to catch and handle the mouse down events?
For some controls, e.g. NSLevelIndicator, my overridden method is not called. Why?
You must override NSView mouseDown, not NSViewController mouseDown.
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)
}
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.
I'm making a little game and I have come across a problem that I haven't been able to solve. The thing is that when I touch the screen an action occours. Well, I want that if there are 2 differents touches the action to happen 2 times, not only on the first one. Right now, if there are 2 players on the same device, the one who touches first is the one who wins, because the second player isn't able to even call the action. How can I invoke the action two times treating every touch recived as input?
In other words, I want to detect when there are two fingers on the screen and split every "finger" in the normal action for one finger.
Of course, my actions takes place here:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//code to get input info
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//calling actions with the arguments of touchesEnded and with the ones of touchesBegan
}
In your storyboard, you need to enable "Multiple Touch" for your view (it's in the Interaction section of the Attributes Inspector.) You can also set this property programmatically:
view.multipleTouchEnabled = true
However, if multiple touches happen at the same time, they will be passed in a single touchesBegan event. To make sure your app detects both touches, iterate over all of the touches in the set in touchesBegan:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
//code to get input info
}
}
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
}