EDIT :
I'm making an app like a Slot Machine, i added iCarousel
for the slot object.
So I have a button that rotates the iCarousel
. In my iCarousel view there are two slots (Slot1 and Slot2). Below is my iCarouselView
: (The box is where the TWO carousel
is)
This is how I spin my iCarousel
:
-(IBAction) spin {
[carousel1 scrollByNumberOfItems:-35 duration:10.7550f];
[carousel2 scrollByNumberOfItems:-35 duration:10.7550f];
}
Here is what I wanna do: I want to forcefully make it stop to the index the user picks.
I have done it this way, the image below is a newViewcontroller
that contains the UIImageView
with a button in it, so when the user taps it, my CustomPicker
pops up. My CustomPicker
contains the image on what the user have picked on the camera roll. So each button has a specific value sent to my iCarouselView
. carousel1 for slot1 and carousel2 for slot2.
So my problem is to spin the carousel and then for a specific time make it stop to the desired image/index the user picks.
Sorry for my bad english.
It's easy. I've done something similar before.
In the following method below:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{
//Configure your view here
.....
//Add a button on each view of the carousel
UIButton * someButton = [[UIButton alloc] initWithFrame:view.bounds];
[someButton setAlpha:0.0];
[someButton addTarget:self action:@selector(fingerLanded:) forControlEvents:UIControlEventTouchDown];
[view addSubview:someButton];
[view bringSubviewToFront:someButton];
//Add a tag - correspond it to the index
someButton.tag = index;
//Make sure the view is user interaction enabled
[view setUserInteractionEnabled:YES];
//Also make sure the button can actually receive touches and there is no view
//blocking touch events from being sent to the button.
return view;
}
Also add
- (void) fingerLanded:(id) sender{
UIButton * theButtonFingerLandedOn = (UIButton *) sender;
//This is the index the user tapped his finger on
NSUInteger index = theButtonFingerLandedOn.tag;
//Scroll to that particular index
[self.carousel scrollToItemAtIndex:index animated:YES];
}
Also add
- (void) viewDidLoad{
//You may need this to enable user interaction on a view flying by
// Look in the - (void)transformItemViews method of the iCarousel library
self.carousel.centerItemWhenSelected = NO;
}
You have to do something similar. Hope it was helpful :) !!
Without extending UIButton you can add a unique TAG to the button, getting the tag from the source of the UIEvent.
IE:
-(void) buttonSetupMethod {
for(each item) {
UIButton * button = [[UIButton alloc] init];
... additional setup ...
button.tag = SOME_UNIQUE_TAG_FOR_BUTTON;
[button addTarget:self action:@selector(processaction:) forControlEvents:desiredEvents];
... retain button ...
[button release];
}
}
-(void) processAction:(id) source
{
UIButton * button = (UIButton *) source;
int tag = button.tag;
... conditional logic on specific tag ...
}
for additional information on passing parameters see : Passing parameters on button action:@selector
Why wouldnt you do your tableview idea, take the below routine call:
[carousel scrollByNumberOfItems:-35 duration:10.7550f];
and put that into another non-action routine, and have the TableView didSelectItemAtIndex method call your new routine, and have your action routine call it.
-(void)newRoutineToCall:(int)itemsToMove{
[carousel scrollByNumberOfItems:itemsToMove duration:10.7550f];
}
-(IBAction) spin {
[self newRoutineToCall:-35];
}
Then Implement
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self newRoutineToCall:[indexPath row]];
}
Ok, let me try rephrasing the question: You have an array of images "imageArray" with one special one "carousel1Image" (selected by newViewController). You want the carousel to go around and around for N seconds and then land on the special one?
If that's correct, something like this should work...
-(void) spinCarousel: (iCarousel *) carousel toIndex:(NSInteger) targetIndex {
NSInteger imagesPerSecond = 5;
double secondsToScroll = 10.7550f;
NSUInteger currIndex = [carousel currentItemIndex];
NSUInteger numItems = [carousel numberOfItems] + [carousel numberOfPlaceholders];
NSUInteger timesAround = floor(secondsToScroll*imagesPerSecond/numItems);
NSInteger numToScroll = timesAround*numItems + targetIndex-currIndex;
[carousel scrollByNumberOfItems:numToScroll duration: secondsToScroll];
}
[self spinCarousel: carousel1 toIndex:[imageArray indexOfItem:carousel1Image]];
[self spinCarousel: carousel2 toIndex:[imageArray indexOfItem:carousel2Image]];
====Old Answer====
I share the confusion of others about what the goal is. For example, it's not clear what a "price" is and what it has to do with your slot machine? "I want the user to pick their last price, it means that the last view or image to where my icarousel can stop" Sorry, but that doesn't match my understanding of the word "price".
But reading your scenario at the end: "what I want is when the user click a UITableViewCell or thumbnail with a specific image from the array, it will pass something/value to my UIButton spin, so that the image click from the UITableViewCell will be the last price." I interpret that as EITHER there is a single value that your spin button needs to have access to OR that there is a value for EACH image that your spin button needs to have access to. Either one seems simple, so I'm not sure why the question.
Assuming it's the first (a single price), then why can't you just have your settings controller set a property with that value (same as how you pass your image NSMutableArray)?
If it's the second (a value for each image), then either create two parallel arrays, one with the price and one with the image, OR have an array with dictionaries in it, each with a price and an image.