6
A
回答
10
您可以使用UIKIT框架中提供的核心圖形來滿足您的要求。
我在我的應用程序中有一個類似的需求與不同的用途,如果需要我可以爲您提供的代碼。
TNQ
.h文件中
#import <UIKit/UIKit.h>
typedef enum _DrawingMode{
DrawingModePen =0,
DrawingModeEraser=1,
} DrawingMode;
@interface DrawingView : UIView {
CGPoint lastPoint;
UIImageView *drawImage;
BOOL mouseSwiped;
int mouseMoved;
DrawingMode mode;
UIColor *_drawingPenColor;
}
@property (nonatomic, retain) UIColor *drawingPenColor;
@property (nonatomic) DrawingMode mode;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic,retain)UIImageView *drawImage;
@end
.M
#import "DrawingView.h"
@implementation DrawingView
@synthesize mode, drawingPenColor=_drawingPenColor, imageView=drawImage;
-(void)initialize{
drawImage = [[UIImageView alloc] initWithImage:nil];
self.autoresizesSubviews = YES;
drawImage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
drawImage.frame = self.bounds;
[self addSubview:drawImage];
self.backgroundColor = [UIColor clearColor];
mouseMoved = 0;
_drawingPenColor = [[UIColor alloc] initWithWhite:0.0 alpha:1.0];
}
-(void)maskImage:(UIImage *)image withMask:(UIImage *)maskImage
{
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
UIImage *tempImage = [[UIImage alloc] initWithCGImage:masked];
//self.clippedImage = tempImage;
[tempImage release];
CFRelease(masked);
CFRelease(mask);
}
-(void)awakeFromNib{
[self initialize];
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
[self initialize];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:self];
//lastPoint.y -= 20;
NSLog(@"touches begin");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
//currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.bounds.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
if (mode == DrawingModePen) {
NSLog(@"drawing");
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
}
else {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 20, 20));
CGContextStrokePath(UIGraphicsGetCurrentContext());
NSLog(@"clearing");
}
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.bounds.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
if (mode == DrawingModePen) {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
}
else {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [self.backgroundColor CGColor]);
}
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)dealloc {
[drawImage release];
[_drawingPenColor release];
[super dealloc];
}
@end
0
現在有一個叫做BrushEngine框架,你可以找到在http://www.cdframeworks.com/product/brushengine
BrushEngine讓您與借鑑你的手指使用線條,形狀(各種內置),自定義圖片和很多其他人。然後,您可以將輸出保存爲圖像或視頻。
相關問題
- 1. 觸摸IOS畫線
- 2. 可可觸摸:動畫上的觸摸
- 3. 在iPhone SDK上,有沒有一種方法可以禁用textview上的觸摸交互?在某種程度上允許觸摸背後的東西接收?
- 4. 如何在觸摸事件上畫線?
- 5. 在ios方面有沒有可用於PayPal Payments Pro SDK的sdk?
- 6. 可可觸摸 - 線程和動畫
- 7. iOS的觸摸事件(觸摸收盤沒有在合適的時間調用)
- 8. 切換視圖沒有動畫 - 可可觸摸
- 9. iOS - UIViews沒有響應觸摸事件
- 10. 有沒有什麼辦法可以在iOS上使用CGContext製作動畫?
- 11. 觸摸IOS中的視圖時可以觸摸點嗎?
- 12. Lua Gideros:觸摸畫線
- 13. spritekit使childnode沒有可觸摸
- 14. 在IOS中畫線的觸摸傳感器
- 15. 在QQuickView上沒有觸發的觸摸事件
- 16. iPhone SDK UIScrollView在移動後沒有得到觸摸事件
- 17. 爲什麼我在Android SDK Manager中沒有觸摸屏支持?
- 18. UITableViewCells沒有突出觸摸
- 19. RecyclerView沒有觸摸反饋
- 20. TableHeaderView沒有響應觸摸
- 21. UIScrollView沒有響應觸摸
- 22. ViewController沒有響應觸摸
- 23. UIBarButtonItem沒有響應觸摸
- 24. 清除的UIImageView什麼畫有觸摸
- 25. 可可觸摸 - 用定時器動畫
- 26. 在iPhone SDK中繪製觸摸點上的曲線
- 27. iPhone SDK使按鈕不可觸摸/可觸摸
- 28. 有沒有什麼辦法可以獲得觸摸位置的MKMapView的lat長度 - iPhone SDK
- 29. UITextView在動畫後沒有響應觸摸事件
- 30. UIViewController在CATransform3D動畫後沒有響應觸摸
可能的重複http://stackoverflow.com/questions/679983/how-to-draw-signature-with-lines-on-iphone-screen – visakh7