本文介绍了如何使用UIBezierPath绘制跟随触摸事件的线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从一个类似的问题中选择了下面的代码,但我没有运气让它工作。
I picked up the following piece of code from a similar question but I'm not having any luck getting it to work.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Check #1");
UITouch* touch = [touches anyObject];
self.currentPath = [UIBezierPath bezierPath];
currentPath.lineWidth = 3.0;
[currentPath moveToPoint:[touch locationInView:self.view]];
[paths addObject:self.currentPath];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self.currentPath addLineToPoint:[touch locationInView:self.view]];
[self.view setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] set];
for (UIBezierPath *path in paths) {
NSLog(@"Check #2?");
[path stroke];
}
}
我得到的是我需要使用 UIBezierPath
,但我不知道如何让它跟随我的手指。我只想画一条线,当用户触摸下来并在它们接触手指的地方结束。
What I've got from it is that I need to use UIBezierPath
but I'm not sure how to get it to follow my finger. I only want to draw a line, start it when the user touches down and end it where they pick up their finger.
如何使用UIBezierPath实现这一线条绘制?
How can I achieve this line drawing using UIBezierPath?
推荐答案
#import <UIKit/UIKit.h>
@interface Canvas : UIImageView
@property (nonatomic, assign) CGPoint location;
@end
#import "Canvas.h"
@implementation Canvas
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.location = [touch locationInView:self];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, self.location.x, self.location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.location = currentLocation;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, self.location.x, self.location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.location = currentLocation;
}
@end
这篇关于如何使用UIBezierPath绘制跟随触摸事件的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!