How to implement Auto sliding iCarousel in iOS? - ios

I have implemented a basic slider using iCarousel iOS library but i dont know how to make it as a Auto slider instead of manual swipe sliding.
Here is my sample project code Pls help me
Code
When i searched in google i got this link,but i didnt understand anything
https://github.com/nicklockwood/iCarousel/issues/84

This solved my problem
in ViewDidAppear() i have added following line
-(void)viewDidAppear:(BOOL)animated
{
sliderCount=0;
[super viewDidAppear:animated];
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(runMethod) userInfo:nil repeats:YES];
}
-(void)runMethod
{
[self.carousel scrollToItemAtIndex:sliderCount animated:YES];
if(sliderCount== 7)
{
sliderCount=0;
}
else
{
sliderCount++;
}
}

In your ViewDidLoad method in your Viewcontroller.
- (void)viewDidLoad
{
[super viewDidLoad];
//configure carousel
carousel.type=iCarouselTypeCylinder;
carousel.autoscroll=0.4;
}

Looking at the github link, I assume you want to autoscroll to an item upon view.
You have defined the function
- (void)scrollToItemAtIndex:(NSInteger)index animated:(BOOL)animated
However this isn't part of the iCarousel delegate, its already defined as part of the class, therefore you just need to call it. Putting it in viewDidAppear would seem appropriate if you wanted it to auto scroll to a particular index when your view controller was presented to the user. Simply add the method below.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// The index of 3 was just chosen as an example
NSInteger chosenIndex = 3;
[self.carousel scrollToItemAtIndex:chosenIndex animated:YES];
}

- (void)viewDidLoad
{
[super viewDidLoad];
carousel.type=iCarouselTypeCylinder;
carousel.autoscroll=0.8;
[carousel reloadData];
}

Related

iCarousel auto scroll effect

I have two concerns to ask.
I would like this scrolling be infinite. I do not want it to stop. I am using iCarousel
My second concern is, while time is passing, scrolling gets
faster with the following code snippet. I would like to keep same speed.
-(void)viewDidAppear:(BOOL)animated
{
[carousel scrollByNumberOfItems:4 duration:20]
}
With the latest version of iCarousel as of January 02, 2015, you could add the following code in your ViewDidLoad method in your Viewcontroller.
- (void)viewDidLoad
{
[super viewDidLoad];
//configure carousel
carousel.type=iCarouselTypeCylinder;
carousel.autoscroll=0.4;
}
Use the autoscroll property instead (just set it to the speed you want)
if u give negative value it moves right to left (the code like this)
- (void)viewDidLoad
{
[super viewDidLoad];
carousel.type=iCarouselTypeCylinder;
carousel.autoscroll= - 0.4;
}

Method called only in viewDidApper but not in viewDidLoad, why?

I created a method that sets the title of a button based on a value.
This method needs to be called when opening the viewController and maybe refreshed when the controller appears again.
So i created the method and I called that method in viewDidLoad and viewDidApper but it seems to be called only when I change page and turn back to the view controller.
Why?
My code is
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self controlloRichieste];
......
}
-(void)viewDidAppear:(BOOL)animated{
[self controlloRichieste];
}
-(void)controlloRichieste{
//Numero richieste di contatto
NSString *numeroRichieste = #"1";
if([numeroRichieste isEqual:#"0"]){
[_labelRequestNumber setTitle:#"Nessuna" forState:UIControlStateNormal];
} else {
_labelRequestNumber.titleLabel.text = numeroRichieste;
_labelRequestNumber.tintColor = [UIColor redColor];
}
//Fine Numero richieste di contatto
}
You can also move that code to viewWillAppear so that it gets called each time it appears.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self controlloRichieste];
}
I see the problem now, try the other way around
-(void)controlloRichieste{
//Numero richieste di contatto
NSString *numeroRichieste = #"1";
if([numeroRichieste isEqual:#"0"]){
[_labelRequestNumber setTitle:#"Nessuna" forState:UIControlStateNormal];
} else {
_labelRequestNumber.tintColor = [UIColor redColor];
[[_labelRequestNumber titleLabel]setText:numeroRichieste];
}
//Fine Numero richieste di contatto
}
Change set the button color, before you change its titleLabel's text
I created a demo PROJECT for you, hope it's helpful!
When you open view first time the viewDidLoad is called and the viewDidAppeare.
The viewDidAppeare is called every time when the view is opened, when you push or present other view controller and go back to the maine one viewDidAppeare is called.
You should call:
[super viewDidAppear:animated];
The viewDidLoad is called just when the view is loaded and after that when it's deallocated and it needs to be allocated again. So mostly when you push or present other view controller and go back to the maine one viewDidLoad is not called.

How do I keep scroll position when navigating in a navigation controller?

have been looking around, but I have not found an answer that helped me out. I am wondering how I can keep the scroll position in a scrollview when I move forwards and backwards in a navigation controller. Currently, when I go to another view in the navigation controller, the scroll view resets and is at the top when I go back. Any code would be greatly appreciated. Sorry if my explanation was not good enough. So far this is what I have, I know that this won't work, but what should I do?
- (void)viewWillAppear:(BOOL)animated {
scrollView.contentOffset.y = scrollSave;
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
CGFloat scrollSave = scrollView.contentOffset.y;
[super viewWillDisappear:animated];
}
The code you have above shouldn't even compile. You could save this scroll position in another variable, but generally the scroll position shouldn't be resetting itself anyway. You should remove any code that is trying to manipulate the content offset at all, and see if it restores it to the correct scroll position upon returning to the view.
With your current approach, scrollSave is a local variable declared in viewWillDisappear so you cannot access the value in viewWillAppear. You will need to create an instance variable or a property. (If the view controller is still in memory, it should maintain the contentOffset so this might not be necessary)
#interface MyViewController ()
#property (nonatomic) CGPoint scrollSave;
#end
#implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
[scrollView setContentOffset:self.scrollSave];
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
self.scrollSave = scrollView.contentOffset;
[super viewWillDisappear:animated];
}
Now there is an issue with persistence. If the you navigate back in the navigation controller, this view controller will be released from memory. If you need to save the users position then I would suggest using NSUserDefaults.
#interface MyViewController ()
#property (nonatomic) NSUserDefaults *defaults
#end
#implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
CGPoint scrollSave = CGPointFromString([_defaults objectForKey:#"scrollSaveKey"]);
scrollView.contentOffset = scrollSave;
[super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[_defaults setObject:NSStringFromCGPoint(scrollView.contentOffset) forKey:#"scrollSaveKey"];
[_defaults synchronize];
[super viewWillDisappear:animated];
}
Kyle's answer should get you storing the variable okay, but in regards to assigning the contentOffset.y, trying animating the scrollview.
[scrollView scrollRectToVisible:
CGRectMake(scrollView.frame.origin.x, scrollSave.y, scrollView.frame.size.width, scrollView.frame.size.height)
animated:NO];
That code would go into viewWillAppear.

how to Reaload viewDidLoad in a method , Not call again such as [self viewDidLoad ];

i want to reload ViewdidLoad in a method But now want to call again like [self viewDidLoad ];
is this possible?
Instead of calling viewDidLoad: make another method (newMethod) and move all the code in it that needs to be called then from
- (void)viewDidLoad{
[super viewDidLoad];
[self newMethod];
}
Then from your code where you want to call viewDidLoad: call
[self newMethod];
Swift version
func viewDidLoad() {
super.viewDidLoad()
self.newMethod()
}
Then from your code where you want to call viewDidLoad: call
self.newMethod()
Copy all code in - (void)viewDidLoad then paste in viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//paste your viewDidLoad codes
}
Yes you can call from any method but if you post your scenario then it is better to reply
[self viewDidLoad];

Show UIPickerView when editing UITextField

I want to show a UIPickerView and not the keyboard when I edit a UITextField. I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
depCityField.inputView=pickerDestinations;
comeCityField.inputView=pickerDestinations;
}
It doesn't work, but with UIDatePicker it works fine...
I have created an example programmatically for your reference. I really hope this helps you. This is the source.
Sounds like your textFields dont have the delegate set. You need to set the UIViewController as the delegate to the textFields.
Your viewDidLoad should probably look like this:
- (void)viewDidLoad()
{
[super viewDidLoad];
depCityField.delegate = self;
comeCityField.delegate = self;
depCityField.inputView=pickerDestinations;
comeCityField.inputView=pickerDestinations;
}

Resources