手指追踪与屏幕IOS上的触摸不匹配

时间:2014-03-05 04:40:14

标签: ios touchesmoved

我是堆栈溢出的新成员,但我一直在使用这些论坛中找到的答案多年。我有一个问题可以通过对我的主题进行更彻底的研究来更好地回答,但是我正在为我在学校的一个班级进行合作,并且已经被委托对其他人的代码进行一些更改......并且坦率地说我准时不足,所以我希望能在这里得到一些帮助。

我已经研究过这里发现的其他一些问题和答案,但它们似乎都没有直接解决我的问题。

我有一个应该捕获人签名的应用程序。它目前适用于iphone 4,当我尝试更换硬件时会出现问题。如果我选择一个3.5英寸的屏幕,或任何版本的ipad,我用我的签名制作的触摸在屏幕顶部是准确的,但随着我向下移动逐渐变得越来越不准确(就好像图像被描绘一样)是一个4英寸的屏幕,而触摸是在3.5英寸的屏幕上发生的)

这是我的ViewController代码:

@implementation SignatureViewController

@synthesize clearButton;
@synthesize tempDrawImage;
@synthesize tempSaveImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    red = 0.0/255.0;
    green = 0.0/255.0;
    blue = 0.0/255.0;
    brush = 5.0;
    opacity = 1.0;
    [self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];

    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempDrawImage setAlpha:opacity];
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    HousingAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    appDelegate.tempSig = self.tempDrawImage.image;
}

- (IBAction)clearSig:(id)sender {
    [self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
    return;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    [self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
}

@end

我认为touchesMoved方法中的“self.view.frame.size.width”和“self.view.frame.size.height”会提取有关硬件屏幕大小的信息并动态填充但显然事实并非如此。

非常感谢任何帮助。

此外,目前正在将签名保存在bg-signature-portriat.png背景图像(一个带有x的简单线条)的顶部。我想单独获得签名,但无法理解如何做到这一点(也许这需要分层UIImage?还有一些不能轻易解释而无法访问故事板?)。

非常感谢你,我保证会有更好的问题(希望有几个答案)继续前进。

1 个答案:

答案 0 :(得分:1)

试试这可能对您有所帮助,并且很容易更改代码

- (void)showPopOverscreenforSignatureScreen //:(UIButton *)sender call this method in button action
{
    mouseMoved = 0;
    [SignatureContentView setHidden:NO];
    SignatureContentView.frame=CGRectMake(kLabelWidth+20, 150, 500, 200);
    signaturedImageView =(UIImageView *)[self.view viewWithTag:imageViewSelectedTagvalue];
    [signaturedImageView setHidden:YES];  
}
- (void)allocPopOverscreenforSignatureScreen
{
    SignatureContentView =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 500, 200)];
    SignatureContentView.layer.cornerRadius=5.0f;
    SignatureContentView.layer.backgroundColor=[UIColor lightGrayColor].CGColor;
    SignatureContentView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:SignatureContentView];

    signatureImageView = [[UIImageView alloc] initWithImage:nil];
    signatureImageView.layer.cornerRadius=5.0f;
    signatureImageView.layer.backgroundColor=[UIColor lightGrayColor].CGColor;
    signatureImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper_.png"]];
    signatureImageView.frame = CGRectMake(0, 0, 500, 148);
    signatureImageView.userInteractionEnabled=YES;
    [SignatureContentView addSubview:signatureImageView];
    mouseMoved = 0;

    UIButton *cancelButton =[UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame=CGRectMake(50, 150, 100, 40);
    [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    cancelButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
    [cancelButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
    [SignatureContentView addSubview:cancelButton];

    UIButton *clearButton =[UIButton buttonWithType:UIButtonTypeCustom];
    clearButton.frame=CGRectMake(200, 150, 100, 40);
    [clearButton setTitle:@"Clear" forState:UIControlStateNormal];
    clearButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
    [clearButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [clearButton addTarget:self action:@selector(clearsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
    [SignatureContentView addSubview:clearButton];

    UIButton *useButton =[UIButton buttonWithType:UIButtonTypeCustom];
    useButton.frame=CGRectMake(350, 150, 100, 40);
    useButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
    [useButton setTitle:@"Use" forState:UIControlStateNormal];
    [useButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [useButton addTarget:self action:@selector(getSignaturefromsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
    [SignatureContentView addSubview:useButton];
    [SignatureContentView setHidden:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    mouseSwiped = NO;
    //The below statements will help to get the exact point user touches with out this it will take ZERO yaxis  
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:signatureImageView];
    lastPoint.y -= 20;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:signatureImageView];
    currentPoint.y -= 20;
    UIGraphicsBeginImageContext(signatureImageView.frame.size);
    [signatureImageView.image drawInRect:CGRectMake(0, 0, signatureImageView.frame.size.width, signatureImageView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.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());
    signatureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;
    mouseMoved++;
    if (mouseMoved == 20) {
        mouseMoved = 0;
    }
}

- (void)clearsignatureImageView
{
    signatureImageView.image = nil;
}

- (void)cancelsignatureImageView
{
    [SignatureContentView setHidden:YES];
    [signaturedImageView setHidden:NO];
}
- (void)getSignaturefromsignatureImageView
{
    signaturedImageView.image=nil;
    if (signatureImageView.image !=nil) {
        AppDelegate *delegate =[AppDelegate sharedInstanceofAppdelegate];
        UIGraphicsBeginImageContext(signatureImageView.frame.size);
        [signatureImageView.image drawInRect:CGRectMake(0, 0, signatureImageView.frame.size.width, signatureImageView.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.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());
        signatureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        // UIImageWriteToSavedPhotosAlbum(signatureImageView.image, nil, nil, nil);// black
        UIGraphicsEndImageContext();
        [SignatureContentView setHidden:YES];
        [signaturedImageView setHidden:NO];
        NSArray *docArr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath =[NSString stringWithFormat:@"%@/%@",[docArr objectAtIndex:0],[NSString stringWithFormat:@"signature%@.png",[self PathNameforDocuments]]];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        }
        filePath =[[docArr objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"signature%@.png",[self PathNameforDocuments]]];
        NSData* pictureData = UIImagePNGRepresentation(signatureImageView.image);
        BOOL success=  [pictureData writeToFile:filePath atomically:YES ];
        if (success) {
            delegate.userSignatureImagePathString=filePath;
        }
        delegate.userSignatureImage=signatureImageView.image;
        signaturedImageView =(UIImageView *)[self.view viewWithTag:imageViewSelectedTagvalue];
        signaturedImageView.image=delegate.userSignatureImage;
    }
    else
    {
    // please sign
    }
}
-(void)viewDidLoad
{
    [self allocPopOverscreenforSignatureScreen];
}

使用UIGestureRecognizerDelegate委托并全局声明这些对象:

UIImageView *signatureImageView, *signaturedImageView;
BOOL mouseSwiped;
int mouseMoved;
UIView * SignatureContentView;