iCarousel stop at user picked index

前端 未结 4 978
春和景丽
春和景丽 2021-01-11 21:28

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 iCarou

相关标签:
4条回答
  • 2021-01-11 21:34

    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]];
    }
    
    0 讨论(0)
  • 2021-01-11 21:36

    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 :) !!

    0 讨论(0)
  • 2021-01-11 21:50

    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

    0 讨论(0)
  • 2021-01-11 21:54

    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.

    0 讨论(0)
提交回复
热议问题