Trying to make my image's position change randomly - ios

I am using Xcode to try to change my images position randomly every time a button is pushed but for some reason, the image only moves once in a while when the button is pushed even though I know it received the button's IBAction because the NSLog in the IBAction displayed in the console, I have tried it two ways and both methods only worked sporadically
Method 1:
ranX = arc4random() %320;
ranY = arc4random() %480;
myImage.center = CGPointMake(ranX, ranY);
Method 2:
myImage.center = CGPointMake(arc4random() %320, arc4random() %480);
One more thing: every time it does work, the next IBAction to run puts it right back into its original position.
More information that might be pertinent I don't know, I do not have size classes enabled, it is triggered by touch up inside. I also DID make all the RNGs arc4random_uniform.
The problem is fixed, just deselect auto layout.

What control event did you add to your button? Since you stated it moves every once in a while, you might have done the wrong event. Try UIControlEventTouchUpInside
So,
[myButton addTarget:self action:#selector(moveImage) forControlEvents:UIControlEventTouchUpInside];
You're using a storyboard I see, Make sure that the send event for the button is "Touch Up Inside". Click on the button, go to the Connections Inspector and double check this.

Related

Highlight UIButton when not selecting the button itself

I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.

How do I change an Image when a button is pressed?

Now I know this question may sound like a duplicate, but I doubt it seeing as I've researched and none of the possible duplicates answer my question. If it is a duplicate and it's been answered by all means show me the way!
But my question doesn't deal so much with a button changing an image, more so the button is the image.
So my button is an image and when pressed a sound plays and then the image changes until the sound is done. My problem is I can't get the image to change.
Now I have tried this method thinking that I could press my button and my new image would cover up my button until the sound was done:
UIImage *dolDerp = [UIImage imageNamed:#"dolphin2.png"];
[imageview setImage:dolDerp];
I have an outlet set up and it's connected to an image view object and then the action is connected to the button, so in theory when the button is pressed the new image should take over the screen. Now obviously the code needs to be tweaked so the button would go away after a few seconds when the sound is played, but my problem is I can't even get the button to display. I also would prefer to just have the button objects image change if possible?
If anyone could offer some help it's much appreciated!
the best thing is in Interface Builder assign the two images to the button Normal/Selected states.
in code just use this line:
myButton.selected = YES;
//or
myButton.selected = NO;
What #Mahmoud Fayez suggested is good. In code, the proper way to set a buttons image/background image is [button setImage:image forState:UIControlStateNormal] or the similar method for setBackgroundImage
If you are using IB though, it is indeed best to set the different images for different states in IB, then change the buttons state (which will then change the buttons image) in code.

UIBarButtonItem triggers action only when pressed for long enough

I have a few simple UIBarButtonsItems(no customization other than changing style, color and alpha). Everything was working perfectly fine for some time. But now I need to press BarButton for a long time(4-5 seconds) before the action is triggered. I just moved toolbar around and changed color and alpha after which this started happening. I don't exactly understand what caused this change of behavior.
I used interface builder to build the view and assigned action to UIBarButtonItems by Ctrl+Dragging.
I tried looking up a lot both on google and so. Most of the questions are about UIBarButtons not responding at all. Still I tried the solution of cleaning build of project and Xcode cache. But nothing changed.
Could someone help me with this?
I realized my mistake. I had two events getting triggered in same space in view. First was to reveal toolbar(i.e. increase its alpha) when tapped in particular area of the view and second one was of course button tap event for UIBarButtonItem. When tapped in that area the first event got triggered and eventually when pressed for long it also fired the second event once the toolbar was visible(if toolbar was invisible before).
Sorry to bother you all.

Adding a custom UIButton and action to a view in window app delegate (ios)

I am working on a tab bar application which needs a global title bar with a setttings button attached. I have achieved adding an imageview to the main window, then a label, another image, and finally a button. However, I cannot get this button to fire an action. it is set up like this:
[settingsButton addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchupInside];
Then the action defined like:
-(void)settings{
NSLog(#"please do something");
}
However nothing happens, the image doesn't even change like it is being pressed. Can i not define buttons in the app delegate this way? is it because the target is not something that is control based? i tried different targets but i must not fully understand what setting a target is. thanks for any help.
P.S.- i tried setting up the method like so as well:
-(void) settings:(id)sender{
}
and calling it by replacing the action with #selector(settings:) to no avail. Thank you.
I solved it. I forgot I changed the view behind the button to a UIImageView. I can't believe I didn't think of this before since this same issue cost me 2 hours the other day. When you have an imageView always remember to set this flag if you want stuff on it:
(someImageView).userInteractionEnabled = YES;
Hopefully I save someone an hour or two of needless headaches.
P.S. Thanks for the speedy comment !

Event fired two times when clicked to button in my iOS. app

I made a simple IOS. application where the UI contains buttons. For example I have nine buttons each button represent a number same as numeric keyboard. I made this buttons the following way, put it to the storyboard and made the Touch Up Inside event with ctrl drag functions, after I copied the buttons eight times.
The problem is when I pressed a button the event comes sometimes twice. It happens randomly. I put a break point into line NSLog I did not see faulty thing at all.
- (IBAction)digitPressed:(UIButton *)sender {
if ([lastPressed isEqualToString:[sender currentTitle]]) {
NSLog(#"Douple pressed digit"); // break it here
}
// store to lastPressed
lastPressed = [sender currentTitle];
}
If you get multiple times in digitPressed when clicking once I would guess something went wrong when you copied the buttons so check the wiring in your storyboard.
And as Vince said it's a good practice to use id, it's not because this method gets triggered by buttons now that they should always be triggered by a button.
It's easier to debug when you log every time instead of only doubles.
NSLog(#"Button pressed: %#", [sender currentTitle]);

Resources