Skip to content

Instantly share code, notes, and snippets.

@AFMac
Created July 13, 2014 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AFMac/ffafacbf6bbe02f94cf2 to your computer and use it in GitHub Desktop.
Save AFMac/ffafacbf6bbe02f94cf2 to your computer and use it in GitHub Desktop.
Custom view drawing
# coding: utf-8
import ui
from console import hud_alert
import ui
class MyView (ui.View):
def __init__(self):
# This will also be called without arguments when the view is loaded from a UI file.
# You don't have to call super. Note that this is called *before* the attributes
# defined in the UI file are set. Implement `did_load` to customize a view after
# it's been fully loaded from a UI file.
iv = ui.ImageView(frame=self.bounds)
pass
def did_load(self):
# This will be called when a view has been fully loaded from a UI file.
pass
def will_close(self):
# This will be called when a presented view is about to be dismissed.
# You might want to save data here.
pass
def draw(self):
# This will be called whenever the view's content needs to be drawn.
# You can use any of the ui module's drawing functions here to render
# content into the view's visible rectangle.
# Do not call this method directly, instead, if you need your view
# to redraw its content, call set_needs_display().
# Example:
if self.path:
self.path.stroke()
# path = ui.Path.oval(0, 0, self.width, self.height)
# ui.set_color('red')
# path.fill()
#img = ui.Image.named('ionicons-beaker-256')
#img.draw(0, 0, self.width, self.height)
def layout(self):
# This will be called when a view is resized. You should typically set the
# frames of the view's subviews here, if your layout requirements cannot
# be fulfilled with the standard auto-resizing (flex) attribute.
pass
def touch_began(self, touch):
# Called when a touch begins.
x,y = touch.location
print x
path = ui.Path.oval(x, y, 20, self.height)
ui.set_color('red')
path.stroke()
# v.set_needs_display()
def touch_moved(self, touch):
# Called when a touch moves.
x,y = touch.location
#Runs from 0 - 380
#0 - 126 --> green
#127 - 252 --> yellow
#253 - 380 --> red
print x
if x < 191:
self.background_color = ((2*x/380),1,0,1)
else:
self.background_color = (1,1-(2*(x-190)/380),0,1)
def touch_ended(self, touch):
# Called when a touch ends.
x, y = touch.location
with ui.ImageContext(100,300) as ctx:
ov = ui.Path.oval(x, y, 20, 40)
ui.set_color('black')
ov.fill()
img = ctx.get_image()
img.draw(x, y)
def keyboard_frame_will_change(self, frame):
# Called when the on-screen keyboard appears/disappears
# Note: The frame is in screen coordinates.
pass
def keyboard_frame_did_change(self, frame):
# Called when the on-screen keyboard appears/disappears
# Note: The frame is in screen coordinates.
pass
v = ui.load_view('behave')
v.present('sheet')
@cclauss
Copy link

cclauss commented Jul 13, 2014

The line v = ui.load_view('behave') raises an error because there is no .pyui file.

This does not load your .pyui file but it should at least get you started...

# coding: utf-8

import ui

class MyView (ui.View):
    def __init__(self):
        self.iv = ui.ImageView(frame=self.bounds)
        self.path = None

    def draw(self):
        if self.path:
            self.path.stroke()

    def touch_began(self, touch):
        # Called when a touch begins.
        x,y = touch.location
        print('Begin:', x, y)
        self.path = ui.Path.oval(x, y, 20, self.iv.height)
        ui.set_color('red')
        self.path.stroke()
#        v.set_needs_display()

    def touch_moved(self, touch):
        # Called when a touch moves.
        x,y = touch.location

        #Runs from 0 - 380  # Where does 380 come from?
        #0 - 126 --> green
        #127 - 252 --> yellow
        #253 - 380 --> red

        print('Moved:', x, y)
        self.background_color = ((2*x/380),1,0,1) if x < 191 else (1,1-(2*(x-190)/380),0,1)

    def touch_ended(self, touch):
        # Called when a touch ends.
        x, y = touch.location

        with ui.ImageContext(100,300) as ctx:
            ov = ui.Path.oval(x, y, 20, 40)
            ui.set_color('black')
            ov.fill()
            img = ctx.get_image()
            img.draw(x, y)

#v = ui.load_view('behave')
v = MyView()
v.present('sheet')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment