- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 UIImageView
上绘制一个“消失的笔画”,它会在触摸事件之后在固定时间延迟后自动删除。这是我的 ViewController 中的内容。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
CGPoint lp = lastPoint;
UIColor *color = [UIColor blackColor];
[self drawLine:5 from:lastPoint to:currentPoint color:color blend:kCGBlendModeNormal];
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self drawLine:brush from:lp to:currentPoint color:[UIColor clearColor] blend:kCGBlendModeClear];
});
lastPoint = currentPoint;
}
- (void)drawLine:(CGFloat)width from:(CGPoint)from to:(CGPoint)to color:(UIColor*)color blend:(CGBlendMode)mode {
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(context, from.x, from.y);
CGContextAddLineToPoint(context, to.x, to.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, width);
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetBlendMode(context, mode);
CGContextStrokePath(context);
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:1];
UIGraphicsEndImageContext();
}
绘制阶段运行良好,但后续删除阶段存在一些问题。
UIGraphicsBeginImageContext
在 dispatch_after
中运行的成本所致。有没有更好的方法来绘制自擦线?
奖励:我真正喜欢的是“缩小和消失”的路径。换句话说,在延迟之后,我不想只是清除描边路径,而是让它从 5pt 缩小到 0pt,同时淡出不透明度。
最佳答案
我会让 View 以 60 Hz 的频率连续绘制,并且每次使用存储在数组中的点绘制整条线。这样,如果您从数组中删除最旧的点,它们将不再被绘制。
要连接 View 以显示刷新率(60 Hz),请尝试以下操作:
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
将年龄属性与每个点一起存储,然后循环遍历数组并删除早于阈值的点。
例如
@interface AgingPoint <NSObject>
@property CGPoint point;
@property NSTimeInterval birthdate;
@end
// ..... later, in the draw call
NSTimeInterval now = CACurrentMediaTime();
AgingPoint *p = [AgingPoint new];
p.point = touchlocation; // get yr touch
p.birthdate = now;
// remove old points
while(myPoints.count && now - [myPoints[0] birthdate] > 1)
{
[myPoints removeObjectAtIndex: 0];
}
myPoints.add(p);
if(myPoints.count < 2)
return;
UIBezierPath *path = [UIBezierPath path];
[path moveToPoint: [myPoints[0] point]];
for (int i = 1; i < myPoints.count; i++)
{
[path lineToPoint: [myPoints[i] point];
}
[path stroke];
因此,在每次绘制调用时,创建一个新的贝塞尔路径,移动到第一个点,然后向所有其他点添加线条。最后,划线。
要实现“收缩”线,您可以在数组中的连续点对之间绘制短线,并使用age属性来计算笔画宽度。这并不完美,因为各个线段在起点和终点处具有相同的宽度,但它是一个起点。
重要提示:如果您要获得很多积分,性能将成为一个问题。这种使用 Quartz 的路径渲染并没有完全调整为真正快速渲染。事实上,它非常非常慢。
Cocoa 数组和对象也不是很快。
如果您遇到性能问题并且想要继续此项目,请研究 OpenGL 渲染。通过将纯 C 结构插入 GPU,您将能够更快地运行该程序。
关于ios - 使用 CGContextRef 绘制自删除路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42125817/
我假设原点在左下角,y轴从0开始向上扩展。 x 轴从 0 开始向右扩展。 我只是问,因为我从绘图代码中得到了非常奇怪的结果......想确保我没有弄错这部分。 最佳答案 这取决于此上下文的使用位置。
如何将使用 CGBitmapContextCreate() 创建的 CGContextRef 绘制到 NSView? 我应该先将其转换为图像吗?如果是这样的话,这不是一个昂贵的操作吗? 最佳答案 Sh
我尝试在 ipad 上创建一个简单的手指绘画应用程序。我能够正确地绘制到屏幕的路径,但我希望有一个选项可以完全清除所有绘制路径的屏幕。我目前拥有的代码清除了上下文,但是当我调用绘图代码块时,所有路径都
我正在制作一个类似于绘图应用程序的应用程序,并希望在用户触摸的地方绘制图像。我可以在 O.K. 的位置绘制图像。使用此代码: CGContextRef ctx = UIGraphicsGetCurre
我想画一个圆弧并填充它。第一张图是这样的。我想得到第二张图的效果 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFra
所以,我有这段代码,它画了一个圆圈,周围有一条线。 - (void)drawRect:(CGRect)rect { CGContextRef contextRef = UIGraphicsGe
我有一个 CGContextRef,里面画了一些东西,我想把它清除掉,让它是空的(全透明的)。怎么做? 最佳答案 CGContextClearRect应该完成这项工作。 关于ios - 如何在绘图之前
当我调用函数 CGContextStrokePath(下面代码的最后一行)时,我的程序崩溃了。上下文是否会以某种方式损坏? (它只有在 expression(它是一个 NSArray)中有特定值时才会
由于对我之前的结果不满意,我被要求创建一个徒手绘图 View ,该 View 在缩放时不会模糊。我能想到这是可能的唯一方法是使用 CATiledLayer,否则在缩放时画线会太慢。目前,我已将其设置为
如何将 CGContextAddArc 中的所有像素点存储到 NSMutableArray. 或 CGContextRef 到 NSMutable Array static inline float
在我的应用程序中,我需要调整和裁剪一些本地和在线存储的图像。我正在使用Trevor Harmon's tutorial它实现了UIImage+Resize。 在我的 iPhone 4(iOS 4.3.
我刚刚开始学习 OSX 编程,我偶然发现了一个问题,我不知道是否应该使用 CGContextRef 还是 NSGraphicsContext。 有什么区别? 我几乎不知道 CG 或 NS 之间的区别[
在重写 UIView drawRect 时,我使用 CGContextDrawImage 绘制主图像。在它上面我需要绘制另一个图像(使用多重混合模式),所以我实际上需要在它上面绘制。 第二个图像需要准
我想在 UIImageView 上绘制一个“消失的笔画”,它会在触摸事件之后在固定时间延迟后自动删除。这是我的 ViewController 中的内容。 - (void)touchesMoved:(N
这个问题已经存在: 关闭 10 年前。
我有很多时间都在忍受一个问题,就是简单地绘制两个单独的形状(例如矩形) 具体来说,我有一个数据源,它是一个数组,这个数组包含包含 CGPoint 的子数组。我想做的只是根据每个子数组绘制路径,并且它们
我有我的 UIView 的框架,在 drawRect 中它使用它作为上下文,但我希望它在这个上下文之外绘制。我如何指定我自己的上下文,让它这样做? 最佳答案 我不明白您如何将框架用作上下文。框架是 C
如何画一条有形的线?例如带有 CGContextRef 的心形、菱形或星形线? 心线示例: 菱形线示例: 三角线示例: 星线示例: 这是我目前拥有的: - (void)draw { CGCon
这个问题在这里已经有了答案: Assigning an existing CGColor to a CGColor property works in iOS Simulator, not iOS
目前,我在绘制小点时遇到图形问题。 我注意到在大多数专业日历应用程序中,事件日历标识符是一个小点,其颜色是事件日历颜色。 我目前正处于申请阶段,我需要画一个更好的点。这是我的意思的照片。 它在这里可能
我是一名优秀的程序员,十分优秀!