Automatically change background image in swift - ios

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");
}

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.

iOS QOS_CLASS_USER_INITIATED freezes while interacting with UI (Swift)

Excuse me, if it is a stupid question, but i'm just trying to understand GCD.
I tried to do a simple thing: UITableViewController with one UITableViewCell and UIProgressView on the cell. And this progressView should update every second.
In cellForRowAtIndexPatch I did this
let cell = tableView.dequeueReusableCellWithIdentifier("onlyCell", forIndexPath: indexPath) as! OnlyTableViewCell
cell.runChanges()
return cell
In runChanges function I just create timer
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("doSomething"), userInfo: nil, repeats: true)
And doSomething looks like this:
func doSomething() {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue),0)) {
if self.i >= 100 {
self.timer!.invalidate()
return
}
++self.i
dispatch_async(dispatch_get_main_queue()) {
self.prgrs.progress = Float(self.i)/100.0
print(self.i)
}
}
}
So I thought that it would work all the time, while I interact (maybe) with the UI. And it works quite good while I don't touch it. But when I interact with UI, for example, scroll UITableView and hold my finger(mouse in simulator) on the screen, it freezes.
I thought if now it is 22% and I will scroll it and hold my finger for 10 seconds it maybe should continue from about 32% but it shows 23%.
Sorry for my English and stupidness.
Images and source here.
I had a similar issue with NSTimer, adding your timer to mainRunLoop should fix it.
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("doSomething"), userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

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?

better way to set alpha for a view after a few seconds in apple watch / watchkit in swift

I am trying to set alpha with animation to image view in swift for apple watch application.
to do that i have tried code below
UIView.animateWithDuration(1.5, animations: {
self.splashscreenImage.alpha = 1.0
return
})
but its iving error as
'WKInterfaceImage' does not have a member named 'alpha'
so i implemented code below in willActivate function
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("splashscreenAlpha"), userInfo: nil, repeats: false)
and
func splashscreenAlpha() {
self.splashscreenImage.setAlpha(0.0)
}
but in this scenario, view is like invisible, like image view has a black background. But i would like it to gone with animation. How can i do that?
You should try using
UIView.animateWithDuration(1.5, animations: {
self.splashscreenImage.setAlpha(1.0)
return
})
to make alpha animation.

Resources