iCarousel stop at user picked index

前端 未结 4 1051
春和景丽
春和景丽 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: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 :) !!

提交回复
热议问题