How to Load Icarousel View from Storyboard or Xib

How to load iCarousel view from storyboard or xib?

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

Load view from an external xib file in storyboard

My full example is here, but I will provide a summary below.

Layout

Add a .swift and .xib file each with the same name to your project. The .xib file contains your custom view layout (using auto layout constraints preferably).

Make the swift file the xib file's owner.

enter image description here
Code

Add the following code to the .swift file and hook up the outlets and actions from the .xib file.

import UIKit
class ResuableCustomView: UIView {

let nibName = "ReusableCustomView"
var contentView: UIView?

@IBOutlet weak var label: UILabel!
@IBAction func buttonTap(_ sender: UIButton) {
label.text = "Hi"
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

guard let view = loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view
}

func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
}

Use it

Use your custom view anywhere in your storyboard. Just add a UIView and set the class name to your custom class name.

enter image description here

How to implement UI like iCarousel Rotary type, Objective-C?

Finally I did with custom view and I have achieved UI as I need it. Here, I am providing my code for someone needy.

- (void)viewDidLoad
{
[super viewDidLoad];
_iCarouselItems = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++)
{
[_iCarouselItems addObject:@(i)];
}
self.iCarouselView.dataSource = self;
self.iCarouselView.delegate = self;
_iCarouselView.type = iCarouselTypeCustom;
dispatch_async(dispatch_get_main_queue(), ^{
[_iCarouselView reloadData];
});
}

#pragma mark iCarousel methods
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [_iCarouselItems count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling

if (view == nil)
{
NSLog(@"viewForItemAtIndex is %ld",(long)index);
//don’t do anything specific to the index within
//this `if (view == nil) {…}` statement because the view will be
//recycled and used with other index values later

view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200.0f)];
// ((UIImageView *)view).image = [UIImage imageNamed:@"smiley-400x400.jpg"];

view.contentMode = UIViewContentModeCenter;
view.backgroundColor = [UIColor whiteColor];
view.layer.cornerRadius = 8;
view.layer.masksToBounds = true;
view.layer.borderColor = [UIColor grayColor].CGColor;
view.layer.borderWidth = 2;

label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:50];
label.tag = 1;
[view addSubview:label];

}
else
{
//get a reference to the label in the recycled view
label = (UILabel *)[view viewWithTag:1];
}
label.text = [_iCarouselItems[index] stringValue];
return view;
}

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
CGFloat offsetFactor = [self carousel:carousel valueForOption:iCarouselOptionSpacing withDefault:0.3]*carousel.itemWidth;

CGFloat zFactor = 400.0;
CGFloat normalFactor = 0;
CGFloat shrinkFactor = 100.0;
CGFloat f = sqrt(offset * offset + 1)-1;

transform = CATransform3DTranslate(transform, offset * offsetFactor, f * normalFactor, f * (-zFactor));
transform = CATransform3DScale(transform, 1 / (f / shrinkFactor + 1.0), 1 / (f / shrinkFactor + 1.0), 1.0 );

return transform;
}

- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index
{
return true;
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
NSLog(@"Selected carouselindex is %ld",(long)index);
}

- (NSInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
//note: placeholder views are only displayed on some carousels if wrapping is disabled
return 0;
}

enter image description here

Reuse Identifier on iCarousel

You could give your imageView a tag like this:

let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0 , width: 295, height: 295)
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
imageView.tag = 2

Then you can retrieve the imageView from the cell like this:

if let imageView = cell.viewWithTag(2) as? UIImageView {
imageView.transform = moveImg
}

OR

You could filter through the subviews of the cell and find the first subview that can be cast to a UIImageView. Although this isn't very robust, consider creating a Class and XIB for this cell.

cell.subviews.flatMap { $0 as? UIImageView }.first?.transform = moveImg


Related Topics



Leave a reply



Submit