Untitled_130.py
import ui,ctypes,string | |
from objc_util import * | |
UIFont=ObjCClass('UIFont') | |
import sys | |
c.CTFontCreatePathForGlyph.restype = c_void_p | |
c.CTFontCreatePathForGlyph.argtypes = [c_void_p, c_void_p, c_void_p] | |
createPath=c.CTFontCreatePathForGlyph | |
glyphs_list=[] | |
for w in [-1, -0.5, 0., 0.5, 1]: | |
f=UIFont.systemFontOfSize_weight_(512,w) | |
glph=f.glyphWithName_('A') | |
glyphs_list.append(UIBezierPath.bezierPathWithCGPath_(ObjCInstance(createPath(f,glph,None)))) | |
#choose any two glyphs, but must have same number of points, in same order | |
# this works very well with original glyphpath, not so much with ondevice generated ones | |
glyphs_list=[glyphs_list[2],glyphs_list[3]] | |
def _get_CGColor(color): | |
"""Get a CGColor from a wide range of formats.""" | |
return UIColor.colorWithRed_green_blue_alpha_( | |
*ui.parse_color(color) | |
).CGColor() | |
class AniPathView(ui.View): | |
'''animates a path based on touch''' | |
def __init__(self, path, color="#21abed", | |
line_width=1, line_color="#0f210f", | |
*args, **kwargs): | |
super().__init__(self, *args, **kwargs) | |
self.bg_color='white' | |
# Draw the full path on one layer | |
self._layer = ObjCClass("CAShapeLayer").new() | |
self._layer.setStrokeColor_(_get_CGColor(line_color)) | |
self._layer.setFillColor_(_get_CGColor(color)) | |
self._layer.setLineWidth_(line_width) | |
self._layer.path=path | |
self.setupani() | |
self._path=path | |
self.lasti=0 | |
self.setup_layer() | |
def setup_layer(self): | |
ObjCInstance(self).layer().addSublayer_(self._layer) | |
b=ObjCInstance(self).layer().bounds() | |
b.size.height-=44 | |
b.origin.y=44 | |
self._layer.frame=b | |
def curpath(self): | |
try: | |
self._layer.presentationLayer().path() | |
except AttributeError: | |
return self._path | |
def touch_began(self,touch): | |
i=int(touch.location.x/self.width*len(glyphs_list)) | |
p=glyphs_list[i%len(glyphs_list)] | |
#print(i) | |
self._layer.timeOffset=touch.location.x/self.width | |
#self.setPath(p.CGPath()) | |
def touch_moved(self,touch): | |
i=int(touch.location.x/self.width*len(glyphs_list)) | |
'''if not self.lasti == i: | |
self.lasti=i | |
p=glyphs_list[i%len(glyphs_list)] | |
self.setPath(p.CGPath(),0.2)''' | |
self._layer.timeOffset=(touch.location.x/self.width) | |
@on_main_thread | |
def setupani(self): | |
animation=ObjCClass('CABasicAnimation').animationWithKeyPath('path') | |
animation.fromValue = glyphs_list[0].CGPath() | |
animation.toValue = glyphs_list[1].CGPath() | |
animation.removedOnCompletion=False | |
animation.duration=1. | |
self._layer.speed=0. | |
self._layer.timeOffset=0. | |
self._layer.addAnimation(animation, forKey='a') | |
self.a=animation | |
#return self.curpath() | |
def setPath(self,path,duration=0.3): | |
animation=ObjCClass('CABasicAnimation').animationWithKeyPath('path') | |
animation.fromValue = self.curpath() | |
animation.duration=duration | |
self._path = path | |
self._layer.addAnimation(animation, forKey=None) | |
self._layer.path=self._path | |
#print(self._path) | |
pv=AniPathView(glyphs_list[0].CGPath()) | |
pv.present() | |
#pv.setupani(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment