Swift opens a woman's signature (handwriting is signed by the user on the screen and a picture is generated)

Source: Internet
Author: User
This article describes how to use the Swift language to capture a user's signature on an iOS device (in fact, this is a simple drawing board program).

Implementation features are as follows:

1, the top of the page provides a signature area (UIView) that the user can sign in this area.

2, click "Preview Signature", will obtain the user signature generation UIImage, displays in the ImageView below.

3, click "Save Signature" to save the user's signature to the device album.

The effect chart is as follows:
The code is as follows:

---drawsignatureview.swift (signed view component)---

The code is as follows Copy Code
Import Uikit

public class Drawsignatureview:uiview {

Public properties
public var linewidth:cgfloat = 2.0 {
Didset {
Self.path.lineWidth = LineWidth
}
}
public var strokecolor:uicolor = Uicolor.blackcolor ()
public var signaturebackgroundcolor:uicolor = Uicolor.whitecolor ()

Private property
private var path = Uibezierpath ()
private var pts = [Cgpoint] (Count:5, Repeatedvalue:cgpoint ())
private var ctr = 0

Init
Override Init (Frame:cgrect) {
Super.init (Frame:frame)

Self.backgroundcolor = Self.signaturebackgroundcolor
Self.path.lineWidth = Self.linewidth
}

Init
Required public init? (Coder Adecoder:nscoder) {
Super.init (Coder:adecoder)

Self.backgroundcolor = Self.signaturebackgroundcolor
Self.path.lineWidth = Self.linewidth
}

Draw
Override public func DrawRect (rect:cgrect) {
Self.strokeColor.setStroke ()
Self.path.stroke ()
}

Touch Signature Related methods
Override public func Touchesbegan (touches:set<uitouch>, withevent event:uievent?) {
If let Firsttouch = touches.first{
Let touchpoint = Firsttouch.locationinview (self)
self.ctr = 0
Self.pts[0] = touchpoint
}
}

Override public func touchesmoved (touches:set<uitouch>, withevent event:uievent?) {
If let Firsttouch = touches.first{
Let touchpoint = Firsttouch.locationinview (self)
self.ctr++
SELF.PTS[SELF.CTR] = touchpoint
if (self.ctr = = 4) {
SELF.PTS[3] = Cgpointmake (self.pts[2].x + self.pts[4].x)/2.0,
(Self.pts[2].y + self.pts[4].y)/2.0)
Self.path.moveToPoint (Self.pts[0])
Self.path.addCurveToPoint (Self.pts[3], controlpoint1:self.pts[1],
CONTROLPOINT2:SELF.PTS[2])

Self.setneedsdisplay ()
Self.pts[0] = self.pts[3]
SELF.PTS[1] = self.pts[4]
SELF.CTR = 1
}

Self.setneedsdisplay ()
}
}

Override public func touchesended (touches:set<uitouch>, withevent event:uievent?) {
if self.ctr = = 0{
Let touchpoint = Self.pts[0]
Self.path.moveToPoint (Cgpointmake (TOUCHPOINT.X-1.0,TOUCHPOINT.Y))
Self.path.addLineToPoint (Cgpointmake (TOUCHPOINT.X+1.0,TOUCHPOINT.Y))
Self.setneedsdisplay ()
} else {
self.ctr = 0
}
}

Signature View Empty
public func clearsignature () {
Self.path.removeAllPoints ()
Self.setneedsdisplay ()
}

Save Signature as UIImage
public func getsignature ()->uiimage {
Uigraphicsbeginimagecontext (Cgsizemake (Self.bounds.size.width,
Self.bounds.size.height))
Self.layer.renderInContext (Uigraphicsgetcurrentcontext ()!)
Let Signature:uiimage = Uigraphicsgetimagefromcurrentimagecontext ()
Uigraphicsendimagecontext ()
return signature
}
}

---viewcontroller.swift (using sample)---

The code is as follows Copy Code

Import Uikit

Class Viewcontroller:uiviewcontroller {

Signature Preview
@IBOutlet weak var imageview:uiimageview!

Signature Area View
var drawview:drawsignatureview!

Override Func Viewdidload () {
Super.viewdidload ()

Signature area location Size
var drawviewframe = Self.view.bounds
DrawViewFrame.size.height = 200
Add a signature area
Drawview = Drawsignatureview (frame:drawviewframe)
Self.view.addSubview (Drawview)

}

Preview signature
@IBAction func previewsignature (sender:anyobject) {
Let Signatureimage = Self.drawView.getSignature ()
Imageview.image = Signatureimage
}

Save Signature
@IBAction func savasignature (sender:anyobject) {
Let Signatureimage = Self.drawView.getSignature ()
Uiimagewritetosavedphotosalbum (signatureimage, nil, nil, nil)
Self.drawView.clearSignature ()
}

Clear Signature
@IBAction func clearsignature (sender:anyobject) {
Self.drawView.clearSignature ()
Self.imageView.image = Nil
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.