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

一切正常,我非常了解这种控制。

但到目前为止,我使用手动代码在 iCarousel View 中添加 View 并将其显示在屏幕上,但现在根据我的新要求,我有 3 到 4 个新主题需要显示 iCarousel View ,因此下面是我与该 View 相关的问题.

  • 如何从 XIB 加载不同的 View ?
  • 如何将 View 从 Storyboard 加载到 iCarousel View ?

  • 目前我正在使用以下方法手动添加 View 。
    viewForItemAtIndex:(NSInteger)index resuingView:(UIView*) view
    

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

    最佳答案

    使用 nib 的自定义 View 创建带有倒轮类型的 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

    关于ios - 如何从 Storyboard或 xib 加载 iCarousel View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41976440/

    10-14 22:25
    查看更多