Page 1 of 1

vScroll maths

Posted: Wed Mar 20, 2013 6:32 am
by Nakia
Afternoon everyone,

I am looking for some advice on how to calculate the vScroll number I need to position a fld in the centre of the scrolling fld (so the iOS Keyboard doesn't cover it)
What I am thinking is there must be some way mathematically to calculate the vScroll value needed so the currently focused fld is placed in the centre of the screen and this must have been done by someone else before...

I just can't seem to get into my head how to go about it....

I hope this makes sense :)

Re: vScroll maths

Posted: Wed Mar 20, 2013 11:23 am
by Mark
Hi,

This is actually very easy. The top of the keyboard in pixels is always the same, but you need to take different cases into account, such as whether the keyboard is visible or not, the orientation of the device and the resolution. You can write a simple function, which returns the top of the keyboard in pixels depending on these cases.

Kind regards,

Mark

Re: vScroll maths

Posted: Wed Mar 20, 2013 4:34 pm
by jacque
See the screenrect entry in the dictionary. If you use "the effective working screenrect" it will give you the available dimensions while the keyboard is showing. Item 4 of the effective working screenrect will be the top of the keyboard.

If your field is short enough to fit, just set the bottom of it to item 4 of that rectangle, and it will sit right on top of the keyboard. If the field is too tall to fit, you'll need to make some scrolling adjustments or else resize it to fit the available height.

If you need more help, let us know.

Re: vScroll maths

Posted: Wed Mar 20, 2013 10:06 pm
by Nakia
Thanks All,

I suppose I should provide a little more context.. :D
I have a full card size scrolling region (That has a content rect maybe 3 x card lengths) on an iOS App and this region contains multiple button and iOS Input Flds.
When the user focuses on one of the objects I want the scroll value of the scrolling region to change so the objects the user focused on is above the keyboard and visible (my App on runs in Portrait mode)

So can anyone offer some guidance on how the math would work to calculate the vScroll value needed to set focused object to the middle of the scrolling region...

Re: vScroll maths

Posted: Wed Mar 20, 2013 11:23 pm
by Simon
Hi Nakia,
I get the problem and ouch.
I think you have to figure out before hand the vScroll of the fld when it's in the middle and record it.
on openField
set the vScroll of grp"myScrollGrp" to myFldInView --myFldInView is the recorded value,maybe custom prop?

That is terrible...Must be a better way

Simon

Re: vScroll maths

Posted: Thu Mar 21, 2013 12:07 am
by bn
Hi Nakia,

is the scrolling field a native LiveCode list field in a scrolling group? Is the line hilited? or is there a button always on same height as the focused object? Do you use a native field as placeholder for the iOS input field?

In other words do you have a location of the focused object or a proxy for the location?

the math is not that complicated. What is complicated is to understand what kind of objects you are scrolling. A screenshot would probably help to understand what you are dealing with.

Kind regards
Bernd

Re: vScroll maths

Posted: Thu Mar 21, 2013 12:09 am
by jacque
Pass this the long name or ID of the object you want to be visible above the keyboard:

Code: Select all

on setscroll pObj
  put item 4 of the effective working screenrect into tKeyboardTop
  put the bottom of pObj + tKeyboardTop  into tTargetScroll -- may need to fiddle with this a little
  set the scroll of grp 1 to tTargetScroll
end setscroll
This will fail if there isn't enough room at the bottom of the group to scroll upward past its last object. That is, you can't scroll the group any higher than the last thing in it. So if your last object is a field, it isn't going to scroll upward enough.

I'd be tempted to just move the whole group maybe. This may get you close, you might have to adjust the scroll of the group a little more:

Code: Select all

on setscroll pObj
  put item 4 of the effective working screenrect into tKeyboardTop
  put the height of this cd - the top of grp 1 - tKeyboardTop into tOffset
  set the scroll of grp 1 to the top of pObj -- probably needs adjusting
  set the top of grp 1 to the top of grp 1 - tOffset
end setscroll
Also, you could check the bottom of the object to see if it's already high enough, and you won't have to do anything if it is. The dimensions are returned at the current card coordinates, not the offset of the scrolling group, so all you have to check is "the bottom of field whatever" to know where it is.

Store the correct location of the group before you move it so that you can put it back when the keyboard goes away.

Simon posted before I could. All the above assumes regular LiveCode groups and fields. If you're using native controls there's more to it. I think you'll need to destroy the native controls and rebuild them after you've moved the group or the objects.

Re: vScroll maths

Posted: Thu Mar 21, 2013 12:33 am
by Nakia
The Scrolling object is an iOS Scrolling Group that contains iOS Input Flds and Buttons (content length of around 3 x card length but this can and will vary) :shock:

- I have attached some screen shots as requested.
So, looking at the screen shots the user can scroll that group up and down (iOS Scrolling Group) and when the user presses on a button/input fld that is currently sitting where the keyboard will cover it
I want to be able to calculate the vScroll I need to set on the scrollingGroup so the focued object is above where the keyboard sits...
ScreenShot1.JPG
ScreenShot2.JPG

Re: vScroll maths

Posted: Thu Mar 21, 2013 1:13 am
by Nakia
I did try this ... Just seems to scroll to the bottom of the scroller

Code: Select all

on setscroll pObj
  put item 4 of the effective working screenrect into tKeyboardTop
  put the bottom of pObj + tKeyboardTop  into tTargetScroll -- may need to fiddle with this a little
  set the scroll of grp 1 to tTargetScroll
end setscroll

Re: vScroll maths

Posted: Thu Mar 21, 2013 2:21 am
by bn
Hi Nakia,

I made a little stack that simulates your problem.

The hardest part is when the target is at the bottom of the scrolling group and changing the scroll will not scroll the target into view because there is nothing left to scroll.
In that case I change the height of the group (make is smaller) then you can see the target above the keyboard. The keyboard is the red line.
The original rect of the scrolling group is restored after 3 seconds via script.
Of course you would have to adapt it to your real project.
setScrollOfGroup.livecode.zip
(2.13 KiB) Downloaded 294 times
Look at the comments in the code of the button "scroll"

I hope it makes sense, in the little testing I did it worked.

Kind regards
Bernd

Re: vScroll maths

Posted: Mon Mar 25, 2013 10:35 am
by bn
Nakia,

did you solve the problem? If so how?

Kind regards
Bernd

Re: vScroll maths

Posted: Mon Mar 25, 2013 12:12 pm
by jmburnod
Salut Bernd,

I tested setScrollOfGroup with the simulator.
The scroll work fine.
The script of btn "Scroll" with openfield on the cd script does the job automatically. However, sometimes the vscroll of the group scroll up when i write in the field.

I put the modified stack in attachment

King regards
Jean-Marc

Re: vScroll maths

Posted: Mon Mar 25, 2013 12:17 pm
by jmburnod
Hi Bernd,
I said
However, sometimes the vscroll of the group scroll up when i write in the field.
It seems that only concern the physical keyboard :D

Re: vScroll maths

Posted: Mon Mar 25, 2013 2:01 pm
by bn
Merci Jean-Marc for looking into this.

Kind regards
Bernd

Re: vScroll maths

Posted: Tue Mar 26, 2013 12:21 am
by Nakia
Thanks for everyone help with this.

I have not got back to testing of yet but hope to do so in the coming days (busy with another part of the project right now)