Here's my situation:
There are 8 ImageViews. The 4 on the left are called in my code (starting at the top): Left1, Left2, Left3, left4. The 4 on the right (always starting at the top): Right1, Right2, Right3, Right4.
Then there are two red Buttons in the middle: "Up" and "Down" and a Label.
What should happen is that when the Button Up is pressed, all the ImageViews go to a position which is above the top of the screen (so their center.y < 0) that I've set in the code, and when this happens the label text is set to "Up".
When on the contrary I press the Button Down, all the ImageViews go back to the initial position and the label text is set to "Down".
What happens instead is that the label.text changes according to my code, but my ImageViews don't move.
Here's my code:
First i declare the objects:
class ViewController: UIViewController {
#IBOutlet weak var Up: UIButton!
#IBOutlet weak var Down: UIButton!
#IBOutlet weak var Lab: UILabel!
#IBOutlet weak var Left1: UIImageView!
#IBOutlet weak var Left2: UIImageView!
#IBOutlet weak var Left3: UIImageView!
#IBOutlet weak var Left4: UIImageView!
#IBOutlet weak var Right1: UIImageView!
#IBOutlet weak var Right2: UIImageView!
#IBOutlet weak var Right3: UIImageView!
#IBOutlet weak var Right4: UIImageView!
In the View Did Load I hide all the ImageViews:
Left1.hidden = true
Left2.hidden = true
Left3.hidden = true
Left4.hidden = true
Right1.hidden = true
Right2.hidden = true
Right3.hidden = true
Right4.hidden = true
Then there are the two Buttons actions:
-Button Up
#IBAction func Up(sender: AnyObject) {
Lab.text = "Up"
//-------- 1 ----------//
Right1.center.y = 0 - 31
Left1.center.y = 0 - 31
Right1.hidden = false
Left1.hidden = false
//-------- 2 ----------//
Right2.center.y = 0 - 300
Left2.center.y = 0 - 300
Right2.hidden = false
Left2.hidden = false
//-------- 3 ----------//
Right3.center.y = 0 - 650
Left3.center.y = 0 - 650
Right3.hidden = false
Left3.hidden = false
//-------- 4 ----------//
Right4.center.y = 0 - 960
Left4.center.y = 0 - 960
Right4.hidden = false
Left4.hidden = false
}
Here I put the Label text to "Up" and, for every couple of ImageView (when I say couple I mean that the ImageViews at the top of the image (1) are the Couple 1, the ones at the bottom the Couple 4) I set their center.y to a value above the screen and then i show them.
-Button Down
#IBAction func Down(sender: AnyObject) {
Lab.text = "Down"
//-------- 1 ----------//
Right1.center.y = 92
Left1.center.y = 92
//-------- 2 ----------//
Right2.center.y = 171
Left2.center.y = 171
//-------- 3 ----------//
Right3.center.y = 249
Left3.center.y = 249
//-------- 4 ----------//
Right4.center.y = 326
Left4.center.y = 326
}
Here i just put the label text to "down" and the ImageViews back to the initial positions.
Now, this should work perfectly, but what happens is that the label text is changed to up when I press the Up Button, but the ImageViews don't move, just appear.
On the contrary if I comment the label instructions then it works:
-Button Up
#IBAction func Up(sender: AnyObject) {
//Lab.text = "Up"
//-------- 1 ----------//
Right1.center.y = 0 - 31
Left1.center.y = 0 - 31
Right1.hidden = false
Left1.hidden = false
//-------- 2 ----------//
Right2.center.y = 0 - 300
Left2.center.y = 0 - 300
Right2.hidden = false
Left2.hidden = false
//-------- 3 ----------//
Right3.center.y = 0 - 650
Left3.center.y = 0 - 650
Right3.hidden = false
Left3.hidden = false
//-------- 4 ----------//
Right4.center.y = 0 - 960
Left4.center.y = 0 - 960
Right4.hidden = false
Left4.hidden = false
}
-Button Down
#IBAction func Down(sender: AnyObject) {
//Lab.text = "Down"
//-------- 1 ----------//
Right1.center.y = 92
Left1.center.y = 92
//-------- 2 ----------//
Right2.center.y = 171
Left2.center.y = 171
//-------- 3 ----------//
Right3.center.y = 249
Left3.center.y = 249
//-------- 4 ----------//
Right4.center.y = 326
Left4.center.y = 326
}
In this way the ImageViews move as they should, but of course the label text doesn't change.
Is there any relationship between the label and the imageviews?
I really hope you can help me, this is crazy.
Thank you
Related
I want to calculate Runs per Overs and display on a Run Rate Label.
As you can see in my code, the Run Rate will change as i'm using steppers for Runs and Overs.
The run rate need to be displayed up to 2 decimal points (eg. 4.50 or 10.74 etc.)
I want to create function but I don't know how to calculate.
How can I do this?
// RUNS
var runsInt = 0
var oversFloat: Float = 0.0
var runRate: Double = 0.0
#IBOutlet weak var runsLabel: UILabel!
#IBOutlet weak var displayRunsLabel: UILabel!
#IBOutlet weak var oversLabel: UILabel!
#IBOutlet weak var oversRectangle: UILabel!
#IBOutlet weak var displayOversLabel: UILabel!
#IBOutlet weak var runRateLabel: UILabel!
#IBOutlet weak var displayRunRateLabel: UILabel!
#IBAction func RunStepper(_ sender: UIStepper) {
let runsValue = Int(sender.value)
displayRunsLabel.text = String(runsValue)
}
// OVERS
#IBAction func OversStepper(_ sender: UIStepper) {
let value = Int(sender.value)
let remainder = value % 10
if remainder == 6 {
sender.value = Double(value + 4)
} else if remainder == 9 {
sender.value = Double(value - 4)
}
displayOversLabel.text = String(format: "%.1f", sender.value / 10)
}
I used this page for information on calculating run rate.
runRate = (total runs scored)/(total overs faced)
Note: If overs is 47.5, the stepper value will be 475.0 and
oversInt will be set to 475. When calculating runRate, 47.5
actually represents 47 5/6 which we can calculate as
Double(oversInt / 10) + Double(oversInt % 10) / 6
Create a function called calculateRunRate and
add property observers (didSet) to runsInt and oversInt to trigger the calculation of runRate. Then anytime either of those values changes, runRate will be recalculated and the displayRunRate label will be updated.
// RUNS
var runsInt = 0 {
didSet {
calculateRunRate()
}
}
#IBOutlet weak var runsLabel: UILabel!
#IBOutlet weak var displayRunsLabel: UILabel!
#IBAction func RunStepper(_ sender: UIStepper) {
runsInt = Int(sender.value)
displayRunsLabel.text = String(runsInt)
}
// OVERS
var oversInt = 0 {
didSet {
calculateRunRate()
}
}
#IBOutlet weak var oversLabel: UILabel!
#IBOutlet weak var oversRectangle: UILabel!
#IBOutlet weak var displayOversLabel: UILabel!
#IBAction func OversStepper(_ sender: UIStepper) {
var value = Int(sender.value)
let remainder = value % 10
if remainder == 6 {
value += 4
} else if remainder == 9 {
value -= 4
}
sender.value = Double(value)
oversInt = value
displayOversLabel.text = String(format: "%.1f", sender.value / 10)
}
// RUN RATE
var runRate = 0.0
let maxOvers = 40.0
#IBOutlet weak var runRateLabel: UILabel!
#IBOutlet weak var displayRunRateLabel: UILabel!
#IBOutlet weak var displayRequiredRunRateLabel: UILabel!
func calculateRunRate() {
var oversDouble = 0.0
// Don't divide by zero
if oversInt == 0 {
// can this even happen?
// set runRate to some value that makes sense
runRate = 0.0
} else {
oversDouble = Double(oversInt / 10) + Double(oversInt % 10) / 6
runRate = Double(runsInt) / oversDouble
}
displayRunRateLabel.text = String(format: "%.2f", runRate)
// required run rate
let targetScore = Int(targetScoreLabel.text ?? "") ?? 0
var requiredRunRate = 0.0
if oversDouble < maxOvers {
requiredRunRate = Double(targetScore - runsInt) / (maxOvers - oversDouble)
}
displayRequiredRunRateLabel.text = String(format: "%.2f", requiredRunRate)
}
I'm trying to get the int values from my labels and pass them to another label and add them together. In the code below, kicklabel, handballlabel, marklabel, etc., all need to add up and equal into fantasylabel. Based on how this code is set up, I'm not sure how to perform that.
I want to be able to add more lines of labels also, so that any amount of the array of labels add to a fantasy label.
Here's the code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var kickLabel1: UILabel!
#IBOutlet weak var handballLabel1: UILabel!
#IBOutlet weak var markLabel1: UILabel!
#IBOutlet weak var tackleLabel1: UILabel!
#IBOutlet weak var goalLabel1: UILabel!
#IBOutlet weak var pointLabel1: UILabel!
#IBOutlet weak var freeForLabel1: UILabel!
#IBOutlet weak var freeAgainstLabel1: UILabel!
#IBOutlet weak var fantasyLabel: UILabel!
var counters: [UILabel: Int] = [:]
override func viewDidLoad() {
super.viewDidLoad()
for label: UILabel in [kickLabel1,handballLabel1,markLabel1,tackleLabel1,goalLabel1,pointLabel1, freeForLabel1, freeAgainstLabel1, fantasyLabel] {
counters[label] = 0
for direction: UISwipeGestureRecognizerDirection in [.up, .down, .left, .right] {
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(didSwipe(_:)))
swipeGesture.direction = direction
label.addGestureRecognizer(swipeGesture)
label.isUserInteractionEnabled = true
label.isMultipleTouchEnabled = true
}
}
}
#objc func didSwipe(_ gestureRecognizer: UISwipeGestureRecognizer) {
guard let label = gestureRecognizer.view as? UILabel else { return }
debugPrint("\(gestureRecognizer.direction)")
switch gestureRecognizer.direction {
case .up:
counters[label] = counters[label]! + 10
print(counters)
case .down:
counters[label] = 0
print(counters)
case .left:
counters[label] = counters[label]! - 1
print(counters)
case .right:
counters[label] = counters[label]! + 1
print(counters)
default:
counters[label] = 0
}
label.text = "\(counters[label]!)"
}
}
At the end of your didSwipe you need to sum all of the Int values stored in your counters dictionary. Then update your fantasy label with that sum.
let sum = counters.reduce(0) { $0 + $1.value }
fantasyLabel.text = "\(sum)"
And you probably don't want a gesture on the fantasy label since that value is read-only based on the sum of the other data.
I would also get rid of the individual label outlets except for the fantasy label. Use an IBOutletCollect to store an array of UILabel.
I am making a calculator app in iOS 9, Swift and with Xcode 7 and I would like to know how to create UIButtons when the user switches to landscape mode only. Adding more operators to the left of the number and AC Button and changing the constraints, shifting the buttons over to make room for the new ones. The basic calculator for iOS does this and I'm wondering how??
I'm new to iOS development and I have been web browsing to only find an overriding function called: viewWillTransitionToSize(). Is this the right function to do this sort of thing?
Also, everytime I enter the landscape view, another button gets created. How can I stop this from happening?
Attached is my programmatic creation of the UIbutton and the function I listed above.
Sorry for the array of questions/problems. I am having difficulties finding tutorials or help on this issue. Any guidance or solution will be helpful. Thank you so much.
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var outputDisplay: UILabel!
#IBOutlet weak var number7: UIButton!
#IBOutlet weak var number1: UIButton!
#IBOutlet weak var number2: UIButton!
#IBOutlet weak var number3: UIButton!
#IBOutlet weak var number4: UIButton!
#IBOutlet weak var number5: UIButton!
#IBOutlet weak var number6: UIButton!
#IBOutlet weak var number9: UIButton!
#IBOutlet weak var number8: UIButton!
#IBOutlet weak var numberZero: UIButton!
#IBOutlet weak var decimalPoint: UIButton!
#IBOutlet weak var plusMinusToggle: UIButton!
#IBOutlet weak var acClear: UIButton!
#IBOutlet weak var equalButton: UIButton!
#IBOutlet weak var multiply: UIButton!
#IBOutlet weak var divide: UIButton!
#IBOutlet weak var subtraction: UIButton!
#IBOutlet weak var addition: UIButton!
#IBOutlet weak var deletion: UIButton!
#IBOutlet weak var squareRoot: UIButton!
var typing:Bool = false
var firstNumber: Float = 0.0
var secondNumber: Float = 0.0
var operation:String = ""
var result: Float = 0.0
var valueToPass: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
number1.layer.cornerRadius = number1.bounds.size.width / 8.0
number2.layer.cornerRadius = number2.bounds.size.width / 8.0
number3.layer.cornerRadius = number3.bounds.size.width / 8.0
number4.layer.cornerRadius = number4.bounds.size.width / 8.0
number5.layer.cornerRadius = number5.bounds.size.width / 8.0
number6.layer.cornerRadius = number6.bounds.size.width / 8.0
number7.layer.cornerRadius = number7.bounds.size.width / 8.0
number8.layer.cornerRadius = number8.bounds.size.width / 8.0
number9.layer.cornerRadius = number9.bounds.size.width / 8.0
numberZero.layer.cornerRadius = numberZero.bounds.size.width / 8.0
equalButton.layer.cornerRadius = equalButton.bounds.size.width / 8.0
deletion.layer.cornerRadius = deletion.bounds.size.width / 8.0
plusMinusToggle.layer.cornerRadius = plusMinusToggle.bounds.size.width / 8.0
decimalPoint.layer.cornerRadius = decimalPoint.bounds.size.width / 8.0
acClear.layer.cornerRadius = acClear.bounds.size.width / 8.0
addition.layer.cornerRadius = addition.bounds.size.width / 8.0
divide.layer.cornerRadius = divide.bounds.size.width / 8.0
multiply.layer.cornerRadius = multiply.bounds.size.width / 8.0
subtraction.layer.cornerRadius = subtraction.bounds.size.width / 8.0
squareRoot.layer.cornerRadius = squareRoot.bounds.size.width / 8.0
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
if (UIDevice.currentDevice().orientation.isLandscape) {
print("Device in Lanscape")
let newButton1 = UIButton(type: UIButtonType.System) as UIButton
newButton1.frame = CGRectMake(100, 100, 100, 50)
newButton1.backgroundColor = UIColor.whiteColor()
newButton1.center = CGPoint(x: number1.frame.width, y: number1.frame.height)
newButton1.layer.cornerRadius = newButton1.bounds.size.width / 8.0
self.view.addSubview(newButton1)
}
else{}
}
#IBAction func numberPressed(sender: AnyObject) {
// set the variable "number" to whichever number the user presses
let number = sender.currentTitle!
if typing {
outputDisplay.text = outputDisplay.text! + String(number!)
} else {
outputDisplay.text = number!
}
typing = true
}
#IBAction func calculationPressed(sender: AnyObject) {
typing = false
firstNumber = Float(outputDisplay.text!)!
operation = sender.currentTitle!!
}
#IBAction func equalPressed(sender: AnyObject) {
secondNumber = Float(outputDisplay.text!)!
typing = false
//arithmetic operations
if operation == "+" {
result = firstNumber + secondNumber
}
else if operation == "-" {
result = firstNumber - secondNumber
}
else if operation == "✕" {
result = firstNumber * secondNumber
}
else if operation == "÷" {
result = firstNumber / secondNumber
}
// else if operation == "%" {
// result = (firstNumber * secondNumber) / 100
// }
else if operation == "√" {
result = sqrt(firstNumber)
}
outputDisplay.text = "\(result)"
}
#IBAction func squareRoot(sender: AnyObject) {
result = sqrt(Float(outputDisplay.text!)!)
outputDisplay.text = "\(result)"
}
#IBAction func deletePressed(sender: AnyObject) {
if outputDisplay.text!.characters.count != 0 {
outputDisplay.text = outputDisplay.text!.substringToIndex(outputDisplay.text!.endIndex.predecessor())
}
else{
outputDisplay.text = "0"
}
}
#IBAction func plusMinus(sender: AnyObject) {
if( outputDisplay.text![outputDisplay.text!.startIndex] == "-"){
outputDisplay.text!.removeAtIndex(outputDisplay.text!.startIndex)
}else{
outputDisplay.text!.insert("-", atIndex: outputDisplay.text!.startIndex)
}
}
#IBAction func decimal(sender: AnyObject) {
let decimal = sender.currentTitle!
outputDisplay.text = outputDisplay.text! + decimal!
}
#IBAction func clear(sender: AnyObject) {
result = 0
firstNumber = 0
secondNumber = 0
outputDisplay.text = "0"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I would do this using Size Classes. You can design different layouts and button arrangements in storyboard using the different size classes. If this application is on the iPhone, you would use Compact width and Regular height when in Portrait and Compact height and Regular width when in Landscape.
I'm learning Swift, and I'm stuck with a little addition system.
I set a variable for the price of a burger and also for a taco. When I click on one of those, the price appears for my selection, but if I click a second time the price doesn't add with the last one. I want to make it like a restaurant bill.
Here a little video to show it: https://www.youtube.com/watch?v=L8YvIqkAU3k
Here's the first View Controller:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var sandwich: UILabel!
var sandwichplus = 0
var Tacosplus = 0
var Burgerplus = 0
var prixvaleur = 0.0
let tacosprix = 5.5
let burgerprix = 2.6
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
sandwich.hidden = true
}
#IBOutlet weak var NombreTacos: UILabel!
#IBOutlet weak var NombreBurger: UILabel!
#IBOutlet weak var Burger: UIImageView!
#IBOutlet weak var Prix: UILabel!
#IBOutlet weak var Tacos: UIImageView!
#IBAction func WantTacos(sender: AnyObject) {
prixvaleur = prixvaleur + tacosprix
Tacos.hidden = false
Burger.hidden = true
sandwich.hidden = false
sandwichplus++
Tacosplus++
NombreTacos.text = " Tacos =\(Tacosplus)"
println(tacosprix)
sandwich.text = " Sandwich =\(sandwichplus)"
Prix.text = "Prix =\(prixvaleur)€"
}
#IBAction func WhatInBurger(sender: AnyObject) {
}
#IBAction func WantBurger(sender: AnyObject) {
prixvaleur = prixvaleur + burgerprix
Burger.hidden = false
Tacos.hidden = true
sandwich.hidden = false
sandwichplus++
Burgerplus++
prixvaleur + burgerprix
NombreBurger.text = "Burger =\(Burgerplus)"
sandwich.text = " Sandwich =\(sandwichplus)"
Prix.text = "Prix =\(prixvaleur)€"
}
}
When you press button this code count the price let Ajouteztacos = prixvaleur + tacosprix where prixvaleur = 0 and tacosprix = 5 every time so it shows 5 for every click in both of your action so update your code like this:
First of all make your prixvaleur variable because it is constant like this:
var prixvaleur = 0
After that update your button action like this:
#IBAction func WantTacos(sender: AnyObject) {
prixvaleur = prixvaleur + tacosprix
Tacos.hidden = false
Burger.hidden = true
sandwich.hidden = false
sandwichplus++
Tacosplus++
NombreTacos.text = " Tacos =\(Tacosplus)"
println(tacosprix)
Prix.text = "Prix =\(prixvaleur)"
}
#IBAction func WantBurger(sender: AnyObject) {
prixvaleur = prixvaleur + burgerprix
Burger.hidden = false
Tacos.hidden = true
sandwich.hidden = false
sandwichplus++
Burgerplus++
prixvaleur + burgerprix
NombreBurger.text = "Burger =\(Burgerplus)"
sandwich.text = " Sandwich =\(sandwichplus)"
Prix.text = "Prix =\(prixvaleur)"
}
It will update the price every time you click on button.
I am new to iOS and Swift. I am trying to add a background image for a slide right effect. I want that image to stick to the right of the screen no matter what orientation. The image will always be a set size (now its (382x145px). How can I adjust it on landscape orientation so that the picture stays to the right.
Here is my custom view cell class.
class CustomRouteViewCell: UITableViewCell {
#IBOutlet var downImage: UIImageView!
#IBOutlet var downTime: UILabel!
#IBOutlet weak var leadingSpaceConstraint: NSLayoutConstraint!
#IBOutlet weak var trailingSpaceConstraint: NSLayoutConstraint!
#IBOutlet var locationTitle: UILabel!
#IBOutlet weak var panView: UIView!
#IBOutlet var timerLabel: UILabel!
var indexRow: Int = 0
var timeInterval: Int = 0
override func awakeFromNib() {
super.awakeFromNib()
self.selectedBackgroundView = nil
var img = UIImage(named: "tableSwipeArrow.png")!
self.panView.backgroundColor = UIColor(patternImage: img)
if self.respondsToSelector(Selector("setLayoutMargins:")) {
layoutMargins = UIEdgeInsetsZero
}
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
preservesSuperviewLayoutMargins = false
}
var panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
panGestureRecognizer.delegate = self
addGestureRecognizer(panGestureRecognizer)
}
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
switch(recognizer.state) {
case UIGestureRecognizerState.Changed:
var translation = recognizer.translationInView(recognizer.view!)
var little = CGFloat(trailingSpaceConstraint.constant + translation.x * -1)
trailingSpaceConstraint.constant = fmax(0, little)
leadingSpaceConstraint.constant = fmin(0, leadingSpaceConstraint.constant + translation.x)
recognizer.setTranslation(CGPointZero, inView: recognizer.view!)
timeInterval = Int(trailingSpaceConstraint.constant / 30) * 5
timerLabel.text! = "\(timeInterval)"
case UIGestureRecognizerState.Ended, UIGestureRecognizerState.Cancelled:
var refreshAlert = Alarm.getReminderView(self.timeInterval, view: self.parentViewController!.view)
self.parentViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
leadingSpaceConstraint.constant = 0
trailingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.25, animations: { () -> Void in
self.layoutIfNeeded()
})
default:
trailingSpaceConstraint.constant = 0
leadingSpaceConstraint.constant = 0
}
}
}
extension CustomRouteViewCell: UIGestureRecognizerDelegate
{
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer.isKindOfClass(UIPanGestureRecognizer) {
var velocity = (gestureRecognizer as! UIPanGestureRecognizer).velocityInView(gestureRecognizer.view!)
return fabs(velocity.x) > fabs(velocity.y)
}
return true;
}
}
Use auto layout to link the position of the images.
In your case you can connect the trailing of the background image with the container view.
Hold Control on image and link the Trailing Attribute with the main view
Just adjust the constant to 0.