I am working on a XCode 4.4 project and I am at a point where I need to conserve some space in my GUI. I have two buttons that each handle a method, one to power on a device and the other to power off the same device.
My question is how can I combine these two buttons into one so when the user presses the power on button it changes to the power off button and so on. I need to be able to change the icon on the button as well to display a change. I would think this is generally pretty simple to achieve. Any advice would be greatly appreciated, thanks in advance.
Well, all you need to do is when the button is pressed, update the button to reflect the current state. So you will change the image on the button and probably the text. Just look at the documentation of NSButton for what you can set.
So you might do something like:
- (IBAction)buttonWasPressed:(id)sender {
if (device.isTurnedOn) {
[device turnOff];
[self.button setTitle:#"Turn On"];
} else {
[device turnOn];
[self.button setTitle:#"Turn Off"];
}
}
You can have a single action like this.
- (IBAction)btnAction:(id)sender {
NSButton *btn=(NSButton *)sender;
[btn setTitle:([btn.title isEqualToString:#"On"] ? #"Off" : #"On")];
if([btn.title isEqualToString:#"On"]){
//Switch ON the device
}else{
//Switch OFF the device
}
}
Related
I would like to make my button pop up when i actually press the button for more than one secs. When it pops up, the letter or word in the button should appear with it... Any guidance on how to do this? Would really appreciate any help provided...
This is easy, What you need to do is implemenent longtap on that Button. UILongPressGestureRecognizer. So what that gesture takes over, you can animate the CGSizeMake to make the frame grow bigger than what it is or you could use scale property to scale the button bigger.
Hope this is clear enough. Let me know if this works out for you...
UPDATE: Some code -
- (void)longPressHandler:(UILongPressGestureRecognizer *)gesture
{
UIButton *btn;
if(gesture.state == UIGestureRecognizerStateBegan)
{
btn = (UIButton *)gesture.view;
[btn setFrame:CGRectMake(btn.frame.origin.x, btn.frame.origin.y, 150, 70)];
}
return;
}
FYI, LongTap gesture has different states UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, UIGestureRecognizerStateEnded. There are some more but these are the main.
I have programmed a Quiz in Xcode where I've created UIButtons programmatically displaying the answer to a question. When the user clicks the button the next question is loaded and the buttons position i shuffled to not make the right answer in the same place for each question (the right answer is however under the same UIButton for every question.)
This is how I create my buttons:
if (self.buttonRightAnswer == nil)
{
self.buttonRightAnswer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.buttonRightAnswer addTarget:self action:#selector(rightAnswer) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.buttonRightAnswer];
}
self.buttonRightAnswer.frame = CGRectMake(20.0, [[randomPositionArray objectAtIndex:0] floatValue], 280.0, 40.0);
[self.buttonRightAnswer setTitle:[[self.quiz.arrayWithQuestions objectAtIndex:questionNr - 1]valueForKey:#"RightAnswer"] forState:UIControlStateNormal];
When the user clicks the right an answer the next question loads. Here it's a problem. The next question loads with the same method only shuffled positions and the animation of the press-down of the button is drawn after the buttons been moved resulting in the user seeing where the right answer is now.
How can I wait for the animation before moving the buttons?
I could solve this by having the buttons stay in one place, shuffling out the questions over the buttons and comparing the users answer with the right answer.. but this requires a hell of a lot more code.
You can use some of shit-code and insert some kind of 'sleep' before next shuffling buttons
Before you create the buttons againg (after a question was answered), you can add something like (if you use differen images for button states)
-(void)waitUntilButtonDeselected
{
bool selected = YES;
while (selected)
{
bool selected = (self.buttonRightAnswer.currentBackgroundImage == UIImageOfNotSelectedButton);
}
}
The idea here is to check if the image of the button corresponds to not selected button.
Typically when using a UIButton, events are triggered on UIControlEventTouchUpInside, or at the end of the touch.
You're triggering it on the touch down which is causing issues with the animation you want to happen.
It might be simpler for you to switch to using UIControlEventTouchUpInside instead of UIControlEventTouchDown since this should solve your problem and be more consistent with other iPhone apps.
so here is the deal:
I have a label (NSTextField) that I want to activate after I click a button. This label will appear while the program is loading some wavs (since it usually makes a minor delay when it does). I then want it gone once this has happened (and the new View appears).
Now, the problem I have is that this update does not seem to happen when I tried this. If I don't make it disappear at the end then I can see it, but only after the delay has occured (rendering it pointless).
Currently I am using:
[label2 setHidden:NO];
I understand that this will occur once the method I called it in has finished (which is a problem). Any idea what I could do instead so that the label is shown while the program is loading wavs?
Thanks heaps!!
Ok, I guess I solved it myself - I hope this helps people.
So when I click the button I disable the buttons and replace the label temporarily. This, however, only happens in the next view (so I'm not sure how to make it occur in the same view).
I disable the buttons for about 1 second, and it is here that the label is shown.
Here's some code to show what I mean:
- (IBAction)clickedTheButton:(id)sender {
[button setEnabled:NO];
[label2 setHidden:NO];
...
//Changes the View
[self nextMethod];
}
The View has now changed, and this method is called next. This enables me to see the label.
-(void)nextMethod{
...
[self performSelector:#selector(delayedDisplay:)
withObject:#"Hi"
afterDelay:1.0]; //delay for 1 second
}
This method then puts them back to their original state (so the label is hidden and the button is activated again)
-(void) delayedDisplay:(NSString *)string{
[button setEnabled:YES];
[label2 setHidden:YES];
}
I am trying to add a UIButton to the view of a MPMoviePlayerController along with the standard controls. The button appears over the video and works as expected receiving touch events, but I would like to have it fade in and out with the standard controls in response to user touches.
I know I could accomplish this by rolling my own custom player controls, but it seems silly since I am just trying to add one button.
EDIT
If you recursively traverse the view hierarchy of the MPMoviePlayerController's view eventually you will come to a view class called MPInlineVideoOverlay. You can add any additional controls easily to this view to achieve the auto fade in/out behavior.
There are a few gotchas though, it can sometimes take awhile (up to a second in my experience) after you have created the MPMoviePlayerController and added it to a view before it has initialized fully and created it's MPInlineVideoOverlay layer. Because of this I had to create an instance variable called controlView in the code below because sometimes it doesn't exist when this code runs. This is why I have the last bit of code where the function calls itself again in 0.1 seconds if it isn't found. I couldn't notice any delay in the button appearing on my interface despite this delay.
-(void)setupAdditionalControls {
//Call after you have initialized your MPMoviePlayerController (probably viewDidLoad)
controlView = nil;
[self recursiveViewTraversal:movie.view counter:0];
//check to see if we found it, if we didn't we need to do it again in 0.1 seconds
if(controlView) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[controlView addSubview:backButton];
} else {
[self performSelector:#selector(setupAdditionalControls) withObject:nil afterDelay:0.1];
}
}
-(void)recursiveViewTraversal:(UIView*)view counter:(int)counter {
NSLog(#"Depth %d - %#", counter, view); //For debug
if([view isKindOfClass:NSClassFromString(#"MPInlineVideoOverlay")]) {
//Add any additional controls you want to have fade with the standard controls here
controlView = view;
} else {
for(UIView *child in [view subviews]) {
[self recursiveViewTraversal:child counter:counter+1];
}
}
}
It isn't the best solution, but I am posting it in case someone else is trying to do the same thing. If Apple was to change the view structure or class names internal to the control overlay it would break. I am also assuming you aren't playing the video full screen (although you can play it fullscreen with embeded controls). I also had to disable the fullscreen button using the technique described here because the MPInlineVideoOverlay view gets removed and released when it is pressed: iPad MPMoviePlayerController - Disable Fullscreen
Calling setupAdditionalControls when you receive the fullscreen notifications described above will re-add your additional controls to the UI.
Would love a more elegant solution if anyone can suggest something other than this hackery I have come up with.
My solution to the same problem was:
Add the button as a child of the MPMoviePlayerController's view;
fade the button in and out using animation of its alpha property, with the proper durations;
handle the player controller's touchesBegan, and use that to toggle the button's visibility (using its alpha);
use a timer to determine when to hide the button again.
By trial-and-error, I determined that the durations that matched the (current) iOS ones are:
fade in: 0.1s
fade out: 0.2s
duration on screen: 5.0s (extend that each time the view is touched)
Of course this is still fragile; if the built-in delays change, mine will look wrong, but the code will still run.
I have some key events, that correspond to buttons also. What property/method to I have to set/call for a button to look depressed (change state?) for say half a second?
The way I solved this is I set the NSButton to a type of 'Push On Push Off' and then used the following code in my key event handler:
NSButton *button = [self.superview viewWithTag:event.keyCode];
if (button != nil && button.state == NSOffState) {
[button performClick:event];
[button performSelector:#selector(performClick:) withObject:event afterDelay:0.5];
}
This will highlight the button as if the user had clicked on it, and then it will click on it again in half a second.
I believe the button cell's -setHighlighted: method controls whether the button looks pressed or not. You may also need to call -setNeedsDisplay: on the button after changing it, and it's possible that the button will change its cell's highlighted state by itself, so I'm afraid you may need to fiddle around to get this working.
(I have to admit, though, that I'm not entirely certain about any of this.)