An oldish retired DEC PDP 8 assembly programmer needs another set of eyes. I created a Hello World program that displays images of the family. The animation works fine, but I would like the user to be able to stop the animation so I added a button to “Pause” the image so that the user can view it at their leisure. However, the .stopAnimation function clears the view. How can this be prevented. Please be kind.
import UIKit
class FamilyUnitController: UIViewController {
#IBOutlet weak var familyMember: UILabel!
#IBOutlet weak var familyPhotos: UIImageView!
var familyName: String = ""
var familyUnitArray = [String]()
#IBOutlet weak var pauseButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
enter code here
configureImageView()
}
#IBAction func pauseImage(_ sender: UIButton) {
if familyPhotos.isAnimating {
familyPhotos.stopAnimating()
pauseButton.setTitle("Paused", for: [])
} else {
familyPhotos.startAnimating()
pauseButton.setTitle("Pause", for: [])
}
}
func configureImageView() {
familyMember.text = familyUnitArray[0]
familyMember.sizeToFit()
let imagePrefix = familyUnitArray[1]
print ("configureImageView called...")
let maxImageCnt = 10
if let imageView = familyPhotos {
print ("Processing images...")
let fileManager = FileManager.default
let path = Bundle.main.resourcePath!
print (#function,">Path> ",path)
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path)!
var imageArr = [UIImage]()
var imageCnt: Int = 0
while let element = enumerator.nextObject() as? String {
print (#function,"> element.description",element.debugDescription)
let tmpStr = element.lowercased()
let tmpImagePrefix = imagePrefix.lowercased()
if (element.hasSuffix("png") || element.hasSuffix("jpg") || element.hasSuffix("jpeg"))
&& tmpStr.hasPrefix(tmpImagePrefix)
&& imageCnt < maxImageCnt {
let elementWithPath = path+"/"+element
imageArr.append(UIImage(contentsOfFile: elementWithPath)!)
imageCnt += 1
}
}
imageView.isUserInteractionEnabled = true
imageView.animationImages = imageArr
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = UIColor.white
imageView.animationDuration = 15
imageView.startAnimating()
}
}
}
You may refer here to for pausing the animation / not stopping it.
Is there a way to pause a CABasicAnimation?
In your case ,the code should be like the following, you may need to change a little to make it perfect.
#IBAction func pauseImage(_ sender: UIButton) {
if familyPhotos.layer.speed != 0 {
let time = familyPhotos.layer.convertTime(CACurrentMediaTime(), to: nil)
familyPhotos.layer.speed = 0
familyPhotos.layer.timeOffset = time
pauseButton.setTitle("Paused", for: [])
} else {
familyPhotos.layer.beginTime = familyPhotos.layer.timeOffset - CACurrentMediaTime()
familyPhotos.layer.timeOffset = 0
familyPhotos.layer.speed = 1
pauseButton.setTitle("Pause", for: [])
}
}
Related
I am trying that when I press the image it takes me to the link
I have this:
class CoverPillCell: TableViewCell {
#IBOutlet weak var imagePill: UIImageView!
#IBOutlet weak var imagenButton: UIButton!
this contains variables that later in the load function call the remoteconfig
height = CGFloat(promoPill?.image_height ?? 0)
width = CGFloat(promoPill?.image_width ?? 0)
target_url = promoPill?.target_url ?? ""
position = promoPill?.position ?? 0
if enabled == true {
enabled = ((promoPill?.enabled) != nil)
}
//This is what I'm trying to when tap go to the target url which is configured on the remote config
if imagenButton.isUserInteractionEnabled == true {
self.imagenButton.addTarget(self, action: #selector(tapPill(_:)), for: .touchUpInside)
}
And in the IBAction I'm trying this:
#IBAction func tapPill(_ sender: UIButton) {
let promoPill = getPromoPill()
target_url = promoPill?.target_url ?? ""
}
Maybe this link can help you
You have to do something like this ->
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
Here is the error that I am seeing.
The "cardButton" is responsible for showing the next question. This is a small card app game that I am trying to make and as you can see from the image this is where I am having the issue with the code.
Here is the code:
import UIKit
class MultipleViewController: UIViewController {
#IBOutlet weak var questionLabel2: UILabel!
#IBOutlet var answerButtons: [UIButton]!
#IBOutlet weak var cardButton: UIButton!
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < count(mcArray) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
#IBAction func answerButtonHandle(_ sender: UIButton) {
if sender.titleLabel?.text == correctAnswer{
sender.backgroundColor = UIColor.green
print("Correct!")
} else {
sender.backgroundColor = UIColor.red
print("Wrong Answer")
}
for button in answerButtons{
button.isEnabled = false
if button.titleLabel?.text == correctAnswer {
button.backgroundColor = UIColor.green
}
}
cardButton.isEnabled = true // next question
}
var correctAnswer: String? // correct answers
var answers = [String]() // answers
var question : String? // Questions
var questionIdx = 0 // not sure what this is ?
override func viewDidLoad() {
super.viewDidLoad()
// titleForButtons() // call buttons as soon its loaded..
cardButton.isEnabled = false
nextQuestion()
}
func nextQuestion (){
let currentQuestion = mcArray![questionIdx]
answers = currentQuestion["Answers"] as! [String]
correctAnswer = currentQuestion["CorrectAnswer"] as? String
question = currentQuestion["Question"] as? String
titleForButtons ()
}
func titleForButtons (){
for (idx,button) in answerButtons .enumerated() {
button.titleLabel?.lineBreakMode = .byWordWrapping
button.setTitle(answers[idx],for:.normal)
button.isEnabled = true
}
questionLabel2.text = question
}
}
The following should work, you did not have the correct syntax for the length of the array. Note that if you have not initialized your questions array, this would cause a crash. Therefore you might want to add a guard into your code. Perhaps use the following
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < (mcArray!.count) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
so I'm new to coding and I've been doing practice games a such to build my skills. I've created this word guessing game and I'm trying to make the game show the word after all guesses have been used up. However, the program doesn't read the code I write to set the label to display the answer. Here is the code I've written so far:
class ViewController: UIViewController {
var listOfWords = ["ladybug", "program", "computer", "language", "glorious", "incandescent"]
let incorrectMovesAllowed = 7
var totalWins = 0 {
didSet {
newRound()
}
}
var totalLosses = 0 {
didSet {
newRound()
}
}
#IBOutlet var letterButtons: [UIButton]!
#IBAction func buttonPressed(_ sender: UIButton) {
sender.isEnabled = false
let letterString = sender.title(for: .normal)!
let letter = Character(letterString.lowercased())
currentGame.playerGuessed(letter: letter)
updateUI()
updateGameState()
}
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var correctWordLabel: UILabel!
#IBOutlet weak var treeImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
newRound()
// Do any additional setup after loading the view, typically from a nib.
}
func enableLetterButtons (_enable: Bool){
for button in letterButtons {
button.isEnabled = _enable
}
}
var currentGame : Game!
func newRound() {
if !listOfWords.isEmpty{
let newWord = listOfWords.removeFirst()
currentGame = Game (word: newWord, incorrectMovesRemaining: incorrectMovesAllowed, guessedLetters: [])
enableLetterButtons(_enable: true)
updateUI()
} else {
enableLetterButtons (_enable: false)
}
}
func updateUI() {
var letters = [String] ()
for letter in currentGame.formattedWord.characters {
letters.append(String(letter))
}
let wordWithSpacing = letters.joined(separator: " ")
correctWordLabel.text = wordWithSpacing
scoreLabel.text = "Wins: \(totalWins), Losses:\(totalLosses)"
treeImageView.image = UIImage (named: "Tree \(currentGame.incorrectMovesRemaining)")
}
func updateGameState(){
var letters = [String] ()
for letter in currentGame.word.characters {
letters.append(String(letter))
}
let theAnswer = letters.joined(separator: " ")
if currentGame.incorrectMovesRemaining == 0 {
correctWordLabel.text = theAnswer
Thread.sleep(forTimeInterval: 3)
totalLosses += 1
} else if currentGame.word == currentGame.formattedWord {
totalWins += 1
} else {
updateUI()
}
}
}
In addition, I have a structure that is written like this:
import Foundation
struct Game {
var word : String
var incorrectMovesRemaining : Int
var guessedLetters: [Character]
mutating func playerGuessed (letter: Character){
guessedLetters.append(letter)
if !word.characters.contains(letter){
incorrectMovesRemaining -= 1
}
}
var formattedWord: String {
var guessedWord = ""
for letter in word.characters {
if guessedLetters.contains(letter) {
guessedWord += "\(letter)"
} else {
guessedWord += "_"
}
}
return guessedWord
}
}
You need to redraw your UI, this is done with self.setNeedsDisplay(). It notifies the system that the view's contents needs to be redrawn. In your updateUI() you can add this.
Regarding setNeedsDisplay you can get more information here
class ViewController: UIViewController {
var listOfWords = ["ladybug", "program", "computer", "language", "glorious", "incandescent"]
let incorrectMovesAllowed = 7
var totalWins = 0 {
didSet {
newRound()
}
}
var totalLosses = 0 {
didSet {
newRound()
}
}
#IBOutlet var letterButtons: [UIButton]!
#IBAction func buttonPressed(_ sender: UIButton) {
sender.isEnabled = false
let letterString = sender.title(for: .normal)!
let letter = Character(letterString.lowercased())
currentGame.playerGuessed(letter: letter)
updateUI()
updateGameState()
}
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var correctWordLabel: UILabel!
#IBOutlet weak var treeImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
newRound()
// Do any additional setup after loading the view, typically from a nib.
}
func enableLetterButtons (_enable: Bool){
for button in letterButtons {
button.isEnabled = _enable
}
}
var currentGame : Game!
func newRound() {
if !listOfWords.isEmpty{
let newWord = listOfWords.removeFirst()
currentGame = Game (word: newWord, incorrectMovesRemaining: incorrectMovesAllowed, guessedLetters: [])
enableLetterButtons(_enable: true)
updateUI()
} else {
enableLetterButtons (_enable: false)
}
}
func updateUI() {
var letters = [String] ()
for letter in currentGame.formattedWord.characters {
letters.append(String(letter))
}
let wordWithSpacing = letters.joined(separator: " ")
correctWordLabel.text = wordWithSpacing
scoreLabel.text = "Wins: \(totalWins), Losses:\(totalLosses)"
treeImageView.image = UIImage (named: "Tree \(currentGame.incorrectMovesRemaining)")
self.setNeedsDisplay()
}
func updateGameState(){
var letters = [String] ()
for letter in currentGame.word.characters {
letters.append(String(letter))
}
let theAnswer = letters.joined(separator: " ")
if currentGame.incorrectMovesRemaining == 0 {
correctWordLabel.text = theAnswer
Thread.sleep(forTimeInterval: 3)
totalLosses += 1
} else if currentGame.word == currentGame.formattedWord {
totalWins += 1
} else {
updateUI()
}
}
}
Create a variable that will keep track of how many times you have guessed wrong. Then you can do this:
Use a while statement:
while numberOfTimesGuessedWrong <= 7{
}
//when you have guessed incorrectly 7 times, the compiler will move here:
wordLabel.text = "\(correctAnswer)"
So when you guess incorrectly 7 times, on the 8th time, it will then show the correct answer.
How to save On-Off value of a button? I have a On-Off button, when a user opens the app and click the Off button, I want the app to save it, so that when the app is reopened, the value of the button is still Off. Hopefully, this can be done by using User Defaults. My code:
class ViewController: UIViewController {
var soundEnable:Bool = true
var soundImage:UIImage?
#IBAction func soundbtn(_ sender: UIButton) {
let soundButton = sender
if (soundEnable) {
soundImage = UIImage.init(named: "mute.png")
soundEnable = false
} else {
soundImage = UIImage.init(named: "sound.png")
soundEnable = true
}
soundButton.setImage(soundImage, for: UIControlState.normal)
}
}
You need to set the image one more place at viewDidLoad.
You input the wrong parameter for setImage:
soundButton.setImage(soundImage, for: UIControlState())
it should be a specific state:
soundButton.setImage(soundImage, for: UIControlState.normal)
Please write these lines in viewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
if (UserDefaultsManager.useDarkTheme) {
soundImage = UIImage.init(named: "mute")
print(UserDefaultsManager.useDarkTheme)
} else {
soundImage = UIImage.init(named: "sound")
print(UserDefaultsManager.useDarkTheme)
}
}
#IBOutlet weak var backgroundImage: UIImageView!
let ImageNameKey = "ImageNameKey"
let soundbg = UIImage(named: "sound")
let mutebg = UIImage(named: "mute")
override func viewDidLoad() {
super.viewDidLoad()
//Sound.play(file: "bg", fileExtension: "wav", numberOfLoops: -1)
let defaults = UserDefaults.standard
if let name = defaults.string(forKey: ImageNameKey) {
if (name == "sound") {
backgroundImage.image = soundbg
} else {
backgroundImage.image = mutebg
}
}
}
#IBAction func clickbtn(_ sender: UIButton) {
let defaults = UserDefaults.standard
if(backgroundImage.image == soundbg){
backgroundImage.image = mutebg
defaults.set("mute", forKey: ImageNameKey)
Sound.enabled = false
}
else{
backgroundImage.image = soundbg
defaults.set("sound", forKey: ImageNameKey)
Sound.enabled = true
}
}
I have added a button, that adds points to a label.
Everything works fine, and the label is then persisted into core data and appears in a tableViewCell.
When I get back to my detailsVC, I get my label with the persisted number, but when I click on the button again to increment the points, the label goes back to zero.
Here's a part of my code:
import UIKit
import CoreData
class GoalDetailsVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// IBOutlets:
#IBOutlet weak var titleTF: UITextField!
#IBOutlet weak var detailsTextView: UITextView!
#IBOutlet weak var pointsLabel: UILabel!
#IBOutlet weak var dateOfEntry: UILabel!
#IBOutlet weak var thumbImage: UIImageView!
// properties
var currentScore = 0
var goalToEdit: Goal? // goalToEdit is now an optional, and it needs to be unwrapped when used.
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
if let topItem = self.navigationController?.navigationBar.topItem {
topItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
}
// now we need to say that if there is a goal to edit ( not equal to nil), then we load the Goal data with the loadGoalData() function.
if goalToEdit != nil {
loadGoalData()
}
imagePicker = UIImagePickerController()
imagePicker.delegate = self
}
// when button is pressed, I need to
// 1 : add a point to the pointsLabel
// 2 : put the current date to the dateLabel
// 3 : persist the new points and date labels.
#IBAction func plusOneBtnPressed(_ sender: UIButton) {
currentScore += 1
pointsLabel.text = "\(currentScore)"
}
#IBAction func minusOneBtnPressed(_ sender: Any) {
}
#IBAction func savePressed(_ sender: Any) {
var goal: Goal!
let picture = Image(context: context) // Image = Entity
picture.image = thumbImage.image // image = attribute
if goalToEdit == nil {
goal = Goal(context: context)
} else {
goal = goalToEdit
}
goal.toImage = picture
// this is unwrapping because the original goalToEdit is an optional.
if let title = titleTF.text {
goal.title = title
}
// we saveed, or persisted the TITLE
if let points = pointsLabel.text {
goal.plusOnes = (points as NSString).intValue
}
// we saveed, or persisted the POINTS
if let details = detailsTextView.text {
goal.details = details
}
// we saved, or persisted the DETAILS
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE MMM d yyyy"
if let date = dateFormatter.date(from: dateFormatter.dateFormat) {
goal.lastEntry = date as NSDate
}
// we saved, or persisted the DATE
ad.saveContext()
_ = navigationController?.popViewController(animated: true)
}
func loadGoalData() {
if let goal = goalToEdit {
titleTF.text = goal.title
pointsLabel.text = "\(goal.plusOnes)"
detailsTextView.text = goal.details
dateOfEntry.text = (String(describing: goal.lastEntry))
thumbImage.image = goal.toImage?.image as? UIImage
}
}
When you get the persisted number you should also set currentScore to that value (if greater than 0). I believe currently you only set it to 0 that's why the incrementation starts over.