Кнопки в iCarousel не работают
Я установил кнопки программно, как показано ниже:
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
DoctorInfo *docInfo = self.allDoctorInfo[index];
//create new view if no view is available for recycling
if (view == nil)
{
//don’t do anything specific to the index within
//this `if (view == nil) {…}` statement because the view will be
//recycled and used with other index values later
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
CGFloat centerPoint = (view.frame.size.width / 2) - 25;
((UIImageView *)view).image = [UIImage imageNamed:@"card_shadowed.png"];
view.contentMode = UIViewContentModeCenter;
UIImageView *headImage = [[UIImageView alloc] initWithFrame:CGRectMake(centerPoint, -30, 50, 50)];
headImage.image = [UIImage imageNamed:@"face.png"];
[view addSubview:headImage];
//share button
UIButton *shareButton = [[UIButton alloc]initWithFrame:CGRectMake(170, -40, 20, 20)];
[shareButton setImage:[UIImage imageNamed:@"share_button.png"] forState:UIControlStateNormal];
[shareButton addTarget:self action:@selector(shareSelected:) forControlEvents:UIControlEventTouchUpInside];
[shareButton.superview setUserInteractionEnabled:YES];
shareButton.clipsToBounds = YES;
[view addSubview:shareButton];
[self.view bringSubviewToFront:shareButton];
return view;
}
И в viewdidload:-
self.carousel.delegate = self;
self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeTimeMachine;
self.carousel.userInteractionEnabled = YES;
Я перепробовал все предложения, которые я гуглил, но ни один не работает в моем случае. Как сделать кнопку нажатой внутри iCarousel?
2 ответа
Его работает идеально, попробуйте этот код
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var itemView = UIImageView()
itemView = UIImageView(frame: CGRect(x: 30, y: 0, width: carousel.frame.size.width - 60, height: 220 ))
itemView.image = UIImage(named: imagesArray[index])
itemView.contentMode = .scaleToFill
itemView.setCornerRadius(clipToBounds: true, radius: 10)
//itemView.setBorder(color: .red, width: 2)
let vwHeight = CGFloat(200)
let btnHeight = CGFloat(100)
let itemHeight = itemView.frame.size.height
let itemWidth = itemView.frame.size.width
let carouselX = CGFloat(30)
let customBtnView = UIView()
customBtnView.frame = CGRect(x: carouselX, y: itemHeight - vwHeight , width: itemWidth, height: vwHeight)
customBtnView.backgroundColor = UIColor.clear
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: itemWidth, height: btnHeight)
button.backgroundColor = UIColor.clear
button.tag = carousel.currentItemIndex
button.addTarget(self, action: #selector(TutorialsVC.buttonNextAction), for: .touchUpInside)
customBtnView.addSubview(button)
carousel.addSubview(customBtnView)
return itemView
}
func buttonNextAction(sender: UIButton) {
print("Next==> \(sender.tag)")
switch sender.tag {
case 0:
//do action number 1
break
case 1:
//do action number 2
break
default:
break
}
}
Кнопка не нажимает, потому что вы добавили ее как подпредставление UIImageView
, который не позволяет подпредставлениям получать сенсорные события. Вместо этого вы должны выделить переданное представление как UIView, а затем добавить два подпредставления; UIButton
и UIImageView
, Эти подпредставления могут хранить вашу кнопку и вашу карту соответственно.