Keep a UIButton selected for a fraction of time in iPhone? - iphone

How can I keep a custom UIButton highlighted on selection, for about 5 seconds and then redirect to other view? In my code it is not showing any color when selected rather redirecting to other view.
This is my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect rect=CGRectMake(0+70*j,yy,79,40);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setContentMode:UIViewContentModeLeft];
button.titleLabel.font = [UIFont systemFontOfSize:14];
NSString *settitle=[NSString stringWithFormat:#"%#",item.TimeStart];
[button setTitle:settitle forState:UIControlStateNormal];
NSString *tagValue=[NSString stringWithFormat:#"%d%d",indexPath.section+1,i];
button.tag=[tagValue intValue];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setShowsTouchWhenHighlighted:YES];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
[hlcell.contentView addSubview:button];
[button release];
}
-(IBAction)buttonPressed:(id)sender
{
[sender setBackgroundColor:[UIColor cyanColor]];
Login *lgn=[[Login alloc] init];
[self presentModalViewController:lgn animated:NO];
[lgn release];
}
How can I do it ?

You have to set setShowsTouchWhenHighlighted:yes to the UIButton.
for example:
UIButton *click_btn=[UIButton buttonWithType:UIButtonTypeCustom];
click_btn.frame=CGRectMake(238,10, 70, 70);
[click_btn setTag:indexPath.row];
[click_btn setShowsTouchWhenHighlighted:YES];
[click_btn addTarget:self action:#selector(sample) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:click_btn];

you just add inside that method sleep(5);
for example:
This is user sample uibutton action method.
-(void)sample{
sleep(5);
sampleclass *classobject=[[sampleclass alloc]init];
[self.navigationController pushViewController:classobject animated:YES];
}

I wouldn't suggest using sleep() as this sleeps all threads and makes the app essentially dead for x seconds.
Highlight the button as per the instructions above and then use
[self performSelector:#selector(sample) withObject:self afterDelay:5.0f];
-(void)sample
{
UIViewController *myNewController = [[UIViewController alloc]init];
enter code here
[self presentModalViewController:myNewController animated:YES];
}

Related

calling the ibaction method programmatically is not changing the background of the button?

I had an application in which I am adding some dynamic number of buttons to a scrollview. I had set a normal background and selected background for the UIButton. For some reasons I need to call the UIButton sender method programmatically by:
[self buttontapped:nil];
as this but it is not changing the background of the button by using the code:
button.selected = YES;
I had set the background like this initially of the button:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
int j = i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected = NO;
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[tabBarS addSubview:btn];
-(void)buttonTapped:(id)sender {
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
btn.selected=NO;
}
btn = (UIButton *)sender;
NSLog(#"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
btn.selected = YES;
}
But everything except change of background only working. Where am I going wrong?
i done with bellow method call your UIButton method like:-
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected=NO;
[btn addTarget:self action:#selector(yourButtonAction:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
btn.tag = i;
[tabBarS addSubview:btn];
and it's Action Method should like with sender
-(IBAction)yourButtonAction:(id)sender
{
UIButton *btn = (UIButton*)sender;
if (![btn isSelected])
{
[btn setImage:[UIImage imageNamed:#"selected_fb_btn.png"] forState:UIControlStateNormal];
[btn setSelected:YES];
[self login];
}
else{
[btn setImage:[UIImage imageNamed:#"facebook.png"] forState:UIControlStateNormal];
[btn setSelected:NO];
}
}
EDIT
You can call button action event programeticuly using bellow trik:-
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
write this bellow line first and also change the method type from void to IBAction...
btn = (UIButton *)sender;
like bellow...
-(IBAction)buttonTapped:(id)sender {
btn = (UIButton *)sender;
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
[btn setSelected:NO];
}
NSLog(#"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
[btn setSelected:YES];
}
You need to set image property rather than backgroundImage property of UIButton.
Use default Image property and selected property.
[btn setSelected:TRUE];
is the log that you are printing, prints the button.tag correctly?
NSLog(#"Tab bar %d is clicked",btn.tag);
In your button action
btn.selected=YES;
in last line makes the button in selected state always
also get the correct instance as
btn=(UIButton *)sender;

How to implement delete functionality for images in different positions in iCarousel iphone

I am using iCarousel as shownh here with carousel type Liner Carousel and implemented delete functionalty .I am able to delete the image in carousel and when I attempt to delete any other iomage in the visible screen It is moved to carousel frame and deleted.
I need to delete the image from its original position.
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor blackColor];
carousel = [[iCarousel alloc] initWithFrame:CGRectMake(-130,300, 320, 100)];
carousel.dataSource = self;
carousel.delegate=self;
carousel.type = iCarouselTypeLinear;
carousel.scrollEnabled=YES;
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 200, 200)];
[self.view addSubview:imageView];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *image = [imagesArray objectAtIndex:index];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0, 60, 60);
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag=index;
NSLog(#"tag is %d",button.tag);
UIButton *deleteButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame= CGRectMake(50, -5, 20 ,20);
UIImage *img = [UIImage imageNamed:#"close.png"];
[deleteButton setImage:img forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:deleteButton];
return button;
}
-(void)deleteButtonClicked:(int )sender
{
NSInteger currentIndex = carousel.currentItemIndex;
[carousel removeItemAtIndex:currentIndex animated:YES];
}
Please help me out.
You shouldn't delete the item at the carousel.currentItemIndex because that is not the item corresponding to the button you clicked, that's just the currently centred item in the carousel.
To get the correct item index for the button you clicked, do this:
-(void)deleteButtonClicked:(id)sender
{
//get the index of the item view containing the button that was clicked
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//update the data model (always do this first)
[imagesArray removeObjectAtIndex:index];
//remove item from carousel
[carousel removeItemAtIndex:index animated:YES];
}
Try this:
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[imagesArray removeObjectAtIndex:index];
add this also:
- (void)awakeFromNib
{
if (self) {
//set up carousel data
wrap = NO;
}
}
or make your
carousel.type = iCarouselTypeCustom;
to
carousel.type = iCarouselTypeLinear;
then implement this: [carousel reloadData];

UIButton ControlState not responding as wanted

Well, I have a custom uitabbarcontroller with 3 UIButtons on it (simulating tabs). I have set background images for normal, selected and highlighted states (selected and highlighted are both the same).
Everything works fine but one thing: When I have on tab (button) selected, and that tab gets pressed again, instead of highlighting it shows the button being pressed (getting darker). I have tried setting to NO the property adjustsImageWhenHighlighted, but instead of getting darker, it shows the Normal state background.
Any suggestion?
EDIT: This is the code I have in the UITabBarController subclass
#import "MyTabBarViewController.h"
#interface MyTabBarViewController ()
#end
#implementation MyTabBarViewController
ExploreViewController *exploreController;
ProfileViewController *profileController;
UIButton* leftButton;
UIButton* rightButton;
- (void)viewDidLoad
{
[super viewDidLoad];
exploreController = [[ExploreViewController alloc] initWithNibName:#"ExploreViewController" bundle:nil];
profileController = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:exploreController, profileController, nil];
[self addLeftButtonWithImage: [UIImage imageNamed:#"LeftTabBarIcon"] highlightImage:[UIImage imageNamed:#"LeftTabBarIcon_On"]];
[self addRightButtonWithImage: [UIImage imageNamed:#"RightTabBarIcon"] highlightImage:[UIImage imageNamed:#"RightTabBarIcon_On"]];
}
- (void) leftTabPressed
{
leftButton.selected = YES;
rightButton.selected = NO;
[self setSelectedViewController:exploreController];
}
- (void) rightTabPressed
{
rightButton.selected = YES;
leftButton.selected = NO;
[self setSelectedViewController:profileController];
}
-(void) addLeftButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.adjustsImageWhenHighlighted = NO;
[[leftButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[leftButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[leftButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
leftButton.frame = CGRectMake(0.0, 367, 160.0, 49.0);
[leftButton addTarget:self action:#selector(leftTabSelectPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftButton];
}
-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.adjustsImageWhenHighlighted = NO;
[[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
[rightButton addTarget:self action:#selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
}
#end
For your Comment your code will be like this:
-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.adjustsImageWhenHighlighted = NO;
rightButton.showsTouchWhenHighlighted = NO;
[[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted | UIControlStateSelected];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
[rightButton addTarget:self action:#selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *longGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureDetected:)]autorelease];
longGesture.delaysTouchesBegan = YES;
[rightButton addGestureRecognizer:longGesture];
[self.view addSubview:rightButton];
}
- (void)longGestureDetected:(UILongPressGestureRecognizer*)longGesture
{
if(longGesture.state == UIGestureRecognizerStateBegan)
[self rightTabPressed];
}
Trick seemed to be changing UIControlStateHighlighted to UIControlStateHighlighted | UIControlStateSelected. Seems odd, but it works.

adding reflection to iCarousel

I tried to add reflection to my icarousel. Firstly my code was like this and it was working wright.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton* button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
return button;
}
After changing, my code became like this:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (button == nil)
{
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[view update];
return view;
}
Reflection appears. But the problem is when i move the carousel view, images not flowing correctly. Sometimes another carousel view appears.
Do you have any idea what causes that?
Code should be:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (view == nil)
{
//no view available to recycle, so create new one
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 99;
[view addSubview:button];
}
else
{
button = (UIButton *)[view viewWithTag:99];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];
[view update];
return view;
}
I use images with reflection built in them... don't need to do anything in code that way.. :p
I found my mistake. I am loading images here:
-(void) getImagesWithThread{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//for(int i=0;i<bookCount;i++){
for(int i=0;i<bookCount;i++){
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [arrBookImages objectAtIndex:i]]];
UIImage *image = [[UIImage imageWithData: imageData] retain];
image = [self scaleToSize:CGSizeMake(140, 200) withImage:image];
if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image];
[image release]; }
[carousel reloadData];
[pool drain];
}
When i performed this in background it didn't reload correctly. İf i perform normally, it works. But i want to load them in a thread. I mean first i want to set images and replace them images from url. but i want them to appear one by one. Do you have any idea?

Putting a button in table footer view

I am creating a custom button and putting it in my table's footer view but the button is going way out from the right corner.
What is wrong here!?!
Here is my code and output image:
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[aButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
[aButton setTitle:#"Click me" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
[aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
[self.tableView setTableFooterView:aButton];
I added the button in the UIView and then set the tableFooterView to this UIView object and it worked. We cannot directly put UIButton as tableFooterView.
Try doing like this. Set IBOutlet UIView *footerView; and synthesize it. Add it as a subview using the method-
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Click Me" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
//set action of the button
[button addTarget:self action:#selector(clickPressed:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
One important addition to Legolas' fine answer: You'll need to implement the tableView delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
foot 50;
}
This was driving me nuts because the button (or, in my case, buttons) were not responding to the touch event. Adding the delegate method made it all better. :-)