我是堆棧溢出的新成員,但我一直在使用這些論壇中找到的答案多年。我有一個問題可以在我更加深入地研究這個話題的時候得到更好的回答,但是我正在爲我在學校的一個班級進行合作,並且已經委託對別人的代碼進行一些修改......並坦率地說準時不足,所以我希望能在這裏得到一些幫助。手指跟蹤不符合屏幕觸摸IOS
我研究了一些其他問題和答案,但沒有一個人似乎直接解決了我的問題。
我有一個應用程序應該捕獲一個人的簽名。它目前正在爲iphone 4工作,當我嘗試更換硬件時,問題就來了。如果我選擇3.5英寸的屏幕或任何版本的ipad,我用我的簽名製作的觸摸在屏幕的頂部是準確的,但隨着我向下移動,逐漸變得越來越不準確(就好像圖像被描繪出來一樣是4英寸的屏幕,而觸摸正在發生一個3.5英寸的屏幕上)
這裏是我的視圖控制器代碼:
@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
我將認爲「self.view.frame.size .width「和」self.view.frame.size.height「在touchesMoved方法將拉動關於硬件的屏幕大小的信息並動態地填充它,但顯然情況並非如此。
任何幫助將不勝感激。
另外,當前簽名被保存在bg-signature-portriat.png背景圖像(帶有x的簡單線條)上。我想單獨獲取簽名,但無法理解如何做到這一點(也許這需要對UIImage進行分層?並且在沒有訪問故事板的情況下無法輕易解釋這些內容?)。
非常感謝你提前,我保證有更好的問題(希望有一些答案)前進。
我使用簽名功能的地方在帶有圖像視圖(用於簽名)的視圖和用於使用,清除,取消的按鈕上創建。如果你對這種代碼格式沒問題,我會寄給你 –
這很好,過去使用模板代碼時遇到的問題是很難將它解決到現有的故事板(這顯然只是一種理解我的差距)。但是,任何你可能會有幫助。謝謝 – krayzk
請檢查我的答案更好試試這個作爲示例代碼 –