0
我試圖實現一個徒手繪製工具,通過該工具,用戶可以使用觸摸開始和觸摸移動方法,在PDF上動態繪製多個形狀,如矩形,日食,圓等。 那麼任何人誰做了這種工具或有知識如何做到這一點,請幫助我。如何在iOS中實現繪圖工具?
預先感謝您。
我試圖實現一個徒手繪製工具,通過該工具,用戶可以使用觸摸開始和觸摸移動方法,在PDF上動態繪製多個形狀,如矩形,日食,圓等。 那麼任何人誰做了這種工具或有知識如何做到這一點,請幫助我。如何在iOS中實現繪圖工具?
預先感謝您。
這是一個非常寬泛的問題,需要在StackOverflow上解答。我仍然試圖在這裏給你一個開始。
1)在您的UIViewController
中聲明兩個全局變量BOOL mouseSwiped
,CGPoint lastPoint
。
2)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
3)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(yourSubview.frame.size);
[yourSubview drawInRect:CGRectMake(0, 0, yourSubview.frame.size.width, yourSubview.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
yourSubview = UIGraphicsGetImageFromCurrentImageContext();
[yourSubview setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
4)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIGraphicsBeginImageContext(yourSubview.frame.size);
[yourSubview drawInRect:CGRectMake(0, 0, yourSubview.frame.size.width, yourSubview) blendMode:kCGBlendModeNormal alpha:1.0];
yourSubview = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
希望這有助於
這個問題是遠遠堆棧溢出過於寬泛。但是,您可能需要首先實施[UIGestureRecognizer](https://developer.apple.com/reference/uikit/uigesturerecognizer)以捕獲用戶觸摸屏幕的位置。 – Tibrogargan
1.使用UIGestureRecognier檢測用戶交互 2.創建UIBezierPath保存點,然後通過覆蓋drawInRect方法在視圖上繪製 – lee5783
非常感謝您的建議。堆棧溢出是給予實際幫助的開發人員的唯一平臺。這就是我問這個問題的原因。其實我想實現一個類似於'FieldWire'app的工具。因此,如果你瞭解它,你可以給我建議一些教程鏈接,Sample Projects。 @Tibrogargan :) –