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?
Related
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;
I am created two buttons that are right next to each other to mimic a segmented control. I am doing this to customize the appearance beyond what the UIKit allows. I decided to use the selected property to keep a button pressed. I have two images that one for each state normal and selected.
The problem is that when I select a button, the button highlights and turns dark, because of the hightlight state. I decided to use the selected image for the highlight state too, but it flashes, any ideas or suggestions.
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
For the "selected" button, disable it and manually switch the image for the state.
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
Check whether the image you given is in your Bundle or check image name you given is in lower case or not. Then write like
[button1 setImage:[UIImage imageNamed:#"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
in your button event method
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
[button setAdjustsImageWhenHighlighted:NO];
This will prevent the flicker.
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:#"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:#selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
do same for another second button.
and in action method set bellow code..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}
Im really having a problem with my image with button frame,
What I want is:
1. When it is selected it will have a checkmark
2. When the view is dismissed then returned back to the previous screen it will still have the check in the image selected
3. When I tap the selected image or another image it will remove the checkmark.
I was able to do it but I'm having problem with the setting of image:
What I want is this:
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
But this is the function I needed:
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setImage:bgImage forState:UIControlStateSelected];
[myButton setSelected:YES];
Here is the code below(If there is something wrong with what I'm doing please tell me, Thanks):
- (void)viewDidLoad {
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected];
}
[view addSubview:myButton];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}
- (void)turnButtonToSelected
{ UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];
if (myButton.selected) {
[sender setSelected:NO];
}else {
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:myButton.tag forKey:#"slot1"];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:#"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self performSelector:#selector(highlightButton:) withObject:sender afterDelay:0.0];
[self dismissModalViewControllerAnimated:NO];
}
Or is there a way to forcefully make a button enabled whenever it is disabled when tapped also?
I have just tried this on a different approach.
For me it works just fine and achieves your effect.
Let me know if you need further explanation or the entire Xcode project.
- (void)viewDidLoad
{
[super viewDidLoad];
//button 1
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(30, 50, 100, 80);
UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//button 2
UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton2.frame = CGRectMake(180, 50, 100, 80);
UIImage *btnImage2 = [UIImage imageNamed:#"DSC_5355.JPG"];
[imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
[imageButton2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:imageButton];
[self.view addSubview:imageButton2];
}
- (void)buttonClicked:(UIButton*)button
{
//NSLog(#"Button clicked! %#", button);
UIImage *checkmarkImage = [UIImage imageNamed:#"check.png"];
UIImage *empty = nil;
//NSLog(#"Imageview: %d", button.imageView.tag);
if (button.imageView.tag == 0)
{
[button setImage:checkmarkImage forState:UIControlStateNormal];
button.imageView.tag = 1;
}
else
{
[button setImage:empty forState:UIControlStateNormal];
button.imageView.tag = 0;
}
}
Best, Chris
Update: I have updated the code. Here is the entire ViewController. Note: I would solve this using coreData to store the current index. The following code is not the best to solve this issue but is based on your initial code!
//
// SOViewController.m
// SOImageOverlay
//
// Created by Chris on 24.07.12.
//
#import "SOViewController.h"
#interface SOViewController ()
#end
#define CHECKMARK_IMAGE #"check.png"
#implementation SOViewController
#synthesize buttons = _buttons;
#pragma mark - Custom getter
- (NSMutableArray*)buttons
{
if (_buttons == nil)
_buttons = [[NSMutableArray alloc] init];
return _buttons;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
// UIButton *myButton =
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// Create view
// UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < 2; ++i) {
/*UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);*/
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
if (i == 0)
{
UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
imageButton.frame = CGRectMake(30, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
else if (i == 1)
{
UIImage *btnImage = [UIImage imageNamed:#"DSC_5355.JPG"];
imageButton.frame = CGRectMake(180, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected:imageButton];
}
[self.view addSubview:imageButton];
//adding current button into buttons array
[self.buttons addObject:imageButton];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
//[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
// //button 1
// UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton.frame = CGRectMake(30, 50, 100, 80);
// UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
// [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
// [imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// //button 2
// UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton2.frame = CGRectMake(180, 50, 100, 80);
// UIImage *btnImage2 = [UIImage imageNamed:#"DSC_5355.JPG"];
// [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
// [imageButton2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// [self.view addSubview:imageButton];
// [self.view addSubview:imageButton2];
}
//- (void)buttonClicked:(UIButton*)button
//{
// //NSLog(#"Button clicked! %#", button);
//
// UIImage *checkmarkImage = [UIImage imageNamed:#"check.png"];
// UIImage *empty = nil;
//
// //NSLog(#"Imageview: %d", button.imageView.tag);
// if (button.imageView.tag == 0)
// {
// [button setImage:checkmarkImage forState:UIControlStateNormal];
// button.imageView.tag = 1;
// }
// else
// {
// [button setImage:empty forState:UIControlStateNormal];
// button.imageView.tag = 0;
// }
//}
// code from stackoverflow
// ------------------------------------------------------------
- (void)turnButtonToSelected:(UIButton*)button
{
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
//UIButton *myButton = button; //just to use the copied code
button.selected = YES;
[button setImage:bgImage forState:UIControlStateSelected];
// [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender
{
UIButton *myButton = (UIButton *)sender;
//I have removed this line... due to the fact that I don't want to
//implement your arrays.
//self.selectedImage = [_images objectAtIndex:myButton.tag];
//deselecting all
for (UIButton *button in self.buttons)
{
button.selected = NO;
//[button setImage:nil forState:UIControlStateNormal];
}
if (myButton.selected) {
[sender setSelected:NO];
}else {
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
// NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// [prefs setInteger:myButton.tag forKey:#"slot1"];
//getting index of button in array
NSInteger index = 0;
NSInteger foundIndex = -1;
for (UIButton *button in self.buttons)
{
if (button == myButton)
{
foundIndex = index;
}
index++;
}
if (foundIndex >= 0)
{
[[NSUserDefaults standardUserDefaults] setInteger:foundIndex forKey:#"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self performSelector:#selector(highlightButton:) withObject:sender afterDelay:0.0];
// [self dismissModalViewControllerAnimated:NO];
}
// ------------------------------------------------------------
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)closeButtonClicked:(UIBarButtonItem *)sender
{
[self dismissModalViewControllerAnimated:YES];
}
#end
Try doing this.
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:NO];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];**
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:YES];
[myButton setImage:bgImage forState:UIControlStateSelected];**
In the later part of code, you are setting the state of the button to "selected" but in the upper code, you haven't. Rather, you have disabled the button. Try the above code which I have provided. Thanks.
Just add you chekedImage as a subview on top of the button when user clicks the button
UIImage *button = [UIImage imageNamed:#"cat.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
//add target button clicked
-(void)buttonClicked : (id)sender{
if(toogleSwitch){
[button addSubview:checkedimageView]; // Just the checked image icon
}
else{
[imageview removeFromSuperview];
}
}
What you are trying to do will only work when you have
resources of two types for all your images.
1) The image itself
2) Image with checked box as transparent layer.
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];
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.