How to Add a button into iCarousel View? - ios

I am using iCarousel for showing an image. But I want to add a button into iCarousel. I implemented it but problem is that custom button action does not work, only didSelectItem work when I click on the button. here is my code. Please, any one helps me out.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
NSString *str=[[appController
sharedappController].allLibraryImagesArrM objectAtIndex:index];
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
((UIImageView *)view).image = [[appController sharedappController].allLibraryImagesArrM objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
//[view addSubview:cross];
UIButton *cross = [[UIButton alloc]init];
cross.frame = CGRectMake(view.frame.size.width/2+30, view.frame.origin.y+30, 40, 40);
[cross setBackgroundImage:[UIImage imageNamed:#"circle"] forState:UIControlStateNormal];
[cross addTarget:self action:#selector(crossMethod) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImage = [UIImage imageNamed:#"cross.png"];
[cross setBackgroundImage:buttonImage forState:UIControlStateNormal];
[view addSubview:cross];
return view;
}

Did you enable UserInteraction?
Enable UserInteraction
[view setUserInteractionEnabled:YES];

Related

UIButton image cannot set properly by taking names from the NSArray

Hello I have a problem like this. In my ViewController I have a delegate method like this
`
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
imgArray=[NSArray arrayWithObjects:#"water-dark.png",#"land-dark.png",#"animal-dark.png",#"air-dark.png",#"other-dark.png", nil];
button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
//UIImage *image = [UIImage imageNamed:#"page.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, 50, 50);
button.layer.cornerRadius=button.frame.size.width/2.0;
button.layer.borderWidth=2;
button.clipsToBounds = YES;
button.titleLabel.font = [button.titleLabel.font fontWithSize:15];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
NSString *strIndex=[NSString stringWithFormat:#"%i",index];
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [imgArray objectAtIndex:[strIndex intValue]]]] forState:UIControlStateNormal];
NSLog(#" CURRENT INDEX %i",index);
return button;
}
`
Here Im lacing 5 buttons on viewcontroller. Those 5 buttons titles are setting perfectly. I put the index number as the title. And now I want to get the image name from theimgArray` and set each image to the button. But the problem is
it set only 0th button image and the 4th(5th) button image only. But when I try to print the image name by taking from the array. All print on each button properly.
What should be the reason for this? And how can I solve this problem. Please help me
This is the output that I get currently. But I need to fill all 5 buttons with images in the array.
Hey First Of all There is no need to take a Button and its click event instead you can just return view in - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view in this method as I listed beolw.
and ya of course you want its click event instead you can use its one of the delegates - (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
This is your main method viewForItemAtIndex()
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
((UIImageView *)view).image = [UIImage imageNamed:[imgArray objectAtIndex:index]];
view.alpha = 1.0;
[view.layer setCornerRadius:125];
[((UIImageView *)view).layer setMasksToBounds:YES];
[((UIImageView *)view) setContentMode:UIViewContentModeScaleAspectFit];
view.contentMode = UIViewContentModeCenter;
return view;
}
Here the arrImages is declared in .h file of your view controller and assigning it in viewDidLoad() method i.e. imgArray = [NSArray arrayWithObjects:#"water-dark.png",#"land-dark.png",#"animal-dark.png",#"air-dark.png",#"other-dark.png", nil];
and for knowledge it is not necessary to write or assign array of images with .png after every image name like you can also write imgArray = [NSArray arrayWithObjects:#"water-dark",#"land-dark",#"animal-dark",#"air-dark",#"other-dark", nil];

How to set subview as a first responder?

I am trying to close the image View while clicking on close button.but button does not responds to touch event.
here is my code:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:#"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}
For the UIImageView to handle touches you need to set userInteractionEnabled to YES, it defaults to NO.
Try:
[imageView setUserInteractionEnabled:YES];
Reference
There is 1 property of UIImageView userInteractionEnabled and its value is by default NO. So in your code add
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;
Maybe there's another view with tag=1 in your self.view. Please try to edit button clicked like this:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}

How to get index of uiimage subview of uiscrollview

I need to detect selected image of uiimage which is placed inside scrollview.
My code is here:
int newscrollviewY = 40;
for(int i = 0; i<1; i++)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5, newscrollviewY, 310, 100)];
scrollView.showsVerticalScrollIndicator = YES;
scrollView.scrollEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.delegate=self;
int imgx = 1;
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
[scrollView addSubview:imageView1];
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
Please help to detect selected image using imageButton or something else.
Replace your selector with buttonClicked:
be Sure that you are adding ":" after selector method. follow or replace the code below:
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
then Add the selector definantion in .m file
- (void) buttonClicked: (id) sender
{
// here you can get the tag of button.
NSInteger tag = ((UIButton *)sender).tag;
// do your implementation after this
}
Hope this will solve your issue. Enjoy Coding..!!
They way you have set it up the tag of the 'button' is already the number of the image in your array of images. So all you need to do is get the tag from the 'button'.
Make sure the button clicked function retrieves the tag from the sender(id) that you receive.
For example a bit like this:
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
UIImage selectedImages = [images objectForKey: [button tag]];
}
As par you question if you are using UIImageview you have to use UITapGestureRecognizer that same like Button. you just need to add UITapGestureRecognizer like:-
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(frameTapGesture:)];
frameTapGesture.numberOfTapsRequired=1;
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
imageView1.tag=img;
[_imgView addGestureRecognizer:frameTapGesture];
[scrollView addSubview:imageView1];
}.
- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view; //cast pointer to the derived class if needed
NSLog(#"%d", view.tag);
}
Reduce your code There are No need to add Button with same Frame of ImageView For getting Click Event or you can also set Image of UIButton for you want to like use UIBUtton then remove UIImageview Code else you simply use UITapGestureRecognizer with getting click event using - (void)frameTapGesture:(UITapGestureRecognizer*)sender
Please try this code
for(int img=0; img<[images count]; img++)
{
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setImage:[UIImage imageNamed:[images objectAtIndex:i]] forState:UIControlStateNormal];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
- (void) buttonClicked: (id) sender
{
UIButton *button = (UIButton *)sender;
UIImage selectedImage = button.currentImage;
}
set button target
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// get Button Action
-(void) buttonAction:(UIButton *)sender
{
NSLog(#"%dl",sender.tag);
// do your implementation after this
}

how to change uiimageview image's programmatically

In my ios application i create uiimageviews programmatically like that,
for (int i=0; i<20; i++) {
productID=[products objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = productID;
[scrollView addSubview:button];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
}
in my buttonActon, i want to change that uiimageview's image to another image. But i can not do that. Can anyone help me? Thank You.
try like this ,
for (int i=0; i<20; i++) {
productID=[products objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+productID;
[scrollView addSubview:button];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
}
keep this one in button action method and pass the imageview tag in the place of imgview.tag
UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
img.image=[UIImage imageNamed:#"name.png"];
Instead of this:
imageView.tag=UrunId;
Write:
imageView.tag = productID + 100;
Because you need a unique tag for each Image View.
Then implement the buttonAction: like:
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:(sender.tag+100)];
[imageView setImage:yourImage];
}
Please try to use this one
You should have different tag for button and image.So please give first different tag like this....
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=button.tag+1;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
After that do this...
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:sender.tag+1];
imageView.image=[UIImage imageNamed:#"imageName"];
}

iCarousel issue

I'm trying to add a iCarousel to an existing project with the code below, but I'm getting this error: MemberCatalog[14990:1a903] -[MainViewController carousel:viewForItemAtIndex:reusingView:]: unrecognized selector sent to instance 0xa0589b0.
The line in iCarousel.m throwing the error is: view = [_dataSource carousel:self viewForItemAtIndex:index reusingView:[self dequeueItemView]];
Any help it will be really appreciate it.
carouselItems = [NSArray arrayWithObjects:
[UIImage imageNamed:#"iLibrary+Featured_AM-RAH.png"],
[UIImage imageNamed:#"iLibrary+Featured_CCA.png"],
[UIImage imageNamed:#"iLibrary+Featured_GI-PA.png"],
nil];
// Initialize and configure the carousel
carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeCoverFlow2;
carousel.delegate = self;
carousel.dataSource = self;
[self.view addSubview:carousel];
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [carouselItems objectAtIndex:index];
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
button.tag=index;
return button;
}
You're implementing a -carousel:viewForItemAtIndex: method, but the view expects a -carousel:viewForItemAtIndex:reusingView: method.
You need to update your method with the reusingView:(UIView *)view parameter and you'll be fine. You should also use that reusable view instead of allocating more and more views, but that's another story.

Resources