【问题标题】:How to load iCarousel view from storyboard or xib?如何从情节提要或 xib 加载 iCarousel 视图?
【发布时间】:2017-06-18 00:04:12
【问题描述】:

我正在使用一个第 3 方控件 iCarousel 视图来显示带有漂亮动画的视图。

一切正常,我非常了解这个控件。

但到目前为止,我正在使用手动代码在 iCarousel 视图中添加视图并将其显示在屏幕上,但现在根据我的新要求,我有 3 到 4 个新主题,我需要在下面显示 iCarousel 视图我的问题与那个观点有关吗?

  1. 如何从XIB 加载不同的视图?

  2. 如何从StoryboardiCarousel 视图加载视图?

目前我正在使用以下方法手动添加视图。

viewForItemAtIndex:(NSInteger)index resuingView:(UIView*) view

请通过Storyboard/ XIB帮助我实现这一目标。

【问题讨论】:

  • 您需要哪种解决方案?
  • 详细说明: 1.如何从 XIB 加载不同的不同视图? 2.如何将故事板中的视图加载到 iCarousel 视图中?
  • 需要在故事板或 xib 的 icarousel 视图中添加自定义视图。
  • @BhaviDev 你得到解决方案了吗?我也在找一样的。

标签: ios objective-c icarousel


【解决方案1】:

使用来自 nib 的自定义视图创建具有倒转轮类型的 iCarousel

   @IBOutlet weak var viewCarousel: iCarousel!

    override func viewDidLoad() {
            super.viewDidLoad()
        viewCarousel.type = .invertedWheel
        viewCarousel.isVertical = true
        viewCarousel.delegate = self
        viewCarousel.dataSource = self
    }

iCarousel 数据源

func numberOfItems(in carousel: iCarousel) -> Int {
    return 25
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
        // use a custom view from nib
        let view = Bundle.main.loadNibNamed("CarouselReuseView", owner: self, options: nil)![0] as! CarouselReuseView
        view.frame.size = CGSize(width: self.view.frame.size.width/2, height: 83.0)
        view.backgroundColor = UIColor.lightGray

        let friend = friendList[index] as friend
        view.lblName.text = friend.fullName
        let url = friend.photo
        let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {data, response, error in
            if error == nil {
                DispatchQueue.main.async {
                    view.imageView.image = UIImage(data: data!)
                    view.imageView.layer.cornerRadius = view.imageView.frame.size.width/2
                    view.imageView.layer.masksToBounds = true
                }
            }
        })
        task.resume()
        return view
    }

}

目标 C

@property (weak, nonatomic) IBOutlet iCarousel *carouselView;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _carouselView.type = iCarouselTypeInvertedWheel;
    _carouselView.vertical = true;
    _carouselView.delegate = self;
    _carouselView.dataSource = self;
    _carouselView.layer.masksToBounds = true;
}

iCarousel 数据源

-(NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return 10;
}

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {

    CustomView* myViewObject = [[[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil] objectAtIndex:0];

    myViewObject.lbl.text = [NSString stringWithFormat:@"%ld",(long)index];

    return myViewObject;
}

这里 CustomView 是我的 UIView 子类,“customView”是 UIView Xib 名称 希望对你有帮助

更多信息:- https://github.com/nicklockwood/iCarousel

【讨论】:

  • 您的意思是要使用 i caurosal 同时显示两个视图?
  • 使用上面的代码,它可以在 NIB 上正常工作,但我有一个问题,我们如何使用情节提要实现这一目标? ?
  • 您可以创建一个viewController并将viewController的视图作为轮播视图返回。您也可以使用单元格并返回 cell.containView
  • 对于 Swift 将框架添加到您从 viewForItemAt 方法返回的视图中 view.frame = CGRect(x: 0, y: 0, width: 200, height: 200) 对于 obj. c 添加 view.frame = CGRectMake(0, 0, 200.0f, 200.0f);
【解决方案2】:

试试这个:

-(UIView *) carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
        UIView  *globleView = (UIView *)view;
        if (!globleView) {
            globleView = [[[NSBundle mainBundle] loadNibNamed:@"yourView" owner:self options:nil] objectAtIndex:0];;
            globleView.backgroundColor = [UIColor clearColor];
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:globleView.frame];

            imageView.tag = 100;
            [globleView addSubview:imageView];

            UILabel *lblFeatureRecipeName = [[UILabel alloc]initWithFrame:CGRectMake(15, ViewHeight(globleView) -50, ViewWidth(globleView)-40, 50)];

            lblFeatureRecipeName.tag = 101;
            [globleView addSubview:lblFeatureRecipeName];
            if (index == 0) {
                [self refreshCarousleViewWithView:globleView withIndex:index];
            }
        }
        return globleView;
    }

【讨论】:

  • 但我不想在该方法中进行任何初始化,想要创建与 tableview 自定义单元格相同的内容,只需将数据分配给该标签和图像,而不是通过此方法创建和添加。
  • 即使我不想将任何框架设置为其自动布局启用,所以它会从情节提要中自行获取。
猜你喜欢
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 2012-08-15
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多