2017-02-01 168 views
2

我正在使用一個第三方控件iCarousel視圖以良好的動畫顯示視圖。如何從故事板或xib加載iCarousel視圖?

每件事情都很好,我很清楚這個控件。

但是到現在爲止,我使用手動代碼在iCarousel視圖中添加視圖,並顯示在屏幕上,但現在按我的新要求,是我得到3,而我需要顯示iCarousel視圖,以便以下4周新的主題是我的問題與這個觀點有關。

  1. 我如何可以加載XIB不同的看法?

  2. 如何我可以加載Storyboard以期iCarousel看法?

目前我正在使用下面的方法手動添加視圖。

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

請幫我通過Storyboard/ XIB實現這一目標。

+0

這你需要哪種解決方案? – KKRocks

+0

詳細闡述了這一點:1.我如何從XIB載入不同的視圖? 2.如何從故事板查看iCarousel VIEW? – KKRocks

+0

需要在故事板或xib的卡集視圖中添加自定義視圖。 – BhaviDev

回答

3

使用定製視圖從筆尖創建具有倒輪類型iCarousel

@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Ç

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

這裏CustomView是我的UIView的子類, 「customView」 是的UIView XIB名 我希望這會幫助你

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

+0

你的意思是想用同樣的方法顯示兩個視圖使用我的caurosal? – Pavankumar

+0

由於我不太瞭解迅速,你可以引導我相同的對象c? – BhaviDev

+0

有了上面的代碼,它可以很好地與NIB一起工作,但我有一個問題,我們如何才能實現這個故事板? ? – BhaviDev

0

試試這個:

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

但我不希望在thas方法中的任何初始化想創建像tableview自定義單元格相同,只是分配數據該標籤和圖像不能通過此方法創建和添加。 – BhaviDev

+0

即使我不想設置任何框架作爲其自動佈局啓用,所以它會從故事板它自己。 – BhaviDev