vScroll maths

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

vScroll maths

Post by Nakia » Wed Mar 20, 2013 6:32 am

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 :)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: vScroll maths

Post by Mark » Wed Mar 20, 2013 11:23 am

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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: vScroll maths

Post by jacque » Wed Mar 20, 2013 4:34 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: vScroll maths

Post by Nakia » Wed Mar 20, 2013 10:06 pm

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...

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: vScroll maths

Post by Simon » Wed Mar 20, 2013 11:23 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: vScroll maths

Post by bn » Thu Mar 21, 2013 12:07 am

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: vScroll maths

Post by jacque » Thu Mar 21, 2013 12:09 am

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: vScroll maths

Post by Nakia » Thu Mar 21, 2013 12:33 am

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

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: vScroll maths

Post by Nakia » Thu Mar 21, 2013 1:13 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: vScroll maths

Post by bn » Thu Mar 21, 2013 2:21 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: vScroll maths

Post by bn » Mon Mar 25, 2013 10:35 am

Nakia,

did you solve the problem? If so how?

Kind regards
Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: vScroll maths

Post by jmburnod » Mon Mar 25, 2013 12:12 pm

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
Attachments
setScrollOfGroup001.livecode.zip
(2.85 KiB) Downloaded 301 times
https://alternatic.ch

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: vScroll maths

Post by jmburnod » Mon Mar 25, 2013 12:17 pm

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
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: vScroll maths

Post by bn » Mon Mar 25, 2013 2:01 pm

Merci Jean-Marc for looking into this.

Kind regards
Bernd

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: vScroll maths

Post by Nakia » Tue Mar 26, 2013 12:21 am

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)

Post Reply