Webmaster4o
Sep 23, 2015 - 18:25
I was just playing around with things, and I modified one of @omz's gists to allow infinite scroll. I noticed when more colors are added at the bottom, sometimes the scrollbar disappears and doesn't come back even when I scroll again. I'm using this code:
import ui
from random import randint
import console
import clipboard
# Generate some random hex colors:
colors = ['#%X%X%X' % (randint(0,255), randint(0,255), randint(0,255)) for i in xrange(50)]
offset = 0
class SVDelegate (object):
def scrollview_did_scroll(self, sv):
scroll = sv.content_offset[1]
#how far you can scroll before hitting the bottom
maxscroll = 0
for sub in sv.subviews:
maxscroll += sub.height
sv.content_size = 0, maxscroll
#see if user is within 500 pixels of end of scroll
if scroll > maxscroll-sv.height-500:
print 'change'
colors2 = ['#%X%X%X'%(randint(0,255),randint(0,255),randint(0,255)) for i in xrange(50)]
for i, c in enumerate(colors2):
swatch = ui.Button(frame=(0, i*80+maxscroll, 400, 80), background_color=c)
swatch.flex = 'w'
swatch.action = tapped
sv.add_subview(swatch)
#Add buttons for all the colors to a scroll view:
scroll_view = ui.ScrollView(frame=(0, 0, 400, 480))
scroll_view.content_size = (0, len(colors) * 80)
scroll_view.delegate = SVDelegate()
def tapped(sender):
r, g, b, a = sender.background_color
hex_color = '#%X%X%X' % (int(r*255), int(g*255), int(b*255))
clipboard.set(hex_color)
console.hud_alert(hex_color + ' copied')
for i, c in enumerate(colors):
swatch = ui.Button(frame=(0, i*80, 400, 80), background_color=c)
swatch.flex = 'w'
swatch.action = tapped
scroll_view.add_subview(swatch)
scroll_view.name = 'Random Color Picker'
scroll_view.present('sheet')
Can anyone else replicate this behavior?