4

I am using one 3rd party control iCarousel view to show view with nice animation.

Every thing is working fine and i am pretty much aware about this control.

But till now I am using manually code to add view in iCarousel view and show it on screen but now as per my new requirement are i got 3 to 4 new themes which i need to show iCarousel view so below is my question related to that view.

  1. How can i load different views from XIB?

  2. How can i load view from Storyboard TO iCarousel view?

Currently i am using below method to add view manually.

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

Please help me to achieve this through Storyboard/ XIB.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
BhaviDev
  • 194
  • 2
  • 10

2 Answers2

4

Create a iCarousel with a inverted wheel type using a custom view from nib

   @IBOutlet weak var viewCarousel: iCarousel!

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

iCarousel DataSource

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
    }

}

Objectiv 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 DataSource

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

here CustomView is my subclass of UIView and "customView" is UIView Xib name i hope this help you

More info :- https://github.com/nicklockwood/iCarousel

Harshal Valanda
  • 5,331
  • 26
  • 63
  • you mean want to show two views at the same using i caurosal? – Pavankumar Feb 01 '17 at 10:05
  • With above code its working fine with NIB but i have one question how can we achieve this with storyboard? ? – BhaviDev Feb 06 '17 at 08:50
  • you can create a viewController and return viewController's view as a carousel View. you can also use a cell and return cell.containView – Harshal Valanda Feb 06 '17 at 09:06
  • For Swift add frame to the view you are returning from viewForItemAt method view.frame = CGRect(x: 0, y: 0, width: 200, height: 200) And for obj. c add view.frame = CGRectMake(0, 0, 200.0f, 200.0f); – Apple_Magic Mar 20 '20 at 17:43
0

Try this :

-(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;
    }
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • But I dont want any initlization in thas method want to creat same like tableview custom cell and just assign data to that label and image not to create and add through this method. – BhaviDev Feb 01 '17 at 10:09
  • Even i dont want to set any frame as its autolayout enable so it will take from storyboard it self. – BhaviDev Feb 01 '17 at 10:11