Horizontal ScrollView in Corona SDK - lua

I created a scroll view in corona sdk,but i have some problem with it i need it only to scroll horizontally. Any Help?
local widget = require("widget")
local rect1 = display.newRect(20,100,150,100)
local rect2 = display.newRect(200,100,150,100)
local rect3 = display.newRect(380,100,150,100)
local rect4 = display.newRect(580,100,150,100)
local rect5 = display.newRect(780,100,150,100)
local scrollView = widget.newScrollView{ left = 0, width = 0, height = 200, }
scrollView:insert(rect1);
scrollView:insert(rect2);
scrollView:insert(rect3);
scrollView:insert(rect4);
scrollView:insert(rect5);

According to Corona SDK API, you can do the following :
scrollView.verticalScrollDisabled = true

local scrollView = widget.newScrollView{ left = 0, width = 0, height = 200, verticalScrollDisabled = true,}

Related

Corona widget.newButton passing data to function

I have scoured the documentation and google for a solution but none was found.
Simply: I want to pass data to a function, when a button is pressed.
Issue: The buttons are dynamically created, so the data must be presented when the button is built.
The code looks like this;
myButton = widget.newButton({
x = width * 0.875,
y = height * heightChange,
width = width * 0.18,
height = height * 0.09,
defaultFile = "Assets/Images/button_up.png",
overFile = "Assets/Images/button_down.png",
onEvent = func_myFunction
})
But I want to do something like;
onEvent = func_myFunction("My Data")
or
onEvent = func_myFunction, myData
neither of which work. Any ideas?
It's simple you just need to add an eventlistener to the end of your code so when it's clicked your data would execute.
myButton = widget.newButton({
x = width * 0.875,
y = height * heightChange,
width = width * 0.18,
height = height * 0.09,
defaultFile = "Assets/Images/button_up.png",
overFile = "Assets/Images/button_down.png",
onEvent = func_myFunction
})
myButton:addEventListener("touch",onEvent);
I hope this fixes your problem.

How to create UIStackView from code correctly

I can't understand why this layout won't render the way i need it. Completing the requirement on android with linearlayouts should be equivalent to this?
What i get:
What i need:
My code:
// my stackview only renders at all if i pass any height > 0 && width > 0,
// but i would love this to be as small as possible, given that all children
// are rendered.
var stackView = new UIStackView(View.Frame);
stackView.Axis = UILayoutConstraintAxis.Vertical;
stackView.Distribution = UIStackViewDistribution.Fill;
foreach (var deviceVm in devices)
{
// i would prefer this not to have a rect parameter and scale as needed.
var descriptionLabel = new UILabel(new CGRect(0, 0, View.Bounds.Width, 20));
descriptionLabel.Text = deviceVm.Description;
descriptionLabel.TextColor = UIColor.Blue;
descriptionLabel.ContentMode = UIViewContentMode.ScaleToFill;
stackView.AddArrangedSubview(descriptionLabel);
}
// i assume this should make it as small as possible, as big as necessary?
stackView.SizeToFit();
var scrollView = new UIScrollView(View.Frame);
scrollView.BackgroundColor = UIColor.Yellow;
scrollView.AddSubview(stackView);
View.AddSubview(scrollView);
Can someone spot a mistake i am doing here? Am i configuring the StackView incorrectly or is this control not the equivalent of LinearLayout(android) / StackPanel(wpf) ?
PS: Sorry for eye cancer paint pictures.

UI Text Field postioning Swift

I am a newbie to ios development and am fairly confused about how to position a UI Text field in swift. I am working with sprite kit and am creating a gaem that involves inputing a quadratic equation. I want to position my text fields along side my labels so it say y=_x^2+x+. When I run the following code, my text fields are in the top left corner! What should I do?
override func didMoveToView(view: SKView) {
self.backgroundColor = SKColor.blueColor()
//create y = label
let yLabel = SKLabelNode(fontNamed:"Note-Worthy-Bold")
yLabel.text = "Y = ";
yLabel.fontColor = SKColor.redColor()
yLabel.fontSize = 30;
yLabel.position = CGPoint(x:CGRectGetMidX(self.frame)-300, y:CGRectGetMidY(self.frame)+100);
self.addChild(yLabel)
//create text fielld for coefiecent of x^2 variable
var xSqrCoefInput = UITextField(frame:CGRectMake(10, 10, 30, 10))
xSqrCoefInput.backgroundColor = UIColor.orangeColor()
self.view?.addSubview(xSqrCoefInput)
//create x^2 Label
let xSqrLabel = SKLabelNode(fontNamed:"Note-Worthy-Bold")
xSqrLabel.text = "x^2+";
xSqrLabel.fontColor = SKColor.redColor()
xSqrLabel.fontSize = 30;
xSqrLabel.position = CGPoint(x:CGRectGetMidX(self.frame)-150, y:CGRectGetMidY(self.frame)+100);
self.addChild(xSqrLabel)
//create text fielld for coefiecent of x variable
var xCoefInput = UITextField(frame:CGRectMake(10, 10, 30, 10))
self.view?.addSubview(xCoefInput)
xCoefInput.backgroundColor = UIColor.orangeColor()
//create x label
let xLabel = SKLabelNode(fontNamed:"Note-Worthy-Bold")
xLabel.text = "x+";
xLabel.fontColor = SKColor.redColor()
xLabel.fontSize = 30;
xLabel.position = CGPoint(x:CGRectGetMidX(self.frame)-0, y:CGRectGetMidY(self.frame)+100);
self.addChild(xLabel)
//create text fielld for constant
var constantInput = UITextField(frame:CGRectMake(10, 10, 30, 10))
self.view?.addSubview(constantInput)
constantInput.backgroundColor = UIColor.orangeColor()
}
CGRectMake takes in 4 parameters.
CGRectMake(x position, y position, width, height)
The default starting position is the top-left corner. Your current textfield position is (10,10), so basically you just have to change the x and y positions for all the textfields.
var xSqrCoefInput = UITextField(frame:CGRectMake(10, 10, 30, 10))
If you wish to place the textfield next to the labels, it would be best to get the position of the labels and set the position from there.

Lua - Corona SDK - Text not appearing inside a display Object

I am trying to move a display object that is hiding over the right side of the screen, into the scene. It works wonderfully with images (i.e. the background), but not with texts (the coords seem correct from debugging them with print(), but they never display, I already tried the obj:toFront).
I thought that they may work inside display objects, so I put everything in a display Object: Nothing. Just the text? Neither. Anyone knows why/how to override this?
function tscreen:init()
local textGroup = display.newGroup()
local menuBackground = self:getBtn("src/bgMenu.png")
menuBackground.isVisible = false
menuBackground.anchorX = 0.5
menuBackground.anchorY = 0.5
self.menuBackground = menuBackground
local optionsText = {
parent = textGroup,
text = "Hello World",
x = centerX,
y = centerY,
width = 128,
font = native.systemFontBold,
fontSize = 14,
align = "center"
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 0, 0 )
setPos(textGroup, W, 0)
--setPos() is a custom function that assigns x and y coords
textGroup.isVisible = false
self.textGroup = textGroup
end
function tscreen:show()
local menuBackground = self.menuBackground
local textGroup = self.textGroup
local inTime = 1200
setPos(menuBackground, 2*W + centerX, centerY)
menuBackground.isVisible = true
setPos(textGroup, W, 0)
textGroup.isVisible = true
self:cancelTween(menuBackground)
self:cancelTween(textGroup)
menuBackground.tween = transition.to(menuBackground, {time = inTime, transition = easing.outExpo, x = centerX,
onComplete = function()
tscreen:cancelTween(menuBackground)
end
})
textGroup.tween = transition.to(textGroup, {time = inTime, transition = easing.outExpo, x = 0,
onComplete = function()
tscreen:cancelTween(textGroup)
print(getPos(textGroup), textGroup.width, textGroup.height)
end
})
end
I have the starters edition of Corona, so I don't have the recently implemented Composer API.
Maybe this isn't the most appropriate site to post this query since there already is a Corona SDK forum, but I'm trying anyway.
I'm not seeing anything wrong, but a group should not be necessary. Verify that the text can be seen ever: in the init(), do
local optionsText = {
text = "Hello World",
x = 0,
y = 100,
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 1, 1 )
If you can't see the text then something else is going on, maybe your init() is not being called or such. Once you see it, change the parameters to what you want (fill color etc), and test. If still works, add a transition, right after, in the init():
local easeXto100 = {
time = 2000,
transition = easing.outExpo,
x = 100,
onComplete = function() print('did you see text move from x=0 to 100?') end
}
transition.to(workText, easeXto100)
If you see it move, then move the relevant parts of code to your show(), if now it disappears this will give you clue.

Corona SDK ScrollView preventing the masked area from being touched?

I want to set my newscrollview to a specific height
there are two ways to do that
1 - either by setting the height property,
or
2 - by using masks
the problem is when you change the ScollView size it changes only the look of the scrollview and it will not prevent the hidden area from being touch enabled? is this a bug?
here is the code
local widget = require "widget"
local myscrollview = widget.newScrollView{
height = 300,
maskFile="mask.png"
}
local obj1 = display.newRect(0, 0, display.contentWidth, display.contentHeight)
obj1:setReferencePoint( display.TopLeftReferencePoint )
obj1:setFillColor(255,27,27)
obj1.x = 0
obj1.y = 0
local obj2 = display.newRect(0, 0, display.contentWidth, display.contentHeight)
obj2:setReferencePoint( display.TopLeftReferencePoint )
obj2:setFillColor(0,27,27)
obj2.x = 0
obj2.y = 260
myscrollview:insert(obj1)
myscrollview:insert(obj2)
is there any way to prevent the masked area from being touched?
Thanks in Advance
UPDATED here is the mask.png:
I think I have figured it out :)
you have to add this line of code
myscrollview.isHitTestMasked = true
see if you want the Documentation of
isHitTestMasked
Thanks to anyone tried to solve :)

Resources