My Custom Button is not getting click in iCarousel

2019-01-25 21:32发布

问题:

In my example I am using iCarousel in which I am trying to expand image on clicking on it? What I have done,after image expands I am adding backButton on right bottom corner of view so that I can return to the original view. But that button is not getting click event. Where am I going wrong?

here is my code.

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

回答1:

The button is probably outside the bounds of it's superview. iOS views can be drawn outside of their superview bounds, but won't receive touch events outside those bounds.

If the button is inside the carousel, try enabling clipsToBounds on the iCarousel view to test the theory. If it's inside a carousel item view, try enabling clipsToBounds on your iCarousel item views.

If it's inside the carousel, either move it outside, or just make the carousel frame bigger. But if it's inside an item view, you can't easily animate making individual carousel views bigger because of the way iCarousel works internally.

In that case, to fix it, I suggest drawing your expanded view in front of the carousel rather than inside it. To make it easier to animate, you could animate the image zoom while inside the carousel as you are doing currently, but then move the view to the front of the superview hierarchy at the end of the animation, so the user doesn't see the switchover.



回答2:

I also had the same problem but I have resolved with below code.

view.userInteractionEnabled = YES;

view = your carousel view item

OR

[yourButton.superview setUserInteractionEnabled:YES];

hope it helps to others.

cheers!!!!