On UIButton action the Taptic of device is not working - ios

There is a taptic which on long press user get vibrate felling with device for a second.
My device is iPhone 6s version 12.6.1 hope it support the taptic.
#IBAction func onClick(_ sender: Any) {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.prepare()
generator.impactOccurred()
// Do Something
}
The above code is not working!
On Button click I want to use the taptic to make user user tap the button! Same thing I would like to use for picker view and other actions where user validate there inputs.
Let me understand is taptic is same small mechanical movementum?

This is a list of all the possible types of Haptic Feedback you can use. All of these should work. I don't see anything necessarily wrong with your code might be a connection issue as well. Make sure the button is not disabled, its connected and or not behind something.
Also yes the 6s does support Haptic feedback.
#IBAction func errorButtonTapped(_ sender: UIButton) {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
}
#IBAction func successButtonTapped(_ sender: UIButton) {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}
#IBAction func warningButtonTapped(_ sender: UIButton) {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
}
#IBAction func lightButtonTapped(_ sender: UIButton) {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.prepare()
generator.impactOccurred()
}
#IBAction func mediumButtonTapped(_ sender: UIButton) {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.prepare()
generator.impactOccurred()
}
#IBAction func heavyButtonTapped(_ sender: UIButton) {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.prepare()
generator.impactOccurred()
}

Make sure this is enabled in settings. If not,
Turn on System Haptic
Go to Settings.
Tap on Sounds & Haptics.
Scroll all the way down to System Haptics.
Toggle this option on.

Related

Why UIButton requires two clicks to change its image

I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:
#IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}
However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.
My question is why the image was not changed in button2 after the first click?
What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?
You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the #IBAction. Let's say:
#IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}
Try this:
override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}
#IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}
And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.
Rename your method/action so it differs from the button/property.
Change Any to UIButton since you know its class.
#IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}
Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.
it's simulator bug. it worked on a real device
#IBAction func button2(_ sender: UIButton) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}

Volume Control With UISlider Swift3

I'm creating a music app and I'm using an UISlider for the music volume control. the problem is when I press the volume button on the iPhone / iPad UISlider does not change or move. I've been looking for where to get no referrals.
This is the code I use:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.slider.setValue(AVAudioSession.sharedInstance().outputVolume, animated: true)
}
}
#IBAction func volumeControlSlider(_ sender: UISlider) {
slider = (MPVolumeView().subviews.filter { NSStringFromClass($0.classForCoder) == "MPVolumeSlider" }.first as! UISlider)
slider.setValue(sender.value, animated: false)
}

Button for copying from Label

I have code to copy the numbers from the Label.
How it's called?
How can I make that when I click on a button, it appears that on the photo
#IBAction func copybutton(_ sender: UIButton) {
UIPasteboard.general.string = displayResultLabel.text
}
Use this code -
UIPasteboard.general.string = sender.title(for: UIControlState.normal)

Swift: How to toggle two UI Switches

I have two UI Switches
When I tap one, I want the other to turn off and vice versa
Below I have two switch actions.
I've tried to check if switch one is on (which is by default) and if it is when tapped, then turn it off, but if it isn't on, then turn it on.
#IBAction func switchOnePressed(_ sender: UISwitch) {
if switchOne.isOn {
label.text = "UISwitch is ON"
switchOne.setOn(false, animated: true)
} else {
label.text = "UISwitch is OFF"
switchOne.setOn(true, animated: true)
}
}
#IBAction func switchTwoPressed(_ sender: UISwitch) {}
Any help would be great!
This simplifies things a lot by reducing the code required. The opposite switch is then set based off the current value of the calling switch.
#IBAction func switchOnePressed(_ sender: UISwitch) {
label.text = sender.isOn ? "UISwitch is ON" : "UISwitch is OFF"
switchTwo.setOn(!sender.isOn, animated: true)
}
#IBAction func switchTwoPressed(_ sender: UISwitch) {
label.text = sender.isOn ? "UISwitch is ON" : "UISwitch is OFF"
switchOne.setOn(!sender.isOn, animated: true)
}
You do not need to set the value for the calling switch since that should already be handled by the interface interaction.
Also, I don't know what your intention is with the label, but it would be misleading in its current form since it does not indicate what switch is on & off. Perhaps you have unique labels for each one, but that is just speculation on my part.

How to get the current Title of a button in Swift 3.0 , ios using sender.titleForState(.Normal)!

I tried to get the title of a button in swift like below.
#IBAction func buttonAction(_ sender: Any) {
let buttonTitle = sender.titleForState(.Normal)!
}
but it didn't work,even it doesn't give any hint when we press . after the sender.
so what is the correct way of doing this in swift 3.0
Or else if we create an IBOutlet and then we use its currentTitle, it works fine like below. Why we cannot get it with sender. for above
#IBOutlet var thebutton: UIButton!
#IBAction func buttonAction(_ sender: Any) {
let buttonTitle = thebutton.currentTitle!
print(buttonTitle)
}
Because parameter sender is in type Any instead of UIButton. Change the method signature to:
#IBAction func buttonAction(_ sender: UIButton) {
if let buttonTitle = sender.title(for: .normal) {
print(buttonTitle)
}
}
and you should be good to go.
Smartest version using Swift 5.0:
#IBAction func buttonAction(_ sender: UIButton) {
print(sender.currentTitle)
}
To get the title of the button regardless of its current state in swift 3.0 try using this:
#IBAction func buttonPressed(_ sender:UIButton){
let buttonTitle = sender.titleLabel?.text
print("\(String(describing: buttonTitle)")
}
This will return the title for the state, based on the state that the button is current in.
In order to resolve the warning, following code may be used.
#IBAction func buttonAction(_ sender: UIButton) {
let buttonTitle = sender.title(for: .normal) ?? String()
print(buttonTitle)
}
Using Swift version 5.5.2, the best way I found out to print a label's title is:
#IBAction func keyPressed(_ sender: UIButton) {
print(sender.title(for: .normal)!)
}
I was receiving a warning while using sender.title(for: .normal). The exclamation mark (!) solved it.
#IBAction func buttonPressed(_ sender: AnyObject) {
let title = sender.title(for: .normal)
print("\(title) button pressed")
}
So this is for swift 4.0, not the last one.
If your button has a regular title you can use:
button.title(for: state) // probably UIControl.State.normal
If your UIButton has an attributed title you'll need to use:
self.attributedTitle(for: state)
Or you can use a more general extension:
extension UIButton {
func titleForState(_ state: UIControl.State = .normal) -> String? {
if let title = self.title(for: state) {
return title
}
if let attribTitle = self.attributedTitle(for: state) {
return attribTitle.string
}
return nil
}
}
I used switch statment:
var buttonConter = 1
#IBAction func hardlessSelected(_ sender: UIButton) {
switch buttonConter {
case 1:
buttonConter += 1
print("Soft")
case 2:
buttonConter += 1
print("Medium")
case 3:
buttonConter += 1
print("Hard")
default:
print("Non")
}
}
}
The simple way just to use currentTitle:
#IBAction func hardlessSelected(_ sender: UIButton) {
print(sender.currentTitle)
}
Instead of using the currentTitle of the UIButton to get the name, you can use titleLabel.
The easy way to get this done is to unwrap the titleLabel then unwrap the text:
#IBAction func buttonAction(_ sender: UIButton) {
var buttonTitle = sender.titleLabel!.text!
print(buttonTitle)
}
Since Swift 3.0, it changed from
sender.titleForState(.Normal)! >> sender.title(for : .normal)!
Change Any to AnyObject.
#IBAction func buttonPressed(_ sender: AnyObject) {
let title = sender.title(for: .normal)!
print("\(title) button pressed")
}
#IBAction func btnNextTapped(_ sender: Any) {
let title = (sender as AnyObject).title(for: .normal)!
}
Swift4.0

Resources