ここでお聞きしたところで回答を試みました。
私はビュー内にスクロールを設定しました。その後、ビューに書き込もうとしましたが、できませんでした。今それは動作します。
SignatureDrawView.h
#import <UIKit/UIKit.h>
@interface SignatureDrawView : UIView
@property (nonatomic, retain) UIGestureRecognizer *theSwipeGesture;
@property (nonatomic, retain) UIImageView *drawImage;
@property (nonatomic, assign) CGPoint lastPoint;
@property (nonatomic, assign) BOOL mouseSwiped;
@property (nonatomic, assign) NSInteger mouseMoved;
- (void)erase;
- (void)setSignature:(NSData *)theLastData;
- (BOOL)isSignatureWrite;
@end
SignatureDrawView.m
#import "SignatureDrawView.h"
@implementation SignatureDrawView
@synthesize theSwipeGesture;
@synthesize drawImage;
@synthesize lastPoint;
@synthesize mouseSwiped;
@synthesize mouseMoved;
#pragma mark - View lifecycle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:drawImage];
self.backgroundColor = [UIColor whiteColor];
mouseMoved = 0;
}
return self;
}
#pragma mark touch handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
NSArray *array = touch.gestureRecognizers;
for (UIGestureRecognizer *gesture in array)
{
if (gesture.enabled & [gesture isMemberOfClass:[UISwipeGestureRecognizer class]])
{
gesture.enabled = NO;
self.theSwipeGesture = gesture;
}
}
}
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
[[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!mouseSwiped)
{
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
self.theSwipeGesture.enabled = YES;
mouseSwiped = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"stopscroll" object:self];
}
#pragma mark Methods
- (void)erase
{
mouseSwiped = NO;
drawImage.image = nil;
}
- (void)setSignature:(NSData *)theLastData
{
UIImage *image = [UIImage imageWithData:theLastData];
if (image != nil)
{
drawImage.image = [UIImage imageWithData:theLastData];
mouseSwiped = YES;
}
}
- (BOOL)isSignatureWrite
{
return mouseSwiped;
}
@end
私が上記に追加して、私はview.Itがある上、私は署名に触れたときにスクロールを停止するためのポストの通知を作成しただけですstart、moving、endメソッドで実装されています。
[[NSNotificationCenter defaultCenter] postNotificationName:@ "stopscroll" 対象:自己];
SignatureDrawView.m
のViewControllerの次の私はUIViewのでscrollViewとUIImageViewを作成しました。上記viewDidLoadメソッドで
ViewController.h
#import <UIKit/UIKit.h>
#import "SignatureDrawView.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIScrollView *scroll;
@property (strong, nonatomic) IBOutlet SignatureDrawView *drawSignView;
@property (strong, nonatomic) IBOutlet UITextField *txtFldDesc;
- (IBAction)actionSave:(id)sender;
- (IBAction)actionClear:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize drawSignView,scroll,txtFldDesc;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+200);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScroll:) name:@"stopscroll" object:nil];
}
- (void)stopScroll:(NSNotification *)notification
{
if([[notification name] isEqualToString:@"stopscroll"])
scroll.scrollEnabled = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionSave:(id)sender
{
scroll.scrollEnabled = YES;
// code for save the signature
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
....Then do your stuff to save this in DB or server
}
- (IBAction)actionClear:(id)sender
{
scroll.scrollEnabled = YES;
//code for clear the signature
[self.drawSignView erase];
}
@end
IストップスクロールにaddObserverを追加しました。
[NSNotificationCenter defaultCenter] addObserver:自己 セレクタ:@selector(stopScroll :)名: "stopscroll" オブジェクト@:なし]。
は最後に、私はstopScroll:
方法
を実施しても、私はactionSave
でscroll.scrollEnabled = YES
とactionclear
方法
を設定し、私は私が試したし、うまくいった後にのみ、あなたのソリューションを与えました。
私のコードを確認し、適用してください。これは完璧に動作します。
出力結果
あなたは自動レイアウト使用していますか? – Lion
@Lionはい私はAutolayoutを使用しています。 – Muju
スクロールビューと署名ビューの制約は何ですか? – Lion