How to animate content offset in collection view in iOS? - ios

I am trying to move my collection view vertically 5px at a time using content offset using time interval trigger but it's not animating, my code for that is
timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(animateScroll), userInfo: nil, repeats: true)
#objc func animateScroll() {
self.previousOffset = self.collectionView.contentOffset.y
self.collectionView.contentOffset.y = self.previousOffset + 5
}

just use method setContentOffset:animated:

Related

Hiding removing SubView from a different file in swift

I am loading a progress view from this file into a different view controller, it is displaying fine but It will not remove. I've tried isHidden, removeFromSuperview(), making the alpha=0, but nothing is working. How can I get rid of it?
func timerAction(){
let progressHUD = ProgressHUD(text: "Taking Reading")
if(timerCounter <= 20){
self.vc!.view.addSubview(progressHUD)
timerCounter += 1
} else {
timer.invalidate()
timerCounter = 0
progressHUD.removeFromSuperview()
}
}
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
Each time the timer is called a new subview is added to the view controller. Add the subview 'progressHUD' just once and update it instead.

Pause timer when scroll view's slide is being touched?

I am trying to automatically scroll slides in my scroll view. I am actually doing this for my onboarding screens.
However I want the timer to pause for a while when user touches the slides and start the same again when he removes his finger.
I am doing something like this but this doesn't work for the case I am asking:
in viewDidLoad -
timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)
#objc func autoScroll() {
let totalPossibleOffset = CGFloat(topImages.count - 1) * self.view.bounds.size.width
if offSet == totalPossibleOffset {
//offSet = 0 // come back to the first image after the last image
//timer.invalidate()
}
else {
offSet += self.view.bounds.size.width
}
DispatchQueue.main.async() {
UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.scrollView.contentOffset.x = CGFloat(self.offSet)
}, completion: nil)
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(page)
self.offSet = page * scrollView.frame.size.width // this updates offset value so that automatic scroll begins from the image you arrived at manually
}
Also I have a second question: How do I start the timer interval again when user manually slides to other other. Right now, when the user slides to another slide before 4 seconds (as 4 seconds is required time to slide to another slide), say 2 seconds, he will slide to next page there after 4-2 = 2 seconds instead of 4 seconds as expected.
I think you should add some flag like
var isSlideTouched = false
In gesture recognizer add
isSlideTouched = true
And some code in autoScroll()
#objc func autoScroll() {
if isSlideTouched {
isSlideTouched = false
return
}

Animating Values in UILable IOS Swift

i want to display values 1-10 repeatedly in UILable with animation user should be able to see the fluctuation of the values slowly how can i achieve this functionality ?
You can use :
var timer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "change_label", userInfo: nil, repeats: true)
func change_label()
{
count = count+1
label.text = String(format: "%d",count)
}

auto scroll using animated UIScrollView.setContentOffset with NSTimer

I'm writing a music player using Swift, but when I'm tried to auto scroll the lyric, I found something weird.
I use a NSTimer to trigger the scroll:
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateTime", userInfo: nil, repeats: true)
func upadate is:
let offset = lyricScrollView.contentOffset
lyricScrollView.setContentOffset(CGPoint(x: offset.x, y: offset.y + 25), animated: true)
but when I change the time interval to 0.1 like this:
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateTime", userInfo: nil, repeats: true)
I found that my scrollview doesn't scroll 10x faster than before, instead, it scrolls a little every 0.1 seconds, not 25 points every 0.1 seconds. If I set animated to false, the problem is gone.
Could someone tell me why this happens?

Automatically change background image in swift

How would I automatically change the background image using a timer i.e. after 5 seconds one background image transitions to another? How do you do this in Swift?
NSTimer(timeInterval: 0.5, target: self, selector: "ChangeImage", userInfo: nil, repeats: true)
and in Action change Image like
func ChangeImage()
{
NSLog("asdasd");
imgView.image = UIImage(named: "img.png");
}

Resources