我的自定义按钮没有在iCarousel iOS中点击?

时间:2014-03-03 11:47:27

标签: ios uibutton icarousel

在我的示例中,我正在使用iCarousel,我试图在点击它时扩展图像? 我做了什么,在图片展开后,我在backButton的右下角添加view,以便我可以返回到原始视图。但是那个按钮没有获得点击事件。我哪里错了?

这是我的代码。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{
UIButton *button = (UIButton *)view;
UIView *buttonBackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 475, 300)];
[buttonBackView setBackgroundColor:[UIColor blackColor]];
 button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, 475,300);

 UIImage *image=[_rareCSNYArrayImageItems objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

[buttonBackView addSubview:button];
 return buttonBackView;

}


    - (void)buttonTapped:(UIButton *)sender
{
UIButton *button=(UIButton*)sender;
[button setFrame:CGRectMake(0,0,675,400)];
[button.superview setFrame:CGRectMake(-100, -70, 675, 500)];
UIButton *buttonReduce=[[UIButton alloc] initWithFrame:CGRectMake(580, 405, 85, 85)];
[buttonReduce setBackgroundColor:[UIColor clearColor]];
[buttonReduce setBackgroundImage:[UIImage imageNamed:@"reducebuutton.png" ] forState:UIControlStateNormal];
[buttonReduce addTarget:self action:@selector(buttonReduceTapped: ) forControlEvents:UIControlEventTouchUpInside];
[button.superview addSubview:buttonReduce];
[self.view bringSubviewToFront:buttonReduce];
}

- (void)buttonReduceTapped:(UIButton *)sender{

UIButton *button=(UIButton*)sender;
NSLog(@"ButtonClicked");
[button.superview setFrame:CGRectMake(0, 0, 475, 300)];
[button removeFromSuperview];
}

2 个答案:

答案 0 :(得分:6)

我也有同样的问题,但我已经解决了下面的代码。

view.userInteractionEnabled = YES;

view =您的轮播视图项

OR

[yourButton.superview setUserInteractionEnabled:YES];

希望对别人有所帮助。

欢呼!!!!

答案 1 :(得分:2)

按钮可能超出了它的超视图范围。 iOS视图可以在其超视图范围之外绘制,但不会在这些边界之外接收触摸事件。

如果按钮位于轮播内,请尝试在iCarousel视图上启用clipsToBounds以测试理论。如果它在旋转木马项目视图中,请尝试在iCarousel项目视图上启用clipsToBounds。

如果它在旋转木马内,要么将其移到外面,要么只是将旋转木马架更大。但如果它位于项目视图中,则由于iCarousel内部工作的方式,您无法轻松制作单个轮播视图的动画。

在这种情况下,为了解决这个问题,我建议在旋转木马前面而不是在旋转木马内部绘制展开的视图。为了使动画更容易,您可以在旋转木马内部动画图像缩放,就像您当前所做的那样,但是在动画结束时将视图移动到超视图层次结构的前面,这样用户就不会看到切换。