Swift 3 - UIButton adding setTitle from plist and database
What you need to do is first create an array with all your target letters (shuffled and added random letters) and then run over that array to update the button titles.
Start by filling a 14 value array with random letters:
var presentedLetters : [String] = [String]()
var availableLetters : [String] = pathDict?.object(forKey: "Letters") as! [String]
var availableLetterIndexes : [Int] = Array(0..<availableLetters.count)
for letterAt in 0 ..< 14
{
let randomIndex : Int = Int(arc4random_uniform(UInt32(availableLetterIndexes.count)))
var charIndex : Int = availableLetterIndexes[randomIndex]
presentedLetters.append(availableLetters[charIndex])
availableLetterIndexes.remove(at: randomIndex)
}
Generate available positions for the answer string:
var availableIndexes : [Int] = Array(0..<presentedLetters.count)
Then go over your letters from the db and add them in random places inside the target array (while remembering added indexes to prevent overriding letters):
var addedAnswerIndexes : [Int] = [Int]()
let answerString = data.ans // ans is a row in the SQLite database
let answerCharacters = answerString.characters.map{String($0)}
for answerCharAt in answerCharacters
{
let randomIndex : Int = Int(arc4random_uniform(UInt32(availableIndexes.count)))
var charIndex : Int = availableIndexes[randomIndex]
presentedLetters[charIndex] = answerCharAt
availableIndexes.remove(at: randomIndex)
}
And then presentedLetters will have an array of all relevant values.
EDIT:
To add the titles to the buttons, just go over presentedLetters array:
for i in 1...14 {
let tileButton = UIButton(type: .roundedRect)
tileButton.setTitle(presentedLetters[i-1], for: .normal)
......
}
}
Good luck!
Swift 3 - CGAffineTransform does not work
In the function, instead of using sender.transform, use btnButton.transform, this is caused because you are out of scope, and sender might be dismissed, which causes the animation to dismiss.
Another option is to strongly hold the object:
let tappedButton : UIButton = sender
and then use tappedButton
EDIT
I mean something like this:
func moveButton(sender: UIButton) {
if ((sender.tag == 1) && (sender == self.btnButton)) {
if self.btnButton.transform == CGAffineTransform.identity {
UIView.animate(withDuration: 0.5, animations:
self.btnButton.transform = CGAffineTransform(translationX: 50, y: 100)
})
}
}
}
EDIT - 2
According to your code there are 2 problems:
- All 14 buttons are stacked 1 on top of the other, meaning button 14 will be the topmost button instead of button 1.
You are not setting the tag, and then it fails in your check in moveButton:
func createButtonPuzzle(id: Int) {
for i in 1...14 {
let btnButton = UIButton(type: .roundedRect)
btnButton.frame = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2, width: 100, height: 100)
btnButton.backgroundColor = .blue
btnButton.setTitle("iButton", for: .normal)
btnButton.addTarget(self, action: #selector(moveButton(sender:)), for: .touchUpInside)
**btnButton.tag = i // This is missing **
view.addSubview(btnButton)
}}
And then this fails:
if sender.tag == 1 {
So to test it and see it working, first you need to either go from 14 to 1 and just from 1..<2 or reposition the buttons in different positions so that it will actually work.
Then the tags will be set and when button with tag 1 will be tapped it will work
Swift 3 - Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
Expression was too complex to be solved in reasonable time; consider
breaking up the expression into distinct sub-expressions
So as said, break it:
let preAxis1: GCFloat = (width / 2) - (totalWidth / 2)
let preAxis2: GCFloat = (tar * targetWidth) + (tar * 5) + 20
let xAxis : CGFloat = preAxis1 + preAxis2
Now, that reveals another (the real one) issue, and what you are doing doesn't make sense:
Binary operator '*' cannot be applied to operands of type 'Character'
and 'CGFloat'
So tar
is a Character
, not a "numeral".
There is no sense about using it in the calculation of xAxis
. What you want in fact is the index of it.
Instead of for tar in data.ans.characters {
, you can do it like that:
for (indexTar, tar) in data.ans.characters.enumerated() {
You are iterating all the characters
in data.ans
and indexTar
is the current index and tar
the Character
.
So by replacing tar
with indexTar
in the previous calculation of xAxis
it makes more sense and should work.
Title and subtitle with different fontsize for UIButton
Try this
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(20 , 13, 200, 85)];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 185, 30)];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont fontWithName:@"Chalkboard" size:14]];
[title setFont:[UIFont boldSystemFontOfSize:18]];
title.text=@"This is the title";
[title setTextColor:[UIColor blackColor]];
[btn addSubview:title];
UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 185, 30)];
[subtitle setBackgroundColor:[UIColor clearColor]];
[subtitle setFont:[UIFont fontWithName:@"Helvetica" size:14]];
subtitle.text=@"This is the sub";
[subtitle setTextColor:[UIColor blackColor]];
[btn addSubview:subtitle];
[title release];
[subtitle release];
[self.view addSubview:btn];
Can't change custom UIFont size on the UIButton
Interestingly enough, it turned out to be my Mac. When I gave my friend to install them, he had no issues.
My Mac is on the old side, so maybe this was the reason. Here are the specs:
**Mac:** MacBook Pro 15" (Late 2008)
**Processor:** 2.4 GHz Intel Core 2 Duo
**Memory:** 4 GB 1067 MHz DDR3
**Graphics:** NVIDIA GeForce 9600M GT 256 MB
constant ... inferred to have type '()?', which may be unexpected
Your developerButton
is the result of the text assignment text = "developer"
, which returns Void
(nothing, ()
). The assignment is conditional and that's why you are getting Void?
(or ()?
).
Split your two assignments correctly.
let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"
Also note that you should use setTitle
instead of setting title text directly:
let developerButton = UIButton(type: .system)
developerButton.setTitle("developer", for: .normal)
Related Topics
Retrieve Multiple Photos Under a Node from Firebase Storage
How to Manage Swiftui State with Nested Structs
Swiftui: How to Make a Button Open a Url in Safari
Spritekit and Scenekit - How to Completely Pause a Game
Playing a Sound in a Swift Playground
Is the Swift Divide "/" Operator Not Working or Have I Missed Something
Missing Argument for Parameter 'From' in Call When Creating Instance of Codable Class
Delay Using Dispatchtime.Now() + Float
Swift, Sprite Kit Game: Have Circle Disappear in Clockwise Manner? on Timer
How to Restrict a Protocol to Value Types Only
Is There a Daylight Savings Check in Swift
Call Parent Constructor When Init a Subclass in Swift
Using Applescript with Apple Events in MACos - Script Not Working
How to Remove an Element of a Given Value from an Array in Swift