10

编辑 :

我正在制作一个像老虎机一样的应用程序,我iCarousel为老虎机对象添加了。所以我有一个按钮可以旋转iCarousel. 在我的 iCarousel 视图中,有两个插槽(插槽 1 和插槽 2)。下面是我的iCarouselView:(框是两个所在的carousel位置)

在此处输入图像描述

这就是我旋转我的方式iCarousel

-(IBAction) spin {

[carousel1 scrollByNumberOfItems:-35 duration:10.7550f];
[carousel2 scrollByNumberOfItems:-35 duration:10.7550f];

}

这是我想做的:我想强制让它停止到用户选择的索引。

我已经这样做了,下面的图片是一个newViewcontroller包含UIImageView一个按钮的图片,所以当用户点击它时,我的CustomPicker弹出窗口。MyCustomPicker包含用户在相机胶卷上选取的图像。所以每个按钮都有一个特定的值发送到 my iCarouselView. carousel1 用于 slot1,carousel2 用于 slot2。

所以我的问题是旋转旋转木马,然后在特定时间使其停止到用户选择的所需图像/索引。

对不起,我的英语不好。 在此处输入图像描述

4

4 回答 4

4

这很简单。我以前做过类似的事情。

在下面的方法中:

- (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;
}

还添加

- (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];

}

还添加

- (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;

 }

你必须做类似的事情。希望它有帮助:) !!

于 2012-06-06T15:45:44.257 回答
0

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.

于 2012-06-05T16:08:58.927 回答
0

在不扩展 UIButton 的情况下,您可以向按钮添加唯一的 TAG,从 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 ...
 }

有关传递参数的更多信息,请参见:在按钮操作上传递参数:@selector

于 2012-05-31T14:50:21.357 回答
0

你为什么不做你的 tableview 想法,接受下面的例行调用:

[carousel scrollByNumberOfItems:-35 duration:10.7550f];

并将其放入另一个非操作例程中,并让 TableView didSelectItemAtIndex 方法调用您的新例程,并让您的操作例程调用它。

-(void)newRoutineToCall:(int)itemsToMove{
    [carousel scrollByNumberOfItems:itemsToMove duration:10.7550f];
}

-(IBAction) spin {        
    [self newRoutineToCall:-35];
} 

然后实施

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self newRoutineToCall:[indexPath row]];
}
于 2012-06-01T17:27:49.720 回答