2017-02-01 1 views
2

J'utilise un contrôle tiers iCarousel pour afficher la vue avec une belle animation.Comment charger la vue iCarousel à partir du storyboard ou du xib?

Tout fonctionne bien et je suis assez au courant de ce contrôle. Mais jusqu'à maintenant j'utilise le code manuellement pour ajouter la vue dans iCarousel voir et le montrer sur l'écran mais maintenant selon ma nouvelle exigence sont j'ai 3 à 4 nouveaux thèmes que je dois montrer iCarousel voir ainsi est ci-dessous ma question lié à cette vue.

  1. Comment puis-je charger des vues différentes de XIB? Comment puis-je charger la vue de Storyboard à iCarousel?

Actuellement j'utilise la méthode ci-dessous pour ajouter une vue manuellement.

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

S'il vous plaît aidez-moi à atteindre cet objectif grâce à Storyboard/ XIB.

+0

qui type de solution dont vous avez besoin? – KKRocks

+0

élaborer plus à ce sujet: 1. COMMENT PUIS-JE CHARGER DIFFÉRENTES VUES DIFFÉRENTES DE XIB? 2. COMMENT PUIS-JE CHARGER VUE DE L'HORLOGE À LA VUE iCarousel? – KKRocks

+0

Besoin d'ajouter une vue personnalisée dans la vue icarousel du storyboard ou du xib. – BhaviDev

Répondre

3

Créer une iCarousel avec un type de roue inversée en utilisant une vue personnalisée de 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; 
} 

ici CustomView est ma sous-classe de UIView et "CustomView" est UIView nom Xib J'espère que cette aide vous

Plus d'info: - https://github.com/nicklockwood/iCarousel

+0

vous voulez montrer deux vues en même temps en utilisant i caurosal? – Pavankumar

+0

Comme je ne suis pas très au courant dans swift pouvez-vous me guider même dans Obj c ??? – BhaviDev

+0

Avec le code ci-dessus fonctionne très bien avec NIB mais j'ai une question comment pouvons-nous y parvenir avec storyboard? ? – BhaviDev

0

Essayez ceci:

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

Mais je ne veux pas initlization dans la méthode thas voulez créer même comme cellule personnalisée tableview et juste assigner des données à cette étiquette et l'image ne pas créer et ajouter à travers cette méthode. – BhaviDev

+0

Même si je ne veux pas mettre n'importe quel cadre comme son auto-mise en veille, il va prendre du storyboard lui-même. – BhaviDev