廚老伯開槍中

建立簽名

來源

作者實作一般常見的建立簽名方法後,發現在retina上面會有一些問題(鋸齒、反應時間等)
所以提供了以下作法
詳細作法請見原文

以下僅摘錄重點方法

宣告物件

@interface mySmoothLineView: UIView {
    UIBezierPath *_path;
    UIImage *incrementalImage;
    CGPoint pts[5];
    uint ctr;
}

記錄使用者touch, 當記錄了四個點後,就畫成路徑並與之前的路徑連接

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [touches anyObject];
     CGPoint p = [touch locationInView: self];
     ctr++;
     pts[ctr] = p;
     if (ctr == 4) {
          pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0);
          [self.path1 moveToPoint: pts[0]];
          [self.path1 addCurveToPoint: pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];
          [self setNeedsDisplay];
          pts[0] = pts[3];
          pts[1] = pts[4];
          ctr = 1;
     }
}

將簽名做成圖片輸出

-(IBAction)btnCaptureSignature_Clicked:(id)sender{
    UIGraphicsBeginImageContext(CGSizeMake(signView.bounds.size.width, signView.bounds.size.height)); 
     [signView.layer renderInContext: UIGraphicsGetCurrentContext()];
     UIImage *imgMySignature = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
}
comments powered by Disqus