-
-
Save AFMac/ffafacbf6bbe02f94cf2 to your computer and use it in GitHub Desktop.
Custom view drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...