Swift 4 auto slide in page view controller with help of timer - ios

I am trying to create auto sliding in page view controller using swift 4. Here, I tried below code but I am getting result, it's repeating first index of image. I need to know, how to implement for move on next index?
// Image array
var name = ["one.png","two.png","three.png","four.png"]
// Timer in viewdidload()
tTime = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true)
// Timer function call (The problem is here...)
#objc func changeSlide() {
let pageContentViewController = self.viewControllerAtIndex(index: 0)
self.pageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}

// Image array
var name = ["one.png","two.png","three.png","four.png"]
var currentIndex = -1
// Timer in viewdidload()
tTime = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true)
// Timer function call
#objc func changeSlide()
{
currentIndex = (currentIndex == name.count - 1) ? 0 : currentIndex + 1
let pageContentViewController = self.viewControllerAtIndex(index: currentIndex)
self.pageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}

Related

How to work with Page control using NSTimer for automatic scrolling in swift4

I am new to iOS.I am trying to do the page control using NSTimer for automatic scrolling and also I want to change the shape of dots in page control .I need page control with rectangle boxes.If anyone helps me ,would be great.
Try this
var index = 0
let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
func update() {
index = index + 1
let x = CGFloat(index) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)
}
I have an example for collection view auto scroll like ganna.com in ios or android application.
For Swift 4.2
code:
var timer: Timer?
#IBOutlet weak var collectionView : UICollectionView!
#IBOutlet weak var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.reloadData()
startTimer()
}
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(scrollAutomatically), userInfo: nil, repeats: true)
}
#objc func scrollAutomatically(_ timer1: Timer) {
for cell in collectionView.visibleCells {
if banner.count == 1 {
return
}
let indexPath = collectionView.indexPath(for: cell)!
if indexPath.row < (banner.count - 1) {
let indexPath1 = IndexPath.init(row: indexPath.row + 1, section: indexPath.section)
collectionView.scrollToItem(at: indexPath1, at: .right, animated: true)
pageControl.currentPage = indexPath1.row
}
else {
let indexPath1 = IndexPath.init(row: 0, section: indexPath.section)
collectionView.scrollToItem(at: indexPath1, at: .left, animated: true)
pageControl.currentPage = indexPath1.row
}
}
}
banner.Count => a number of cells containing a collection view
I hope you find this useful.

how to auto scroll with timer in page view controller in swift 3?

Hi I just want to scroll pages in page view controller automatically with timer in swift 3 - so after I read similar questions I used this Extension code to auto scroll in page view controller But It Didn't worked for me
here is the Extension Code
extension pageVC2 {
func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
if let currentViewController = viewControllers?[0] {
if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
}
}
}
}
and then I tried Another answer so I used This code But I will get Error with ScrollView
Here is the second way that I tried
func moveToNextPageTwoSecond (){
let pageWidth:CGFloat = CGRectGetWidth(self.scrollView.frame)
let maxWidth:CGFloat = pageWidth * 4
let contentOffset:CGFloat = self.scrollView.contentOffset.x
var slideToX = contentOffset + pageWidth
if contentOffset + pageWidth == maxWidth{
slideToX = 0
}
self.scrollView.scrollRectToVisible(CGRectMake(slideToX, 0, pageWidth, CGRectGetHeight(self.scrollView.frame)), animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(pageVC2.moveToNextPageTwoSecond), userInfo: nil, repeats: true)
}

Move UISlider automatically in Swift

I want to move a UISlider from minValue to maxValue in a loop when you hit a button and stop it at the current position when hitting the button again, I want to use Swift.
The main problem i got is that the function slider.setValue() is way to fast, I want the animation more slowly.
#IBAction func setSliderValue(_ sender: UIButton){
slider.setValue(100, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)
}
You can use time to Automatically Move slider.
Create NSTimer variable in global scope:
Also create one Bool variable in global scope for checking if Need to revise it.
var mytimer : NSTimer?
var reversing : Bool?
start timer when you want to animate it:
reversing = NO;
mytimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
On tImer Action, You can write code to change value of Slider:
var sliderrange = slider.maxValue - slider.minValue;
var increment = sliderrange/100;
var newval = slider.value;
newel = reversing ? (slider.value - increment) : (slider.value + increment);
if(newval >= slider.maxValue)
{
reversing = true;
newval = newval - 2*increment;
}
else if(newel <= 0)
{
reversing = false;
}
slider.setValue(newval, animated: true)
On Button Action, You can just stop the timer,
if mytimer != nil {
mytimer!.invalidate()
mytimer = nil
}
This code is working, though the slider only moves from left to right, not from right to left when he reached maximumValue, it should move from left to right and then right to left until the invalidated.
#IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
/*slider.setValue(100, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)*/
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment <= slider.maximumValue)
{
slider.setValue(newval, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)
}
else if (Increment >= slider.minimumValue)
{
slider.setValue(newval, animated: true)
}
}

Starting timer on button click in swift

Ive created a timer in swift to move a UISlider from one end to another again and again when a button is pressed. But I'm always getting a breakpoint at the timer line, although everything should be right.
#IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment >= slider.maximumValue)
{
slider.setValue(newval, animated: true)
}
else
{
slider.setValue(0, animated: true)
}
}
The check of your function is incorrect.
func timerAction(){
let range = slider.maximumValue - slider.minimumValue
let increment = range/100
let newval = slider.value + increment
if newval <= slider.maximumValue {
slider.setValue(newval, animated: true)
} else {
slider.setValue(slider.minimumValue, animated: true)
}
}
Also, in your event handler, should invalidate the timer (if it's not nil) first before instancing a new one.
Its working now with the following code, though the slider is only moving from left to right until it gets invalidated.
#IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
/*slider.setValue(100, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)*/
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment <= slider.maximumValue)
{
slider.setValue(newval, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)
}
else if (Increment >= slider.minimumValue)
{
slider.setValue(newval, animated: true)
}
}
Hope this helps if someone else needs help on a task "starting a count down timer on a button click".
Timer itself has to be declared inside of the button code and additionally create a Obj-C function to update the timer that will be connected trough the #selector.
class ViewController: UIViewController {
var myTimer = Timer()
var secondsToCount = 100
#IBAction func buttonTapped(_ sender: UIButton) {
myTimer.invalidate()
myTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer() {
secondsToCount -= 1
timerDisplayed.text = String(secondsToCount)
}
}
timerDisplayed is a label that I connected to see the value of the timer on the screen of the app.
SWIFT 5.3 & Xcode 12.0.1

Stop typewriter animation and show full text in swift

I have the following code to animate the typewriter text in UITextView.
The problem is after the animation starts from the beginning character by character, I want it able to show full text after a button is pressed.
What is the best way to show up the full text after animation starts?
var myText = Array("".characters)
var myCounter = 0
var timer:Timer?
var TextNumber = 0
func fireTimer(){
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(Main1.typeLetter), userInfo: nil, repeats: true)
}
func typeLetter(){
if myCounter < myText.count {
myTypeWriter.text = myTypeWriter.text! + String(myText[myCounter])
let randomInterval = Double((arc4random_uniform(8)+1))/190
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: randomInterval, target: self, selector: #selector(Main1.typeLetter), userInfo: nil, repeats: false)
} else {
timer?.invalidate()
}
myCounter += 1
}
#IBAction func showFullText(){
//What should i put here?
}
You can do something like this, i.e invalidate the time and convert array into string and place this text
#IBAction func showFullText(){
timer?.invalidate()
let strFromArray = myText.joinWithSeparator(" ")
//In Swift 3, joinWithSeparator becomes joined(separator:)
myTypeWriter.text = strFromArray
}

Resources