- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当用户仍在拖动或触摸时,如何用两根手指绘制/绘制一条直线,并且该直线可见?我已经尝试过使用 coregraphics 进行简单的绘画,但这个对我来说似乎有点复杂。
最佳答案
对于 Justin 的观点,只需处理 touchesBegan
和 touchesMoved
。
因此,如果您将 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/
这一章实现的连接线,目前仅支持直线连接,为了能够不影响原有的其它功能,尝试了2、3个实现思路,最终实测这个实现方式目前来说最为合适了。 请大家动动小手,给我一个免费的 Star 吧~ 大家
本章分享一下如何使用 Konva 绘制基础图形:矩形、直线、折线,希望大家继续关注和支持哈! 请大家动动小手,给我一个免费的 Star 吧~ 大家如果发现了 Bug,欢迎来提 Issue
本章响应小伙伴的反馈,除了算法自动画连接线(仍需优化完善),实现了可以手动绘制直线、折线连接线功能。 请大家动动小手,给我一个免费的 Star 吧~ 大家如果发现了 Bug,欢迎来提 Is
我有一条线(两点(x,y)(x1,y1))和一个带有焦点(rx,ry)的矩形。我需要帮助找出线和矩形之间的碰撞点,C++ 中的一个例子会有所帮助。 最佳答案 我不知道如何仅用“焦点”来表示矩形。您将需
让我们画一条线和一个圆: canvas.create_line(x0,y0,x1,y1) canvas.create_oval(x0,y0,x1,y1) 如何计算每个像素的像素数? 最佳答案 你不能。
本文实例讲述了php使用gd2绘制基本图形。分享给大家供大家参考,具体如下: 应用GD2函数可以绘制的图形有多种,最基本的图形包括条、圆、方形等。无论开发人员绘制多么复杂的图形,都是在这些最基本的
我已经尝试了所有改变颜色的方法: call s:h("Underlined", {"fg": s:norm, "gui": "underline", "cterm": "underline"})
为什么以下行有时会在 chrome 开发控制台中产生消息“未定义不是函数”: (callbackOrUndefined || function() {})(); 这个想法是,如果回调为真,即函数,则执
我想在 MS Chart 中实现直线(分段拟合)图表。 我知道 X 轴上的点 [最小值 = 1.4,最大值 = 2.2]。我已将所有这些点都放在折线图上。 现在的要求是我如何知道 X 轴上的点 [最小
所以我正在使用 charts.js http://www.chartjs.org/我试图使 2 个点之间的线是直线而不是弯曲的,没有明显的原因。 现在看起来是这样的 http://imgur.com/
我已将我的机器人发布到 Azure。现在我要开发自己的聊天用户界面。我想在 JavaScript 中使用 Direct Line API 来调用机器人。 如何在 JavaScript 中使用 Dire
这个有点难解释。我有一个整数列表。因此,例如,[1, 2, 4, 5, 8, 7, 6, 4, 1] - 当根据元素编号绘制时,它类似于凸图。我如何以某种方式从列表中提取此“形状”特征?它不必特别准确
我在学习 javaScript 和 jQuery 的第一周,我正在尝试制作一些动画。 基本上我想要做的就是让这些箭头从屏幕开始,从一侧进入,通过 throw 目标,然后离开屏幕的另一侧。 理想情况下,
假设我有一个向导并且有 3 个部分,假设它是这样的。 [A]----[B]----[C] 假设当前在范围内选择了 A。这是一个彩色的红色圆圈。当 B 在范围内被选中时,就可以移动一个对象(假设一条彩色
我正在使用 botframework-directlinejs NodeJS SDK 为我的机器人前端实现一个新 channel 。该 channel 将提供一些自定义反向 channel 功能;但是
我正在使用直线执行 hql 查询。该作业似乎没有出现在 HDP 2.6 上 Spark History 服务器的资源管理器中。如何让它运行在 Yarn 上? 谢谢 最佳答案 Beeline 是一个 A
我目前正在尝试找到一种如何使用 Google Maps Api V3 获得直线路线的方法。 我已经设法使用地理编码和方向服务来获取从 A 点到 B 点的路线,包括两条替代路线。我还尝试过“无高速公路”
我有一个简单的 Canvas html5。它可以通过选择选项绘制一些形状,如直线、圆形、矩形、多边形,但现在我想让所有绘制都可拖动(如果可能的话)可调整大小,没有 3 部分库,只有纯 JS。
我正在使用在 Kerberos 中添加的帐户启动 beeline 来测试 Sentry: beeline -u "jdbc:hive2://IP:10000/;principal=test_table
我正在使用普通的 html/javascript Canvas (无库)。我有一种从两点计算 Angular 方法,我的理论是,我获取 Angular 并通过循环所需的水平距离 (x) 并对该 Ang
我是一名优秀的程序员,十分优秀!