Coordinates of Buttons in Swift - ios

i'm fairly new to swift and programming and have a very simple question.
I have four buttons with custom images in the UI and i'm trying to put a surrounding Graphic behind the pressed Button. Here's the line of code:
#IBAction func moodButton(sender: AnyObject) {
timerSelection.center = CGPointMake(moodButton.center.x, moodButton.center.y)
}
timerSelection is the UIImageView i'm trying to put to the coordinates of the selected button...
Please Help! & Thanks in advance :)

Try something like this:
#IBAction func moodButton(sender: UIButton) {
timerSelection.center = sender.center
}
You can send this subview to back before:
view.sendSubviewToBack(timerSelection)

Related

append button title to array?

I have an empty array, and I have #IBAction func test(_ sender: UIButton) with 25 different buttons attached to this func. how can I write the code for appending a button title to the array, when pressed?
someArray.append = (this part that I can't figure out?)
Since I don't know which button/buttons will be pressed I can't just write someArray.append = ("buttonTitle")
(ive googled and searched in here for hours, but I can't find the solution)
if let title = sender.title(for: sender.state) {
someArray.append(title)
}
For multiple UIButtons with single IBOutlet Action you can add tags for each UIButton.
So if you've used StoryBoard for UIButtons, so when you click on single UIButton - navigate to Attribute Inspector & add tag to each button. As shown in below fig.1.0
I've set all UIButtons with different title & tags.
Once you set tags, for all buttons, then programmatically you can identify which button is clicked via single - onClick function.
#IBAction func Click(_ sender: UIButton) {
if let title = sender.title(for: sender.state){
print(title)
//Here we are identifying which button is pressed. If I pressed UIButton.tag = 1 then I'm just printing it's title, else appending UIButton value in array.
if sender.tag == 1 {
print(sender.title(for: sender.state))
}
else{
self.someArray.append(title)
}
}
}
So, in this way you can identify all your UIButton actions within a single event action.
#IBOutlet weak var btnoutlet: UIButton!
if let title = btnoutlet.currentTitle {
someArray.append(title)
}

Reference sender in UITapGestureRecognizer action

To set the stage, I have a 4x5 grid of UIImageViews that I would like to flip and show a new image upon tap. I'd like to accomplish this with a single #IBaction. I'm simply having a bit of trouble referencing the selected UIIMageView when a tap is recognized. I'm sure it's something very simple. I'm just now starting to work with UITapGestureRecognizer, so I don't know all the ins and outs just yet. Here's the #IBAction I'm trying to use:
#IBAction func tileTapped(recognizer: UITapGestureRecognizer, _ sender: UIImageView) {
print("Tile Tapped: \(sender.tag)")
}
My print statement is giving me the following no matter what UIImageView is tapped:
Tile Tapped: 0
My tags are set up to reference the row and column they fall in. For example, the tags for my first row are:
00, 01, 02, 03
My biggest challenge is simply retrieving the tag for the appropriate UIIMageView. Once I figure that out, I should be solid.
Thanks in advance!
That is not a valid function for a gesture recognizer. See the documentation for UIGestureRecognizer on the possible signatures.
To access the view associated with the gesture, access the gesture's view property.
#IBAction func tileTapped(_ recognizer: UITapGestureRecognizer) {
if let view = recognizer.view as? UIImageView {
print("Tile Tapped: \(view.tag)")
}
}

Creating duplicate outlets/actions on Xcode

I currently have three separate sliders on my viewcontroller.swift that I would like to all perform the same function, which is to round the slider value to the nearest position. I have already completed the first one, as such:
#IBAction func changePos(_ sender: UISlider) {
slider.value = roundf(slider.value)
}
I have tried many different ways to add the same function to different UISliders by using slider2, changePos2, etc, but my app either crashes or doesn't perform the function.
How do I make separate UISliders perform the same function within the same viewcontroller?
You can hook all 3 sliders up to the same #IBAction. Just use the sender parameter instead of your slider output in the implementation.
#IBAction func changePos(_ sender: UISlider) {
sender.value = sender.value.rounded()
}
The good way to do this is using IBOutletCollections like below.
#IBOutlet var sliders: [UISlider]!
and then using nice swift syntax below:
sliders.forEach { (slider) in
slider.addTarget(self, action: #selector(sliderValueDidChange(sender:)), for: .valueChanged)
}
This way you will have same selector get called for each of the sliders.

Customize a button and its highlighted state in Swift3 using a class

I'm new to Swift and I assume this is a fundamental question to programming for iOS.
I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.
I also have three themes (pink, blue and orange). What I thought of doing is to create three new classes called pink,blue and orange.swift
I don't want to create them programmatically, only style them programmatically.
What I lack to understand is how do I call the function (Example: "ButtonIsPressed") from my pink.swift class into my #IBAction and #IBOutlet in the main view controller that is also object oriented (ie. I don't want to create a function for every button)?
I can't really find a decent and up-to-date Swift 3 Tutorial for this, any help or advice on this topic will be greatly appreciated.
Why can it not be as simple as?:
#IBAction func buttonPressed(_ sender: UIButton!) {
self.backgroundColor = myPinkCGolor
}
I think shallowThought's answer will work for changing backgroundColor based on button state of a specifically named IBOutlet.
I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.
If you want to maintain "state", as in have a "counter" for how many times a button's been clicked or tapped, you can use the "tag" property of the button. Set it to zero, and in your IBAction functions increment it. (Like shallowThought said, use .touchUpInside and .touchDown for the events.)
Also, you have one minor - but important! - thing wrong in your code Brewski:
#IBAction func buttonPressed(_ sender: UIButton!) {
self.backgroundColor = myPinkCGolor
}
Should be:
#IBAction func buttonPressed(_ sender: UIButton!) {
sender.backgroundColor = myPinkCGolor
}
So combining everything - up vote to shallowThought (also, changing his AnyObject to UIButton and making it Swift 3.x syntax on the UIColors - and would end up with this. Note that there is no need for an IBOutlet, and you can wire everything up in IB without subclassing:
// .touchUpInside event
// can be adapted to show different color if you want, but is coded to always show white color
#IBAction func buttonClicked(sender: UIButton) {
sender.backgroundColor = UIColor.whiteColor()
}
// .touchDown event
// will show a different color based on tap counter
#IBAction func buttonReleased(sender: UIButton) {
switch sender.tag {
case 1:
sender.backgroundColor = UIColor.blue
case 2:
sender.backgroundColor = UIColor.red
case 3:
sender.backgroundColor = UIColor.green
default:
sender.backgroundColor = UIColor.yellow
}
sender.tag += 1
}
There is no methode to set the backgroundColor for a certain state, like there is for other UIButton properties, so you have to listen to the buttons actions:
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
#IBAction func buttonClicked(sender: AnyObject) { //Touch Up Inside action
button.backgroundColor = UIColor.whiteColor()
}
#IBAction func buttonReleased(sender: AnyObject) { //Touch Down action
button.backgroundColor = UIColor.blueColor()
}
...
}
or set a unicolor image withimage:UIImage, forState:.selected.

Animation on button image when tapped

I'm new to Swift and to stackoverflow so please comment for any improvements. I can make to my asking of questions!
I have created a UIButton that has an image. When tapped, the image is supposed to change to another image. Currently, once tapped, it 'flashes' with the image it is supposed to change to and then immediately changes back to the original image.
I created an outlet:
#IBOutlet var buttonImage: UIButton!
Then I added this:
override func viewDidLoad() {
super.viewDidLoad()
//Change image.
buttonImage.setImage(UIImage(named:"image1.png"),forState:UIControlState.Normal)
buttonImage.setImage(UIImage(named:"image2.png"),forState:UIControlState.Highlighted)
}
Help is gratefully received.
EDIT - added error screenshot.errorscreenshot
You need to change your viewDidLoad code like this
buttonImage.setImage(UIImage(named:"image1.png"),forState:UIControlState.Normal)
buttonImage.setImage(UIImage(named:"image2.png"),forState:UIControlState.Selected)
Now change you IBAction like this
#IBAction func btnTap(sender: UIButton) {
sender.selected = !sender.selected
}
Hope this will help you.
You need to assign two images for the button like the screenshots below.
As you can see I have state config as Default and Selected.
Later on the action of button you can do
#IBAction btnTapAction(sender: UIButton) {
if sender.selected{
sender.selected = false
}
else{
sender.selected = true
}
}

Resources