我正在使用一个第三方控件iCarousel
视图来显示带有漂亮动画的视图。
每件事情都很好,我非常清楚这种控制。
但到目前为止,我正在使用手动代码在iCarousel
视图中添加视图并在屏幕上显示,但现在根据我的新要求,我需要显示3到4个新主题{{1}查看以下是与该视图相关的问题。
如何从iCarousel
加载不同的视图?
如何将视图从XIB
加载到Storyboard
视图?
目前我正在使用以下方法手动添加视图。
iCarousel
请通过viewForItemAtIndex:(NSInteger)index resuingView:(UIView*) view
帮助我实现这一目标。
答案 0 :(得分:3)
使用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 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
}
}
目标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;
}
这里CustomView是我的UIView的子类,“customView”是UIView Xib的名字 我希望这能帮到你
答案 1 :(得分: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;
}