Plum-O-Meter, 3D觸摸應用,稱重小型水果

分享于 

4分钟阅读

GitHub

  简体
3D Touch Application for Weighing Plums (and other small fruit!)
  • 源代码名称:Plum-O-Meter
  • 源代码网址:http://www.github.com/FlexMonkey/Plum-O-Meter
  • Plum-O-Meter源代码文档
  • Plum-O-Meter源代码下载
  • Git URL:
    git://www.github.com/FlexMonkey/Plum-O-Meter.git
    Git Clone代码到本地:
    git clone http://www.github.com/FlexMonkey/Plum-O-Meter
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/FlexMonkey/Plum-O-Meter
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    Plum-O-Meter

    ###3D触摸应用,用于称重梅子和其他小型水果

    这个博客文章的伴侣项目:http://flexmonkey.blogspot.co.uk/2015/10/the-plum-o-meter-weighing-plums-using.html

    Plum-O-Meter非常简单:当它的视图控制器的touchesBegan被调用时,它为每个触摸添加一个新的CircleWithLabelCircleWithLabel是一个CAShapeLayer,它绘制一个圆形,并具有额外的CATextLayer

    var circles = [UITouch: CircleWithLabel]()
     overridefunctouchesBegan(touches: Set<UITouch>, withEventevent: UIEvent?)
     {
     label.hidden=truefor touch in touches
     {
     let circle =CircleWithLabel()
     circle.drawAtPoint(touch.locationInView(view),
     force: touch.force/ touch.maximumPossibleForce)
     circles[touch] = circle
     view.layer.addSublayer(circle)
     }
     highlightHeaviest()
     }

    当触摸移动时,该字典用于更新触摸的相关CircleWithLabel并更新它半径和标签:

    overridefunctouchesMoved(touches: Set<UITouch>, withEventevent: UIEvent?)
     {
     for touch in touches where circles[touch] !=nil {
     let circle = circles[touch]! circle.drawAtPoint(touch.locationInView(view),
     force: touch.force/ touch.maximumPossibleForce)
     }
     highlightHeaviest()
     }

    这两个方法都调用highlightHeaviest(),此方法循环遍历circles字典中的每个touch +layer项,并根据按touch force排序的字典版本在每个上设置isMax属性:

    funchighlightHeaviest()
     {
     funcgetMaxTouch() -> UITouch? {
     return circles.sort({
     (a: (UITouch, CircleWithLabel), b: (UITouch, CircleWithLabel)) ->Boolinreturn a.0.force> b.0.force }).first?.0 }
     circles.forEach {
     $0.1.isMax=$0.0 ==getMaxTouch()
     }
     }

    如果为true,isMax将图层的填充颜色设置为黄色。

    当李子从屏幕上移走时,它的CircleWithLabel层被删除,因为需要重新计算,所以还调用了highlightHeaviest:

    
     override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
    
    
     {
    
    
     for touch in touches where circles[touch] != nil
    
    
     {
    
    
     let circle = circles[touch]!
    
    
    
     circles.removeValueForKey(touch)
    
    
     circle.removeFromSuperlayer()
    
    
     }
    
    
    
     highlightHeaviest()
    
    
     }
    
    
    
    

    触摸  
    相关文章