iCarousel中的按钮不起作用

时间:2017-06-24 05:30:00

标签: ios objective-c 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;

我已经尝试了我用Google搜索的所有建议,但没有一个在我的情况下工作。如何在iCarousel中点击按钮?

2 个答案:

答案 0 :(得分:1)

它的工作完美尝试此代码

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

答案 1 :(得分:0)

该按钮未点击,因为您已将其添加为UIImageView的子视图,但不允许子视图接收触摸事件。相反,您应该将传入的视图分配为UIView,然后添加两个子视图;一个UIButton和一个UIImageView。这些子视图可以分别存储您的按钮和卡片。