gpt4 book ai didi

ios - 用两根手指画直线

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:24 25 4
gpt4 key购买 nike

当用户仍在拖动或触摸时,如何用两根手指绘制/绘制一条直线,并且该直线可见?我已经尝试过使用 coregraphics 进行简单的绘画,但这个对我来说似乎有点复杂。

最佳答案

对于 Justin 的观点,只需处理 touchesBegantouchesMoved

因此,如果您将 UIView 子类化,CoreGraphics 实现可能如下所示:

@interface CustomView ()

@property (nonatomic, strong) NSMutableArray *paths;
@property (nonatomic, strong) UIBezierPath *currentPath;

@end

@implementation CustomView

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setMultipleTouchEnabled:YES];
}
return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.currentPath)
{
if (!self.paths)
self.paths = [NSMutableArray array];

self.currentPath = [UIBezierPath bezierPath];
self.currentPath.lineWidth = 3.0;

[self.paths addObject:self.currentPath];

[self touchesMoved:touches withEvent:event];
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] != 2)
return;

[self.currentPath removeAllPoints];

__block NSInteger i = 0;
[touches enumerateObjectsUsingBlock:^(UITouch *touch, BOOL *stop) {
CGPoint location = [touch locationInView:self];

if (i++ == 0)
[self.currentPath moveToPoint:location];
else
[self.currentPath addLineToPoint:location];
}];

[self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.currentPath = nil;
}

- (void)drawRect:(CGRect)rect
{
[[UIColor redColor] setStroke];
[[UIColor clearColor] setFill];

for (UIBezierPath *path in self.paths)
{
[path stroke];
}
}

- (void)reset
{
self.paths = nil;
[self setNeedsDisplay];
}

@end

或者,您也可以使用 Quartz 2D 并定义您自己的 CAShapeLayer 对象,然后您的 View 子类或您的 View Controller 可以做类似的事情(不用说,这是 View Controller 实现; View 实现应该是显而易见的):

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (nonatomic, weak) CAShapeLayer *currentLayer;
@property (nonatomic, strong) UIBezierPath *currentPath;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[self.view setMultipleTouchEnabled:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.currentLayer)
{
CAShapeLayer *layer = [CAShapeLayer layer];
layer.lineWidth = 3.0;
layer.strokeColor = [[UIColor redColor] CGColor];
layer.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer:layer];
self.currentLayer = layer;

self.currentPath = [UIBezierPath bezierPath];

[self touchesMoved:touches withEvent:event];
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] != 2)
return;

[self.currentPath removeAllPoints];

__block NSInteger i = 0;
[touches enumerateObjectsUsingBlock:^(UITouch *touch, BOOL *stop) {
CGPoint location = [touch locationInView:self.view];

if (i++ == 0)
[self.currentPath moveToPoint:location];
else
[self.currentPath addLineToPoint:location];
}];

self.currentLayer.path = [self.currentPath CGPath];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.currentPath = nil;
self.currentLayer = nil;
}

- (IBAction)didTouchUpInsideClearButton:(id)sender
{
for (NSInteger i = [self.view.layer.sublayers count] - 1; i >= 0; i--)
{
if ([self.view.layer.sublayers[i] isKindOfClass:[CAShapeLayer class]])
[self.view.layer.sublayers[i] removeFromSuperlayer];
}
}

@end

对于后一种方法,您需要 add the QuartzCore.framework to your project .

关于ios - 用两根手指画直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com