iOS Scrolling Field Effects All Scrollers

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
JacobS
Posts: 58
Joined: Mon Aug 20, 2012 8:41 pm

iOS Scrolling Field Effects All Scrollers

Post by JacobS » Wed Dec 19, 2012 6:45 pm

Hi all,

I have not been able to find any help on this, but it seems like someone must have run into it at some point.
The problem is that I have two iOS native scrollers that I have created on the same card and when I swipe to move one of the fields, the other field moves as well. I know that the problem code is this (inside of my card script):

Code: Select all

on scrollerDidScroll hOffset, vOffset
  set the vScroll of group "scroll1" to vOffset
  set the vScroll of group "scroll2" to vOffset
end scrollerDidScroll
But I'm not sure how to fix this. I tried to find out if I could get the mouseLoc (or the touchLoc) and then use an if..else statement to send the update to only one of the scroll groups based on the location, but I have been unsuccessful so far. I'm not sure what else I can do to fix it. Does anyone know of this problem or how to overcome it?

Thanks
Jacob

strongbow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 146
Joined: Mon Jul 31, 2006 1:39 am
Contact:

Re: iOS Scrolling Field Effects All Scrollers

Post by strongbow » Wed Dec 19, 2012 7:13 pm

I've done this before and I'm pretty sure that you can test the hOffset for the "left" of the field/obj being scrolled. Assuming you're only scrolling vertically anyway.

something like

Code: Select all

on scrollerDidScroll pOffsetX, pOffsetY
   -- check X position for scroller.
   -- NOTE that this will not work if scrollers are above each other. 
   --      Works here because they're beside each other on X axis.
   if pOffsetX = the left of fld "field1" then
      set the vScroll of fld "field1" to pOffsetY
   else if pOffsetX = the left of fld "field2" then
      set the vScroll of fld "field2" to pOffsetY
   end if
end scrollerDidScroll
HTH

cheers, Alan

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: iOS Scrolling Field Effects All Scrollers

Post by Dixie » Wed Dec 19, 2012 7:17 pm

Jacob...

You need to be able to tell the scrollerDisScroll handler which group you have been scrolling... So use the iphoneControlTarget() function, it will return the ID of the control that sent the message...

You can then use that ID to determine which scoller should scroll..:-)

be well

Dixie

strongbow
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 146
Joined: Mon Jul 31, 2006 1:39 am
Contact:

Re: iOS Scrolling Field Effects All Scrollers

Post by strongbow » Wed Dec 19, 2012 7:22 pm

Right - even better!

The key is (from the Dictionary):
The scrollerDidScroll message is sent to the object containing the script that created the scroller control.

JacobS
Posts: 58
Joined: Mon Aug 20, 2012 8:41 pm

Re: iOS Scrolling Field Effects All Scrollers

Post by JacobS » Wed Dec 19, 2012 7:31 pm

Awesome! I got it working.

Thanks everyone for your very quick responses. I was hoping that there was a function like the one that Dixie described, but I guess I just wasn't looking in the right places. And thanks for you help too, Alan. The problem I had with your code was that my two scrollers were right on top of each other. But it works now.

I ended up using the iphoneControlTarget() function and then checked it with the scroller ID's that I had saved in card-local variables and then updated the appropriate scrollers.

Many thanks again for your help
Jacob

scotttyang
Posts: 125
Joined: Sat Jan 31, 2009 12:01 am

Re: iOS Scrolling Field Effects All Scrollers

Post by scotttyang » Thu Dec 20, 2012 7:15 am

Jacob,
Funny that I was trying to figure out how to scroll two scrollers on the same card and got stuck at the same point you were. Can you post the card script, or even the stack with the 2 scollers?

Thanks.

Scott

JacobS
Posts: 58
Joined: Mon Aug 20, 2012 8:41 pm

Re: iOS Scrolling Field Effects All Scrollers

Post by JacobS » Thu Dec 20, 2012 2:34 pm

Scott,

I had two scrollers that were made by the functions "createChScroller" and "createPPScroller". Their ID's were stored in two local variables, scrollidCh and scrollidPP.
Here's all of the important code that I had in my card

Code: Select all

local scrollidCh
local scrollidPP

on preOpenCard
createChScroller   //Create the two scrollers
createPPScroller
end preOpenCard

on closeCard
   if environment() is not "mobile" then exit closeCard
   set the vScroll of group "scrollGroupChart" to 0
   set the vScroll of group "scrollGroupPorP" to 0
   iphoneControlDelete scrollidCh
   iphoneControlDelete scrollidPP
end closeCard

Code: Select all

on scrollerBeginDrag
   put empty
end scrollerBeginDrag

on scrollerScrollToTop
   iphoneControlSet scrollidCh, "vscroll", 0  
   iphoneControlSet scrollidPP, "vscroll", 0  
end scrollerScrollToTop

on scrollerDidScroll hOffset, vOffset
   put iphoneControlTarget() into tID
   --Check what control sent the scroll message. Only update that control!
   if tID=scrollid then
      set the vScroll of group "scrollGroup" to vOffset
   else if tID=scrollidCh then
      set the vScroll of group "scrollGroupChart" to vOffset
   else if tID=scrollidPP then
      set the vScroll of group "scrollGroupPorP" to vOffset
   end if
end scrollerDidScroll
And then I also had code to destroy/recreate the scroller on the fly. This was called during run-time in order to create/destroy the controls whenever the user came to the card. I was using the scroller to overlay a list field in order for user to select items from a list: I wanted to prep the selections for the user, so that's what the "refresh" functions do.
Also, the two fields that I wanted to scroll on are called "FieldChart" and "FieldPorP"

Code: Select all

on createChScroller
   if environment() is not "mobile" then exit createChScroller
   put the formattedHeight of fld "FieldChart" into fldHeight
   put the width of fld "FieldChart" into fldWidth
   
   set the height of fld "FieldChart" to fldHeight
   set the top of field "FieldChart" to the top of group "scrollGroupChart"
   set the unboundedVScroll of group "scrollGroupChart"  to true
   set the vScroll of group "scrollGroupChart" to 0
   
   iphoneControlCreate "scroller"
   put the result into scrollidCh
   
   iphoneControlSet scrollidCh, "rect", the rect of group "scrollGroupChart"
   iphoneControlSet scrollidCh, "contentRect", (0, 0, fldWidth, fldHeight)
   iphoneControlSet scrollidCh, "visible", true
   iphoneControlSet scrollidCh, "canBounce", true
   iphoneControlSet scrollidCh, "declerationRate", fast
   iphoneControlSet scrollidCh, "scrollingEnabled", true
   iphoneControlSet scrollidCh, "canScrollToTop",true
   iphoneControlSet scrollidCh, "canCancelTouches",true
   iphoneControlSet scrollidCh, "delayTouches", true
   iphoneControlSet scrollidCh, "vIndicator", true
   iphoneControlSet scrollidCh, "indicatorStyle", black
   iphoneControlSet scrollidCh, "indicatorInsets",  "0,0,0,0"
   iphoneControlSet scrollidCh, "hscroll", 0
   iphoneControlSet scrollidCh, "vscroll", 0
   
   repeat with count = 1 to the number of lines of fld "FieldChart"
      set the textShift of line count of fld "FieldChart" to 0
   end repeat
end createChScroller

on refreshCh
   //This is used to repaint the scroller selector
   wait for .01 sec
   set the hilitedlines of fld "FieldChart" to the hilitedlines of fld "FieldChart"
end refreshCh

on destroyChScroller
   if environment() is not "mobile" then exit destroyChScroller
   set the vScroll of group "scrollGroupChart" to 0
   iphoneControlDelete scrollidCh
end destroyChScroller

on createPPScroller
   if environment() is not "mobile" then exit createPPScroller
   put the formattedHeight of fld "FieldPorP" into fldHeight
   put the width of fld "FieldPorP" into fldWidth
   
   set the height of fld "FieldPorP" to fldHeight
   set the top of field "FieldPorP" to the top of group "scrollGroupPorP"
   set the unboundedVScroll of group "scrollGroupPorP"  to true
   set the vScroll of group "scrollGroupPorP" to 0
   
   iphoneControlCreate "scroller"
   put the result into scrollidPP
   
   iphoneControlSet scrollidPP, "rect", the rect of group "scrollGroupPorP"
   iphoneControlSet scrollidPP, "contentRect", (0, 0, fldWidth, fldHeight)
   iphoneControlSet scrollidPP, "visible", true
   iphoneControlSet scrollidPP, "canBounce", true
   iphoneControlSet scrollidPP, "declerationRate", fast
   iphoneControlSet scrollidPP, "scrollingEnabled", true
   iphoneControlSet scrollidPP, "canScrollToTop",true
   iphoneControlSet scrollidPP, "canCancelTouches",true
   iphoneControlSet scrollidPP, "delayTouches", true
   iphoneControlSet scrollidPP, "vIndicator", true
   iphoneControlSet scrollidPP, "indicatorStyle", black
   iphoneControlSet scrollidPP, "indicatorInsets",  "0,0,0,0"
   iphoneControlSet scrollidPP, "hscroll", 0
   iphoneControlSet scrollidPP, "vscroll", 0
   
   repeat with count = 1 to the number of lines of fld "FieldPorP"
      set the textShift of line count of fld "FieldPorP" to 0
   end repeat
end createPPScroller

on refreshPP
   //This is used to repaint the scroller selector
   wait for .01 sec
   set the hilitedlines of fld "FieldPorP" to the hilitedlines of fld "FieldPorP"
end refreshPP

on destroyPPScroller
   if environment() is not "mobile" then exit destroyPPScroller
   set the vScroll of group "scrollGroupPorP" to 0
   iphoneControlDelete scrollidPP
end destroyPPScroller
Is that what you're looking for? Hope this helps.

Jacob

Post Reply