button.setTitle blanks out image view - ios

Can anyone tell me why setting the button.setTitle(string, forState) function blanks out the UIImage?
If I comment out both lines that use the button.setTitle, the animation runs fine. When the lines are there, the animation disappears when I press the button.
The var cryButton: UIButton and the function UpdateImage refer to the same button, but I tried adding a second button and referencing the title in that, and it had the same effect. It blanks out the image.
I'm stumped :)
import UIKit
class ViewController: UIViewController {
var counter = 1
var numImages = 8
var toggle = true
var timer = NSTimer()
#IBOutlet weak var babyImage: UIImageView!
#IBOutlet weak var cryButton: UIButton!
#IBAction func UpdateImage(sender: AnyObject) {
if toggle == true {
timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("doanimation"), userInfo: nil, repeats: true)
cryButton.setTitle("Stop crying", forState: UIControlState.Normal)
toggle = false
} else {
timer.invalidate()
babyImage.image = UIImage(named: "WaWa1.png")
cryButton.setTitle("Wa-Waaaaa!", forState: UIControlState.Normal)
toggle = true
}
}
func doanimation() {
babyImage.image = UIImage(named: "WaWa\(counter).png")
if (counter == (numImages)) {
counter = 1
} else {
++counter
}
}
override func viewDidLayoutSubviews() {
babyImage.center = CGPointMake(babyImage.center.x - 400, babyImage.center.y)
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(2, animations: { () -> Void in
self.babyImage.center = CGPointMake(self.babyImage.center.x + 400, self.babyImage.center.y)
})
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

I finally find the problem. It is not caused by the timer, the timer is totally fine. the problem is caused by viewDidLayoutSubviews being called when you set the button title. You are using auto layout, and you are using the intrinsic content size for the button, right? And since the two title has different length so when you set the title, the button's frame will be changed, hence it will be layout again and then viewDidLayoutSubviews will be called. And you set the center value of the imageView inside viewDidLayoutSubviews, so basically when you set the title the image will move 400 points left to its original spot which I guess is out of the screen. Please note: if the image has different size, when you call imageView.image = UIImage(named: "image.png") will also call viewDidLayoutSubviews. I think all the images you are using has same size, thats why when the timer runs viewDidLayoutSubviews was not called. Here is a good answer which explains when does viewDidLayoutSubviews get called. I will let yourself to figure out the proper solution, OK? :)

Actually there is a better way to animate a UIImageView with an array of images, you could probably try this and remove the timer.
babyImage.animationImages = #[image1,image2,image3,image4,image5,image6,image7,image8]
babyImage.animationDuration = 1.6
// There are 8 images, so each image occupies 1.6 / 8 = 0.2 secs
babyImage.startAnimating()
// Use babyImage.stopAnimating() to stop the animation

Related

Thread 1 Fatal error: Index out of range ImageView [duplicate]

This question already has answers here:
Swift Array - Check if an index exists
(12 answers)
Closed 3 years ago.
I'm having issues trying to figure out why am I getting Thread 1 Fatal error: Index out of range on my app. My app displays 8 images show to start image slideshow. For some reason I'm getting this error I can't figure it out. Is there a way to remove this error? Can anyone help me?
Here is my code balow and also a screenshot link here :
var images = [UIImage]()
var counter = 2
var time = Timer()
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var ImageView: UIImageView!
#IBOutlet weak var Slider1: UISlider!
#IBAction func Slider(_ sender: UISlider) {
_ = 0
let value = Int(sender.value)
ImageView.image = images[value]
}
#IBAction func NextButton(_ sender: Any) {
Slider1.value += 1
ImageView.image = images[Int(Slider1.value)]
self.ImageView.animationImages = self.images
self.ImageView.animationDuration = 15.0
self.ImageView.animationRepeatCount = 0
self.ImageView.startAnimating()
UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)
}
#IBAction func PrevButton(_ sender: Any) {
Slider1.value -= 1
ImageView.image = images[Int(Slider1.value)]
self.ImageView.animationImages = self.images
self.ImageView.animationDuration = 15.0
self.ImageView.animationRepeatCount = 0
self.ImageView.startAnimating()
UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)
}
//Set Status Bar to light content (white)
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
//Set Navigation Bar color Example Home, Back button
self.navigationItem.backBarButtonItem?.tintColor = UIColor.white;
time = Timer.scheduledTimer(withTimeInterval: 8, repeats: true) { _ in
self.NextButton(self);
}
super.viewDidLoad()
setup()
images = [#imageLiteral(resourceName: "MainImage1.jpg"), #imageLiteral(resourceName: "MainImage2.jpg"), #imageLiteral(resourceName: "MainPage3.jpg"), #imageLiteral(resourceName: "MainImage4.jpg"), #imageLiteral(resourceName: "MainImage5.jpg"), #imageLiteral(resourceName: "MainImage6.jpg"), #imageLiteral(resourceName: "MainImage7.jpg"), #imageLiteral(resourceName: "MainImage8.jpg")]
sideMenus()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setup(){
self.navigationController?.navigationBar.tintColor = UIColor.white
}
override var prefersStatusBarHidden: Bool{
return false
}
var navigationBarAppearace = UINavigationBar.appearance()
override func viewDidAppear(_ animated: Bool){
}
func sideMenus() {
if revealViewController() != nil {
menuButton.target = revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
revealViewController().rearViewRevealWidth = 275
revealViewController().rightViewRevealWidth = 160
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
}
The error is telling you that you're accessing an element outside the range of the images array. In viewDidLoad you set the images array with 8 elements. These elements have the range of 0-7. If you try to access an element outside that range, you get the out of range error that you're seeing.
Looking at your code, the problem is that in NextButton and PrevButton, you adjust the slider value and index, but you don't check to make sure you haven't gone past the array bounds. If you're on the first image and click the Prev button, you're out of range. If you're on the last image and click the Next button, you're out of range.
What you have to do is make sure you don't have a negative index in the array or have an index that's greater than or equal to the number of items in the array. From a user interface standpoint, the best solution is to disable the Prev button when you're on the first image and disable the Next button when you're on the last image.
Since you asked for an example, here's some code to put in nextButton after incrementing the slider value:
if slider1.value >= images.count {
return
}
And the code for prevButton after decrementing the slider value:
if slider1.value < 0 {
return
}
Now the function will exit if the array index is out of range.
One last thing to check is that the images array is being filled in viewDidLoad. Set a breakpoint in viewDidLoad and step through the code. If there's a problem loading the MainImage.jpg files, the array is going to be empty, and you'll get out of range errors when you access the array.

UIGestureRecognizer on only one part of the screen

If you look into the image, you'll see a textview, button and a slider.
I have completed all the steps except for the last one which is, when I tap on the slider, it constantly disappear. I know it disappear because I implemented UITapGestureRecognizer.
I guess my question is, I want the slider to disappear everytime I tap anywhere on the screen but when I am using the slider, I dont want the slider to disappear which is happening now every time I release my tap.
I have tried implementing one more UITapGestureRecognizer in sizeRefont with a function to keep sizeRefont.isHidden false but when I do that, the slider will not disappear whenever I tap on the screen.
I tried putting sizeRefont.isHidden = false in sizeRefont action and it doesnt work either.
class ResizeController: UIViewController {
#IBOutlet weak var sizeRefont: UISlider!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissRefontSize(_:)))
view.addGestureRecognizer(tap)
}
#IBAction func sizeRefont(_ sender: AnyObject) {
let fontSize = CGFloat(sizeRefont.value)
textView.font = UIFont(name: textView.font!.fontName, size: fontSize * 30.0)
}
#IBAction func showSlider(_ sender: Any) {
sizeRefont.isHidden = false
}
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
if sender.location(in: sizeRefont){
sizeRefont.isHidden = false
} else {
sizeRefont.isHidden = true
}
}
}
There is an error on if sender.location(in: sizeRefont) where it says CGPoint is not convertible to Bool
Image
First thing you need to do is you need to Adjust the dismissRefontSize() method to to the following :
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: view)
If sizeReFont.frame.contains(location) {
// do nothing
}else {
sizeReFont.isHidden = true
}
}
The other thing you need to adjust is creating the tap recognized in your viewDidLoad() to the following:
let tap = UITapGestureRecognizer(target: self, action : #selector(dismissRefontSize(_:)))

My code seems to run too quickly

I am in the process of writing a Simon style memory game, the phase of the game where the program shows the user the current list of stuff to remember seems to run instantly.
The idea is to step through the list (in the code I have placed 1 of each item as debug data) and change the colour on screen for a set period then move to the next.
I thought using for each item in memory array and then call a simple procedure to check which one it is and then change colour for a set period then back to original.
The code I have added here will work if I put breaks in between the test change colour (grey) and the original colour. But for some reason the timer does not seem too work.
Any ideas ?
import UIKit
import Foundation
var gameisrunning = false
var playererror = false
var memoryArray = [Int]()
var currentScore = 0
var timer = NSTimer()
class ViewController: UIViewController {
#IBAction func startGameButton(sender: UIButton) {
if gameisrunning == false {
gameisrunning = true
memoryArray.append(1) //for debug
memoryArray.append(2) //for debug
memoryArray.append(3) //for debug
memoryArray.append(4) //for debug
print(memoryArray) //for debug
gameStart()
} else {
}
}
//these are to be implemented once i get the showing sequence sorted.
#IBAction func redButton(sender: UIButton) {
}
#IBAction func greenButton(sender: UIButton) {
}
#IBAction func yellowButton(sender: UIButton) {
}
#IBAction func blueButton(sender: UIButton) {
}
#IBOutlet weak var redLabel: UILabel!
#IBOutlet weak var greenLabel: UILabel!
#IBOutlet weak var yellowLabel: UILabel!
#IBOutlet weak var blueLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
func addAnotherItemToMemory () {
// adds another item to the memory
memoryArray.append(Int(arc4random_uniform(4)+1))
}
func gameStart () {
// main body of game
showPlayerTheMemory()
}
func showPlayerTheMemory () {
// go through list and highlight the colors one at a time
for eachItem in memoryArray {
self.showColor(eachItem)
}
}
func pauseForAWhile(length: Double) {
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: nil , userInfo: nil, repeats: false)
timer.invalidate()
}
func showColor(buttonItem: Int) {
//check to see which color, change to grey (test color) and back to original after a set time.
if buttonItem == 1 {
self.redLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
self.redLabel.backgroundColor = UIColor.redColor()
print(buttonItem) //for debug
} else if buttonItem == 2 {
self.greenLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
greenLabel.backgroundColor = UIColor.greenColor()
print(buttonItem) //for debug
} else if buttonItem == 3 {
self.yellowLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
yellowLabel.backgroundColor = UIColor.yellowColor()
print(buttonItem) //for debug
} else if buttonItem == 4 {
self.blueLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
blueLabel.backgroundColor = UIColor.blueColor()
print(buttonItem) //for debug
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
New relevant code changed to :
func colorChange (){
self.redLabel.backgroundColor = UIColor.redColor()
self.blueLabel.backgroundColor = UIColor.blueColor()
self.yellowLabel.backgroundColor = UIColor.yellowColor()
self.greenLabel.backgroundColor = UIColor.greenColor()
}
func showColor(buttonItem: Int, length: Double) {
//check to see which color, change to grey (test color) and back to original after a set time.
if buttonItem == 1 {
self.redLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
} else if buttonItem == 2 {
self.greenLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
} else if buttonItem == 3 {
self.yellowLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
} else if buttonItem == 4 {
self.blueLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
}
}
I have been scratching head all day trying to solve this issue which is baffling me. I have copied the new latest code in below, please discard code above.
I have four labels coloured red blue green and yellow. The array which has test data of 4 3 2 1 inside needs to step through each item - change the colour of the label for x secs then return it to normal colour. I have tried NSTimer, I have tried the current delay as in the code attached. Am I missing something as to where I place the code - should it be under viewdidload ??? I have tried for loops and the current code example shows switch in case it acted differently - it didnt !!
What happens basically is simultaneously all labels go grey (test colour right now) and then all go original colour after the x sec delay.
I need some help before I go insane. I honestly know it is something basic but I just cannot figure it out.
import UIKit
import Foundation
var gameisrunning = false
var playererror = false
var memoryArray = [Int]()
var currentScore = 0
class ViewController: UIViewController {
#IBAction func startGameButton(sender: UIButton) {
if gameisrunning == false {
gameisrunning = true
memoryArray.append(4) //for debug
memoryArray.append(3) //for debug
memoryArray.append(2) //for debug
memoryArray.append(1) //for debug
print(memoryArray) //for debug
gameStart()
} else {
}
}
//these are to be implemented once i get the showing sequence sorted.
#IBAction func redButton(sender: UIButton) {
}
#IBAction func greenButton(sender: UIButton) {
}
#IBAction func yellowButton(sender: UIButton) {
}
#IBAction func blueButton(sender: UIButton) {
}
#IBOutlet weak var redLabel: UILabel!
#IBOutlet weak var greenLabel: UILabel!
#IBOutlet weak var yellowLabel: UILabel!
#IBOutlet weak var blueLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
func addAnotherItemToMemory () {
// adds another item to the memory
memoryArray.append(Int(arc4random_uniform(4)+1))
}
func gameStart () {
// main body of game
showPlayerTheMemory()
}
func delayProg (){
//attempt 100093287492 to get a delay in program
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
self.blueLabel.backgroundColor = UIColor.blueColor()
self.yellowLabel.backgroundColor = UIColor.yellowColor()
self.greenLabel.backgroundColor = UIColor.greenColor()
self.redLabel.backgroundColor = UIColor.redColor()
}
}
func showPlayerTheMemory () {
// go through list and highlight the colors one at a time
for var i=0; i <= memoryArray.count-1; i++ {
self.showColor(memoryArray[i])
}
}
func showColor(buttonItem: Int) {
//check to see which color, change to grey (test color) and back to original after a set time.
switch (buttonItem) {
case 1:
self.redLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
case 2:
self.greenLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
case 3:
self.yellowLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
case 4:
self.blueLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
default:
print("error")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is an example of proper implementation of NSTimer()
var myTimer = NSTimer()
func startTimer() {
myTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "myFunction", userInfo: nil, repeats: true)
}
func myFunction() {
myTimer.invalidate()
//do other stuff
}
//the selector is "myFunction", this will be the name of the function that you wish to call every time the timer reaches its specified intervl
//the interval in this case is 10 seconds. In my experience NSTimer is good down to the second but is not precise enough beyond that
//repeats: true ... this will tell the timer to repeat its action consistently firing the selector each time the given time interval is reached. If repeat is set to false then the timer only fires once
//use myTimer.invalidate to stop the timer and to stop calling the selector.
be sure to invalidate your timer or set repeats: false to make sure it doesn't go forever. Make sure your selector is spelled exactly the same as your function. if your function is func myFunction() then the selector should be "myFunction". Make sure you specify a valid time interval, which is taken as seconds.

How can I detect which custom button was tapped in a bank of custom buttons in ios

I have a bank of 6 custom buttons which I implemented using images with user interaction enabled. I also added a tag to each switch. I attached a tap gesture recognizer to each button. I used a techniques described in
detect view was tapped
When I press any of the buttons I always get the last button that the gesture was attached to. Here is the code. Help would be appreciated.
import UIKit
class HomeViewController: UIViewController {
#IBOutlet var buttonImages: [UIImageView]!
var selectedOption = 0
#IBAction func tapped(sender: UITapGestureRecognizer) {
let buttonImage = sender.view!
println("Tag: \(buttonImage.tag) image: \(buttonImage)")
selectedOption = (sender.view?.tag)!
println("tapped: \(selectedOption)")
}
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self,
action: "tapped:")
tapGestureRecognizer.numberOfTouchesRequired = 1
tapGestureRecognizer.numberOfTapsRequired = 1
let x = UIImage(named:"TV normal.png") // replace with array
var tag = 0
for buttonImage in buttonImages {
buttonImage.addGestureRecognizer(tapGestureRecognizer)
buttonImage.image = x
buttonImage.tag = tag
tag++
}
// check
for buttonImage in buttonImages {
println("Tag: \(buttonImage.tag) image: \(buttonImage)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Your issue is due to the fact you are attaching the same UITapGestureRecognizer for each UIImage, but a gesture can be attached to only one UIView at the same time, that's why you always getting back the last one.
In your case, you just need to create a new UITapGestureRecognizer for each UIImage you are creating with the same target / action. This way, your method tapped will work.

Fade Animation for UIImages in Swift

I have a set of images that i would like to have fade in the background of my login screen. I cant find anything in swift that can do this. Is there a way that i would be able to?
Heres my current code:
override func viewDidLoad() {
super.viewDidLoad()
startAnimating()
// Do any additional setup after loading the view, typically from a nib.
}
func startAnimating(){
backgroundImage.animationImages = [
UIImage(named: "background1.jpg"),
UIImage(named: "background2.jpg")
]
backgroundImage.animationDuration = 3
backgroundImage.startAnimating()
}
The below code will help you to animate image with fadeOut effect and change the background.
import UIKit
class ViewController: UIViewController {
var imgView:UIImageView? //class variable to display you bg image
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imgView = UIImageView(frame: self.view.frame) //imgView to display background
self.view.insertSubview(imgView, atIndex: 0) //imgview add above the background of main view
animateImage(1) /*call animageImage with parameter number as image number as i user image name as avatar1.png, avatar2.png, avatar3.png and avatar4.png*/
}
func animateImage(no:Int)
{
var imgNumber:Int = no
let t:NSTimeInterval = 1;
let t1:NSTimeInterval = 0;
var name:String = "avatar\(imgNumber).png"
imgView!.alpha = 0.4
imgView!.image = UIImage(named:name);
//code to animate bg with delay 2 and after completion it recursively calling animateImage method
UIView.animateWithDuration(2.0, delay: 0, options:UIViewAnimationOptions.CurveEaseOut, animations: {() in
self.imgView!.alpha = 1.0;
},
completion: {(Bool) in
imgNumber++;
if imgNumber>4 //only for 4 image
{
imgNumber = 1
}
self.animateImage(imgNumber);
})
}
}

Resources