I followed this video for study Collection View (https://www.youtube.com/watch?v=jQ8EUsQZJ5g)
And I wrote code like this :
if ( CollectionViewFlowLayout == nil ){
let numberOfItemsForRow : CGFloat = 2
let UpDownSpace : CGFloat = 5
let LeftRightSpace : CGFloat = 5
//CollectionViewFlowLayout은 delgate 처럼 소재지를 수정하고 꾸밀 수 있는 것이다.
CollectionViewFlowLayout = UICollectionViewFlowLayout()
//이건 margin의 크기를 정해주는 것이다.
CollectionViewFlowLayout?.sectionInset = UIEdgeInsets.zero
//이건 화면을 수직으로 내릴 것이냐, 수평으로 스크롤 할것이냐를 정해준다.
CollectionViewFlowLayout?.scrollDirection = .vertical
//소재지의 위 아래의 간격을 정해준다.
CollectionViewFlowLayout?.minimumLineSpacing = UpDownSpace
//같은 줄에 있는 소재지의 오른쪽 왼쪽 사이의 간격을 정해준다.
CollectionViewFlowLayout?.minimumInteritemSpacing = LeftRightSpace
let total = (LeftRightSpace * (numberOfItemsForRow - 1))
let width = (CollectionView.frame.width - total) / numberOfItemsForRow
let height = width
//itemsize : 소재지의 크기를 정 할 수 있다
CollectionViewFlowLayout?.itemSize = CGSize(width: width, height: height)
CollectionView.setCollectionViewLayout(CollectionViewFlowLayout!, animated: true)
}
But when I use simulator with iPhone 8, It make error
Other things are Good! without iPhone 8
enter image description here
I think your total should be calculated as:
let total = LeftRightSpace * (numberOfItemsForRow + 1), because you are taking away left, right and interitem spacing.
Or try calculating width as:
let width = (UIScreen.main.bounds.width - total) / numberOfItemsForRow
Related
I am new to Kivy and this is my first time trying to create a scrollView.
I am trying to do a 'Splitwise' ripoff where you can create expenses and let the app figure out who should pay who etc.
I am trying to create a method that opens a popup with all members and their balances as well as a button with the text 'Settle up'. Underneath the balances should be a list of all the registered debts, and you should be able to scroll down to see them all. The popup should also contain a 'Cancel'-button.
My problem is that my code doesn't enable scrolling, and also squishes together the content in the pop up. See picture.
All help highly appreciated!
See (messy) code below:
sbl_ret = GridLayout(cols = 1)
sbl_balances = GridLayout(cols = 3)
sbl_balances.add_widget(Label())
sbl_balances.add_widget(Label(text = 'Balances', font_size = '20sp', size_hint = (1,.2), color = backgroundColor, bold = True))
sbl_balances.add_widget(Label())
for member in self.members:
sbl_balances.add_widget(Label(text = member.getName(), font_size = 40, size_hint_y = None, height = 60))
sbl_balances.add_widget(Label(text = str(round(member.getBalance())) + ' kr', font_size = 40, size_hint_y = None, height = 60))
sbl2 = BoxLayout(orientation = 'vertical')
sbl2.add_widget(Label(size_hint = (1,.2)))
btn = Button(text = 'Settle up', background_color = buttonColor, bold = True, size_hint_y = None, height = 60)
btn.bind(on_press = lambda member: self.settleUp())
sbl2.add_widget(btn)
sbl2.add_widget(Label(size_hint = (1,.2)))
sbl_balances.add_widget(sbl2)
sbl_balances.add_widget(Label(size_hint = (.2,1)))
sbl_balances.add_widget(Label(text = 'Debts', font_size = '20sp', color = backgroundColor, bold = True))
sbl_balances.add_widget(Label(size_hint = (.2,1)))
for member in self.members:
for debt in member.getDebtList():
sbl_balances.add_widget(Label(size_hint = (.2,1)))
sbl_balances.add_widget(Label(size_hint_y = None, height = 60, text = debt.getToWhom().getName() + ' lent ' + str(debt.getAmount()) + ' SEK to ' + debt.getFromWhom().getName() + ' for ' + debt.getDescription(), font_size = 40))
sbl_balances.add_widget(Label(size_hint = (.2,1)))
sbl_balances.bind(minimum_height=sbl_balances.setter('height'))
sbl3 = BoxLayout(size_hint = (1,.15))
sbl3.add_widget(Label(size_hint = (.2,1)))
b = Button(text = 'Cancel', background_color = entryColor, size_hint_y = None, height = 150)
b.bind(on_press = lambda x: self.balanceDialog.dismiss())
sbl3.add_widget(b)
sbl3.add_widget(Label(size_hint = (.2,1)))
sv = ScrollView(do_scroll_x = False)
sv.add_widget(sbl_balances)
sbl_ret.add_widget(sv)
sbl_ret.add_widget(sbl3)
self.balanceDialog = Popup(title = 'Group view', content = sbl_ret, title_align = 'center',
title_color = backgroundColor, background_color = [0,0,0,.7], separator_height = 0, title_size = '28sp')
self.balanceDialog.open()
'''
Try adding size_hint_y=None to your code creating the GridLayout:
sbl_balances = GridLayout(cols = 3, size_hint_y=None)
See the documentation.
I have Label in my UIView which continuously changes values in between 80(min) to 475(Max) now I have run the progress bar status with respect to max and min value of label continuously. below where the code i have tried till now
if ampsMaxValue <= 80
{
ampsMaxValue = 80
ampsLabel.text = String(ampsMaxValue)
ampsprogressBar.progress = Float(0)
return
}
ampsMaxValue = ampsMaxValue - 1
ampsLabel.textColor = UIColor.white
ampsprogressBar.tintColor = UIColor.red
ampsLabel.text = String(ampsMaxValue)
v -= 0.1
ampsprogressBar.progress = Float(v)
}else{
if ampsMaxValue >= 475
{
ampsMaxValue = 475
ampsLabel.text = String(ampsMaxValue)
ampsprogressBar.progress = Float(1)
return;
}
ampsMaxValue = ampsMaxValue + 1
ampsLabel.textColor = UIColor.white
ampsprogressBar.tintColor = UIColor.red
ampsLabel.text = String(ampsMaxValue)
v += 0.1
ampsprogressBar.progress = Float(v)
}
I was able show the increased and decreased value by 0.1, but not solved it. need info about how calculate the exact increased and decreased value for the progress bar with respect to the label max an min value.
Try:
v = (ampsMaxValue - 80) / (475 - 80)
So I have recently uploaded a game to the app store and have been users reporting lags on older devices.
First of all, the game:
It is called "CURVE" (I am not sure if I am allowed to post its name here, but it would help you understand the problem).The idea of the game is that a ball moves up the screen and passes in gaps between walls. Also there are smaller balls that fall down from the main one, thus creating some sort of path.
Now, the lags. They occur in two main moments. First, when the path is created and second, when the player gets through the gaps.
When the player passes through the gaps, he collides with an invisible node. The collsion is noted, the score is updated. The node is then removed and the player proceeds.
I believe the lags occur when swift either creates a SpriteNode or deletes one. Any ideas as to how to deal with this problem?
Here is the code I use when spawning my obstacles - I think it is where the problem is
func createRocks() {
rockHeight = (frame.height * (heightOfRocks))
if gameState != .Dead {
switch spawnCount {
case 10:
levelUp()
case 20:
levelUp()
case 30:
levelUp()
case 40:
levelUp()
case 50:
levelUp()
case 60:
levelUp()
case 70:
levelUp()
case 80:
levelUp()
case 90:
levelUp()
case 100:
levelUp()
case 110:
levelUp()
case 120:
levelUp()
case 130:
levelUp()
case 140:
levelUp()
case 150:
levelUp()
default:
break
}
}
++spawnCount
let leftRock = SKShapeNode(rectOfSize: CGSize(width: (frame.width), height: (rockHeight)), cornerRadius: (cornerRad))
leftRock.fillColor = themeColor
leftRock.strokeColor = themeBorderColor
leftRock.lineWidth = themeBorderWidth
leftRock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ((frame.width) + (themeBorderWidth)), height: ((rockHeight) + (themeBorderWidth))))
leftRock.physicsBody?.dynamic = false
let rightRock = SKShapeNode(rectOfSize: CGSize(width: (frame.width), height: (rockHeight)), cornerRadius: (cornerRad))
rightRock.fillColor = themeColor
rightRock.strokeColor = themeBorderColor
rightRock.lineWidth = (themeBorderWidth)
rightRock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ((frame.width) + (themeBorderWidth)), height: ((rockHeight) + (themeBorderWidth))))
rightRock.physicsBody?.dynamic = false
leftRock.zPosition = CGFloat(10 + (21 * levelNumber))
rightRock.zPosition = CGFloat(10 + (21 * levelNumber))
// 2
let rockCollision = SKSpriteNode(color: UIColor.clearColor(), size: CGSize(width: (frame.width * 2), height: 32))
rockCollision.physicsBody = SKPhysicsBody(rectangleOfSize: rockCollision.size)
rockCollision.physicsBody?.dynamic = false
rockCollision.name = "scoreDetect"
addChild(leftRock)
addChild(rightRock)
addChild(rockCollision)
// 3
let rockWidth = frame.width
let yPosition = frame.height + leftRock.frame.height
let rockDistance = frame.width * (gapWidth)
let min = Int(((frame.width) * (wallWidth)) - (rockWidth / 2))
let max = Int(((frame.width) * (1.00 - ((wallWidth)))) - rockDistance - (rockWidth / 2))
let rand = min + Int(arc4random_uniform(UInt32(max - min)))
//GKRandomDistribution(lowestValue: min, highestValue: max)
let xPosition = CGFloat(rand)
// 4
leftRock.position = CGPoint(x: xPosition, y: yPosition)
rightRock.position = CGPoint(x: xPosition + rockWidth + rockDistance, y: yPosition)
rockCollision.position = CGPoint(x: xPosition, y: CGRectGetMidY(frame))
rockCollision.position = CGPoint(x: CGRectGetMidX(frame), y: yPosition + (rockCollision.size.height * 2))
let endPosition = frame.height + (leftRock.frame.height * 2)
let moveAction = SKAction.moveByX(0, y: -endPosition, duration: timeOfRockMovement)
let moveSequence = SKAction.sequence([moveAction, SKAction.removeFromParent()])
leftRock.runAction(moveSequence)
rightRock.runAction(moveSequence)
rockCollision.runAction(moveSequence)
}
func initRocks() {
let create = SKAction.runBlock { [unowned self] in
self.createRocks()
}
let wait = SKAction.waitForDuration(rockSpawnWait)
let sequence = SKAction.sequence([create, wait])
let repeatForever = SKAction.repeatActionForever(sequence)
runAction(repeatForever)
}
Your approach sounds very similar to a SpriteKit tutorial I wrote, but I had no lag at all there. SpriteKit is awfully fast, but it's possible your exact conditions are causing problems. Things to check:
1) Are you using texture atlases? These are faster to load than separate images in your bundle.
2) If you're re-using images a lot, can you store them in an SKTexture then load your nodes from there?
3) Are there other things happening, such as sounds playing? The iOS Simulator frequently has a brief lag when it plays its first sound in a game.
4) Is the path that gets created very complicated?
I am a beginner level programmer in iOS app development using Swift. Now I am facing the compile time issue "UInt8 is not convertible to CGFloat"
var numberOfAvatars:Int = 8
let count:Int = 1
let columns:Int = 3
let dimension:CGFloat = 84.0
var spacing:CGFloat = (avatarContentcroll.frame.size.width - columns * dimension)/(columns+1)
var scHeight:CGFloat = spacing + (numberOfAvatars/columns) * (dimension + spacing)
I've tried all the solutions out there and did many experiments. And I am not sure why still I am getting this error.
You have to do it explicitly as
var spacing:CGFloat = CGFloat((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = CGFloat(spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing))
or you can try converting every expression in CGFloat
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)
You must explicitly cast your Ints as CGFloats (via as), or construct them -- like so:
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)
My code was working properly but after including widget library, the output got messy. Can you please tell me what is the problem?
--Include sqlite
local sqlite3 = require "sqlite3"
local widget = require "widget"
W=display.contentWidth
H=display.contentHeight
--Open library database file
local path = system.pathForFile("library", system.ResourceDirectory)
db = sqlite3.open( path )
title = display.newText("LIBRARY", 0, 0, nil, 30)
title.x = W/2 title.y=50
--Access records of book table and display them as nx2 grid with title and cover.
xOffset = 10
yOffset = 100
items = display.newGroup()
for row in db:nrows("SELECT * FROM book") do
local text = row.bookName
local img = display.newImageRect("img"..row.bookId..".jpg", 200, 200 )
local t = display.newText(text, 0, 0, nil, 25)
t.x = img.x
t.y = img.contentBounds.yMax+50
item = display.newGroup(img,t)
item.anchorChildren = true
items:insert(item)
items.anchorChildren = true
if(xOffset==10) then
item.anchorX, item.anchorY = 0,0
item.x = xOffset
xOffset = W-10
item.y = yOffset
--t:setTextColor(0,255,0)
else
item.anchorX, item.anchorY = 1,0
item.x = xOffset
xOffset= 10
item.y = yOffset
yOffset = yOffset+item.contentBounds.yMax-50
--t:setTextColor(255,0,0)
end
end
EDIT:
Before including widget:
After including widget:
Ok, clearly the objects in the items display group are what have been affected - see how the library text isn't in that group and it stays where it is.
Just after the x and y of the item is set, print out its position. Also print the position and dimensions of the group - this will give you a better understanding of where the positioning is being taken from, or where the origin (0,0) is at.
local i = 1;
for row in db:nrows("SELECT * FROM book") do
local text = row.bookName
local img = display.newImageRect("img"..row.bookId..".jpg", 200, 200 )
local t = display.newText(text, 0, 0, nil, 25)
t.x = img.x
t.y = img.contentBounds.yMax+50
item = display.newGroup(img,t)
item.anchorChildren = true
items:insert(item)
items.anchorChildren = true
if(xOffset==10) then
item.anchorX, item.anchorY = 0,0
item.x = xOffset
xOffset = W-10
item.y = yOffset
--t:setTextColor(0,255,0)
else
item.anchorX, item.anchorY = 1,0
item.x = xOffset
xOffset= 10
item.y = yOffset
yOffset = yOffset+item.contentBounds.yMax-50
--t:setTextColor(255,0,0)
end
print("--------")
print("Item "..tostring(i).." is at ("..tostring(item.x)..","..tostring(item.y)..")")
print("Item group now at ("..tostring(items.x)..","..tostring(items.y)..")")
print("Item group size = width:"..tostring(items.contentWidth)..", height:"..tostring(items.contentHeight))
end
print("--------")
Compare the output for with/without including widget and you should be able to find the problem.