I tried touching touch related methods in the scene module of pythonista

Motivation

I used ui on pythonista to make simple apps. I used to use the launcher that switches apps with one flick, so I thought about reproducing it, but I had a hard time because there was not much information in Japanese about touch, so I thought about leaving it as a memorandum.

environment

pythonista 3 ver. 3.2 iphone 11 iOS13.2.3

program

I'm thinking of creating an app that gets the direction of the flick and selects the app to launch. Therefore, the program is designed to get the direction of the flick. The actual behavior is just like learning when a method is called.

touchTest.py


import scene
import numpy as np

class MyScene(scene.Scene):
    def setup(self):
        self.backgroud_color = '#3a3aff'

        #Start and end of flick vector
        self.startpoint = np.array([0, 0])
        self.endpoint = np.array([0, 0])
    
    def touch_began(self, touch):
        '''Method called when touch is started'''
        #Remember the start point of the vector
        self.startpoint = np.array([touch.location.x, touch.loctaion.y])
        #To confirm the touch, print according to the touched location.
        if touch.location.x > self.size[0] / 2:     #When the touch is on the right half of the screen
            print('Right')
        else:
            print('Left')
    
    def touch_moved(self, touch):
        '''A method that is called repeatedly while touching'''
        print('Moving !')
    
    def touch_ended(self, touch):
        '''Method called when you speak your finger during touch'''
        #Get the end point of a vector and find the flicked vector
        self.endpoint = np.array([touch.location.x, touch.loctaion.y])
        vec = self.endpoint - self.startpoint
        #When the start point of the vector is brought to the origin, the display is changed depending on which quadrant the end point is in.
        if vec[0] >= 0 and vec[1] > 0:
            print('Up right !')
        elif vec[0] < 0 and vec[1] >= 0:
            print('Up left')
        elif vec[0] <= 0 and vec[1] < 0:
            print('Down left')
        elif vec[0] > 0 and vec[1] <= 0:
            print('Down right')
        
if __name__ == '__main__':
    scene.run(MyScene())

Supplement etc.

When executed, a blue screen will appear. If you touch or flick and look at the console display, you can see how it works. If the launcher application can be realized, it may be upgraded to Qiita. .. ..

I hope more people will enjoy pythonista as a hobby like myself.

Recommended Posts

I tried touching touch related methods in the scene module of pythonista
I tried to touch the API of ebay
I tried running the sample code of the Ansible module
I tried the accuracy of three Stirling's approximations in python
I tried the asynchronous server of Django 3.0
I tried to touch the COTOHA API
I tried the pivot table function of pandas
I tried cluster analysis of the weather map
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
[I tried using Pythonista3] Importing my own module
I tried using the Datetime module by Python
I want to see a list of WebDAV files in the Requests module
I tried adding a Python3 module in C
I tried using the image filter of OpenCV
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
[Linux] I learned LPIC lv1 in 10 days and tried to understand the mechanism of Linux.
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried to put HULFT IoT (Agent) in the gateway Rooster of Sun Electronics
[First data science ⑥] I tried to visualize the market price of restaurants in Tokyo
I tried to graph the packages installed in Python
Specify the lighting Model of SCN Material in Pythonista
I tried to summarize the basic form of GPLVM
I tried the MNIST tutorial for beginners of tensorflow.
I tried to touch the CSV file with Python
I tried using the API of the salmon data project
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried scraping the advertisement of the pirated cartoon site
[Note] I can't call the installed module in jupyter
I tried the simplest method of multi-label document classification
I tried to classify the voices of voice actors
I tried to summarize the string operations of Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to put HULFT IoT (Edge Streaming) in the gateway Rooster of Sun Electronics
[Natural language processing] I tried to visualize the remarks of each member in the Slack community
I tried to find the trend of the number of ships in Tokyo Bay from satellite images.
I tried to find the entropy of the image with python
[Horse Racing] I tried to quantify the strength of racehorses
I tried "gamma correction" of the image with Python + OpenCV
I tried to get the location information of Odakyu Bus
I participated in the translation activity of Django official documents
I tried the super-resolution algorithm "PULSE" in a Windows environment
I tried to find the average of the sequence with TensorFlow
I tried refactoring the CNN model of TensorFlow using TF-Slim
I wrote the basic operation of Seaborn in Jupyter Lab
I tried face recognition of the laughter problem using Keras.
I tried to summarize the code often used in Pandas
I measured various methods of interprocess communication in multiprocessing of python3
I tried programming the chi-square test in Python and Java.
Display the timetable of the Morioka bus location system in Pythonista
I tried touching the multi-vendor router control API library NAPALM
I tried to summarize the commands often used in business
I tried to implement the mail sending function in Python
[Machine learning] I tried to summarize the theory of Adaboost
I wrote the basic operation of Numpy in Jupyter Lab.
[Report] I tried cvusk's "Judgment of authorship in Aozora Bunko".
I tried to fight the Local Minimum of Goldstein-Price Function
I displayed the chat of YouTube Live and tried playing