ارسم الخطوط المنقطة باستخدام الكوارتز على iPhone
-
01-10-2019 - |
سؤال
أقوم بتطوير تطبيق أحتاج إلى رسم خطوط منقط بين بضع نقاط. حاولت
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound)
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 0, lengths, LENGTH_OF_ARRAY)
لكني أرى خطوطًا متقطعة بدلاً من الخطوط المنقطة. كيف يمكنني الحصول على خطوط منقط بدلاً من ذلك؟
المحلول
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat lengths[2];
lengths[0] = 0;
lengths[1] = dotRadius * 2;
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, dotRadius);
CGContextSetLineDash(context, 0.0f, lengths, 2);
// CGContextAddEllipseInRect(context, self.bounds);
يجب أن يعمل هذا الرمز بشكل صحيح.
نصائح أخرى
من فضلك ، انظر الصفحة الرائعة التالية حول أدوار خصائص الخط!https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/
وفقًا للصفحة أعلاه ، إليك الرمز لخط "DOT" مثل (....)
// should
CGContextSetLineCap(context, kCGLineCapRound);
// please see the role of line properties why the first should be 0 and the second should be the doulbe of the given line width
CGFloat dash[] = {0, lineWidth*2};
// the second value (0) means the span between sets of dot patterns defined by dash array
CGContextSetLineDash(context, 0, dash, 2);
لا تنتمي إلى StackOverflow