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];
Related
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];
I am using iCarouselTypeCoverFlow library in my app. I want to blur the back images.
I am able to blur images in viewForItemAtIndex. but while scrolling i am not able to update. here is my code for viewForItemAtindex
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
image = [items objectAtIndex:index];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
button.tag=index;
//NSLog(#"%d %d",carousel.currentItemIndex,index);
if (selectedindex == index) {
[button setBackgroundImage:[items objectAtIndex:index] forState:UIControlStateNormal];
}
else {
[button setBackgroundImage:[self blurredImageWithImage:[items objectAtIndex:index]] forState:UIControlStateNormal];
}
return button;
}
and here is my didscroll
- (void)carouselDidScroll:(iCarousel *)carousel
{
NSInteger current = (carousel.currentItemIndex);
// NSLog(#"%d", current);
NSInteger nextIndex = (carousel.currentItemIndex + 1) % carousel.numberOfItems;
// NSLog(#"%d", nextIndex);
NSInteger prevIndex = (carousel.currentItemIndex + carousel.numberOfItems - 1) % carousel.numberOfItems;
// NSLog(#"%d", prevIndex);
}
Any suggestion or help would be appreciated.
So finally i got my answer. definitely not hoping for this . but unfortunately answer is ,
nicklockwood answer
I am using Nick Lockwood's iCarousel class to display a series of images stored in an array using the method
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100.0f, 100.0f)];
((UIImageView *)view).image = [images objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
}
}
I would now like to display text stored in a similar array below(and centered) each image much like movie titles are placed under thumbnails in the imDB app. Any help would be immensely appreciated. Thanks in advance!
You can add UILabel to view i.e,
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100.0f, 100.0f)];
((UIImageView *)view).image = [images objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 100.0f, 30.0f)];
lbl.text = [titlesArray objectAtIndex:index]; // Here you can add titles to label
/* Here you can code the EFX. */
[view addSubview:lbl];
}
}
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
}
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.