forcing a button to swap the images - objective-c

I'm working on a matching program for iPad and when a user selects a button an image is "uncovered" and then when the user selects a second button,another image is uncovered. I then programmatically check for a match and if not, revert both button images back to their initial state.
This is working fine except when a match is NOT made, the switch happens so fast that you do not have time to see what you have "uncovered". I tried to make it sleep but the image doesn't ever swap to the uncovered state... Thoughts?
The code for this is as follows:
//Take action on the tap of one of the buttons
if(isFirstSelection)
{
firstSelection = [(UIButton *)sender tag];
tempImageItem = [tileArray objectAtIndex:firstSelection];
tempImage = [tempImageItem tileImage];
firstSelectionName = [[NSString alloc] initWithString:[tempImageItem tileName]];
[(UIButton *)sender setImage:tempImage forState:UIControlStateNormal];
tempButton = sender;
isFirstSelection = NO;
}else{
secondSelection = [(UIButton *)sender tag];
tempImageItem = [tileArray objectAtIndex:secondSelection];
tempImage = [tempImageItem tileImage];
secondSelectionName = [[NSString alloc] initWithString:[tempImageItem tileName]];
[(UIButton *)sender setImage:tempImage forState:UIControlStateNormal];
//Two game pieces have been removed so check to see if they are a match
if([firstSelectionName isEqualToString:secondSelectionName])
{
//Match found
//do something
}else{
**//NO MATCH FOUND
[NSThread sleepForTimeInterval:3];
//Display the checker board pieces again
[(UIButton *)sender setImage:[UIImage imageNamed:#"originalImage"] forState:UIControlStateNormal];**
}
//Reset isFirstSelection Flag to YES for next selection
isFirstSelection = YES;
}

From what i understand you want to put the button's original image after 3 seconds so the user has the time to see what happened.
You should look at the NSTimer class to trigger that code
[(UIButton *)sender setImage:[UIImage imageNamed:#"originalImage"] forState:UIControlStateNormal];
in 3 seconds from now.

For "run this code in X seconds" I prefer:
[self performSelector:#selector(spinWheel:) withObject:[NSNumber numberWithUnsignedInt:0] afterDelay:delay];
no need to muck around with timer objects.

Related

Show touch when clicked UIButton Objective-C

I have a UIButton that has only an UIImage set, when Clicked (long press) the images changes automatically with a shadow, What I want is that even if the button gets clicked but not with a long press the images should have the shadow. I already tried from IB to set the UIButton properties Reverse on Highlight and Shows Touch On Highlight but with no results
This is about UIControl Class. It's default to be with shadow. So if you wanna customize it, you should addTarget to your button to every action on UIButton like tapping,long press,touchUpInside and I think it s not about long press it's just you can't realize the difference on tapping.
in one of my project I customize my button actions like this:
buyButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*3/4-2, 0, self.frame.size.width/4+4, self.frame.size.height)];
[buyButton addTarget:self action:#selector(buyButtonClicked:) forControlEvents:UIControlEventTouchDown];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpInside];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpOutside];
and methods of this like:
- (void) buyButtonClicked:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fede32"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fdca2e"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"cfd3dc"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"c0c5d0"];
}
}
- (void) buyButtonNormal:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fdca2e"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fede32"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"c0c5d0"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"cfd3dc"];
}
[delegate purchaseOffer:selectedOffer];
}
If you need more information about UIButton actions there are a lot of question on this site.

Repeating UIButton animation and change background

Can someone please tell me why this doesn't work?:
//Changing the Images
- (void) squareOneColour {
NSUInteger r = arc4random_uniform(5);
[self.squareOne setBackgroundImage:[self.colorArray objectAtIndex:r] forState:UIControlStateNormal];
}
//Moving buttons
- (void) squareOneMover {
NSUInteger r = arc4random_uniform(3);
[self.squareOne setHidden:NO];
CGPoint originalPosition = self.squareOne.center;
originalPosition.y = -55;
[self.squareOne setCenter:originalPosition];
CGPoint position = self.squareOne.center;
position.y += 760;
[UIView animateWithDuration:r + 3
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.squareOne setCenter:position];
}
completion:^(BOOL complete) {
if (complete) {
[self squareOneColour];
[self squareOneMover];
}
}
];
}
I am trying to get a UIButton to animate down the screen and when the animation is finished, for it to repeat but with a different background. The code above seems it should work but the second animation is never completed. (The UIButton is animated once, then everything stops). I have tried putting the code shown below (into the completion block), with an outside NSUInteger method creating a random number for r but it never works. But it does work when I replace r with a different number, like 1 or 2.
I found the answer. After playing around with it, I found that the function I wanted the UIButton to do was only fit for a UIImageView. I guess I'll have to make an invisible button over the UIImageView
You're stepping on your own feet. Step out to the next cycle of the runloop and all will be well:
[self squareOneColour];
dispatch_async(dispatch_get_main_queue(), ^{
[self squareOneMover];
});

Cycling through Image Array within Object in Time Intervals (Objective-C)

My general question is how do you have an object with an image array:
#import <Foundation/Foundation.h>
#interface Object: NSObject {
UIImage *image[imageNumber];
}
And then take these images and apply them to a button in a different class, starting with imageArray[0] and every few seconds changing it to imageArray[1], etc, but stopping the loop at the last image.
What I specifically did was create a Plant object:
#import <Foundation/Foundation.h>
#interface Plant : NSObject {
UIImage *plantImage[3];
int imageNumber; //to specify which image to display, increments with timer
NSTimer *plantImageTimer;
}
and declared then made a few methods like
-(void)addPlantImages:(UIImage *) firstImage withPlantImage2:(UIImage *) secondImage withPlantImage3:(UIImage *) thirdImage{
plantImage[0] = firstImage;
plantImage[1] = secondImage;
plantImage[2] = thirdImage;
}
//this makes the Plant into a button
-(void)createPlantButtonForPlant:(Plant *) plantType forView:(UIView *) view withButtonRect:(CGRect) buttonRect{
imageNumber=0;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = buttonRect;
button.hidden = NO;
[view addSubview:button];
[plantType growPlant:button]; //explained in code below
}
And from here I don't know what to do anymore.
I tried using an NSTimer, but you can't put parameters into the selector, so I can't specify which button (or Plant) I want to cycle images through.
Extra tidbits of code in Plant that I have (which do not work); I'm using NSTimer userinfo incorrectly, but I don't understand how to use it, to be honest.
-(void)growPlant:(UIButton *) button{
plantImageTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(changePlantImage:) userInfo:button repeats:YES];
}
-(void)changePlantImage: (UIButton *) plantButton{
if(imageNumber == 3){
[plantImageTimer invalidate];
} else {
[plantButton setImage:plantImage[imageNumber] forState:UIControlStateNormal];
imageNumber++;
}
}
I imported Plant into my view controller (called PracticeViewController) and tried to make it so that when I clicked a button, a Plant button popped up and cycled through the images.
(void)buttonAction{
theButton.hidden = YES; //I did make this button, just didn't add the code here. Declared in header.
CGRect imageViewRect = CGRectMake(100, 100, 100, 100);
Plant *testPlant = [Plant new];
[testPlant addPlantImages:[UIImage imageNamed:#"plant2.png"] withPlantImage2:[UIImage imageNamed:#"plant2.png"] withPlantImage3:[UIImage imageNamed:#"plant2.png"];
[testPlant createButtonForPlant:testPlant forView:self.view withButtonRect:imageViewRect];
}
If you need more code or other information, just comment below and I will reply asap.
Now that I've done more programming, I realize how complicated I made this code lol. Anyway, all I would have had to do was add
[button addTarget:self action:#selector(changePlantImage) forControlEvents:UIControlEventTouchUpInside];
after making the button instance. This would then scroll through whichever images the plant had in its array.

How to use UIActivityIndicatorView on custom UIButton?

I want to use UIActivityIndicatorView on my custom UIButton.
Here is my code:
if (sender.tag == 1)
{
// Start animating
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
// Check if the network is available
if ([self reachable]) {
// Stop animating
activityIndicator.hidden = YES;
[activityIndicator stopAnimating];
}
}
What I want to do here is:
Once the user touch the button, I want to start the ActivityIndicatior while Reachable checking the network availability. Once it done pass it to the next view.
Update
UIActivityIndicator is on top of my custom UIButton. It build successfully, but ActivityIndicator is not showing when I touch the button.
I'm sure you had your answer since then.
After 4 years, language has changed to Swift, but I had the same problem : UIActivityIndicatorView is not showing when I add it through :
self.myButton.addSubview(self.myIndicatorView)
I had to climb a level up :
self.myButton.superview!.addSubview(self.myIndicatorView)
Assuming everything else is correct in your project, the problem here is that you're showing and hiding an activity indicator in the same event loop, without giving a pause to draw it. Let me explain:
If you have the code:
UIView* view = someView;
view.backgroundColor = [UIColor redColor];
// various synchronous operations
view.backgroundColor = [UIColor yellowColor];
The view will only ever have a background color of yellow.
To answer your question, you probably want to animate the spinner, do some asynchronous operation, then stop the animation. The key being to not stall on the main event loop while your asynchronous task is running. If your comfortable with blocks it would look something like this:
if (sender.tag == 1) {
// Start animating
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
// Check if the network is available
[self checkReachableWithCallback:^{
// Stop animating
activityIndicator.hidden = YES;
[activityIndicator stopAnimating];
}];
}

How to correctly display a "progress" sheet modally while using Grand Central Dispatch to process something?

I'm trying to display a sheet on a window containing a single progress bar, to show the progress of some long function running asynchronously using Grand Central Dispatch. I've almost got it, but can't get the sheet to appear to be in focus, probably because I haven't used runModalForWindow: or similar.
This is approximately what I'm doing at the moment, it happens as a result of a button press on the main window:
// Prepare sheet and show it...
[NSApp beginSheet:progressSheet modalForWindow:window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
[progressSheet makeKeyAndOrderFront:self];
[progressBar setIndeterminate:NO];
[progressBar setDoubleValue:0.f];
[progressBar startAnimation:self];
// Start computation using GCD...
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 1000; i ++) {
// Do some large computation here
// ...
// Update the progress bar which is in the sheet:
dispatch_async(dispatch_get_main_queue(), ^{
[progressBar setDoubleValue:(double)i];
});
}
// Calculation finished, remove sheet on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[progressBar setIndeterminate:YES];
[NSApp endSheet:progressSheet];
[progressSheet orderOut:self];
});
});
This works, except the main window is still in focus, the sheet is out of focus, and the progress bar doesn't animate (unless I use setUsesThreadedAnimation:YES on it).
The problem I think I'm having is that I'm not sure how to run the sheet modally without blocking the main thread before I start the asynchronous computation?
As stated by Brad, it should work.
To do a quick test, I created a sheet programmatically (normally, you would probably use a nib file, but they are hard to paste into this text). If I call the code below from a button in a normal Cocoa window, it works as expected. Notice that the text field on the sheet is first responder, and if you type on the keyboard while it is open, it will accept the input.
#define maxloop 1000
- (IBAction)startTask:(id)sender
{
// Prepare sheet and show it...
breakLoop = NO;
NSRect sheetRect = NSMakeRect(0, 0, 400, 114);
NSWindow *progSheet = [[NSWindow alloc] initWithContentRect:sheetRect
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:YES];
NSView *contentView = [[NSView alloc] initWithFrame:sheetRect];
NSProgressIndicator *progInd = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(143, 74, 239, 20)];
NSTextField *inputField = [[NSTextField alloc] initWithFrame:NSMakeRect(145, 48, 235, 22)];
NSButton *cancelButton = [[NSButton alloc] initWithFrame:NSMakeRect(304, 12, 82, 32)];
cancelButton.bezelStyle = NSRoundedBezelStyle;
cancelButton.title = #"Cancel";
cancelButton.action = #selector(cancelTask:);
cancelButton.target = self;
[contentView addSubview:progInd];
[contentView addSubview:inputField];
[contentView addSubview:cancelButton];
[progSheet setContentView:contentView];
[NSApp beginSheet:progSheet
modalForWindow:self.window
modalDelegate:nil
didEndSelector:NULL
contextInfo:NULL];
[progSheet makeKeyAndOrderFront:self];
[progInd setIndeterminate:NO];
[progInd setDoubleValue:0.f];
[progInd startAnimation:self];
// Start computation using GCD...
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < maxloop; i++) {
[NSThread sleepForTimeInterval:0.01];
if (breakLoop)
{
break;
}
// Update the progress bar which is in the sheet:
dispatch_async(dispatch_get_main_queue(), ^{
[progInd setDoubleValue: (double)i/maxloop * 100];
});
}
// Calculation finished, remove sheet on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[progInd setIndeterminate:YES];
[NSApp endSheet:progSheet];
[progSheet orderOut:self];
});
});
}
- (IBAction)cancelTask:(id)sender
{
NSLog(#"Cancelling");
breakLoop = YES;
}
Apologies for the ugly sheet, but apart from that this code works as expected, so the issue you are seeing is probably unrelated to GCD.
I had exactly the same problem. With some trial and error, I found the solution. Make sure your sheet's window is (a) an NSWindow not an NSPanel (this may not matter) and that the window has a Title Bar (which, as it's a sheet you're using) will not be displayed.
I turned the Title Bar off for that reason, but somehow it's required to correctly achieve focus. Ticking the Title Bar checkbox gives my progress bar sheet focus.