quartz-2d

UIView with a Dashed line

三世轮回 提交于 2019-11-27 03:41:09
What I have: To create this line, I basically have an UIView and I do the following: void setLayerToLineFromAToB(CALayer *layer, CGPoint a, CGPoint b, CGFloat lineWidth) { CGPoint center = { 0.5 * (a.x + b.x), 0.5 * (a.y + b.y) }; CGFloat length = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); CGFloat angle = atan2(a.y - b.y, a.x - b.x); layer.position = center; layer.bounds = (CGRect) { {0, 0}, { length + lineWidth, lineWidth } }; layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1); } Note: This code was found here on stackoverflow, so if someone can give me the

How to map atan2() to degrees 0-360

人盡茶涼 提交于 2019-11-27 03:05:43
atan2(y,x) has that discontinuity at 180° where it switches to -180°..0° going clockwise. How do I map the range of values to 0°..360°? here is my code: CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y); float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width); I'm calculating the direction of a swiping touch event given the startPoint and endPoint, both XY point structs. The code is for the iPhone but any language that supports atan2f() will do. Thanks for your help guys, with both the general solution and code. Update : I made erikkallen's answer

‘invalid context 0x0’ error when using CGContext* functions

末鹿安然 提交于 2019-11-26 21:53:43
问题 /* Adding the Path */ UserGraphBuff = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(UserGraphBuff,5,10,0,1); CGContextSetLineWidth(UserGraphBuff, 2 ); CGContextBeginPath(UserGraphBuff); //line to last user point CGContextAddLineToPoint(UserGraphBuff, (*xVal)[sizeof xVal / sizeof *xVal - 1], (*yNewVal)[sizeof yNewVal / sizeof *yNewVal - 1]); //line to rest of user points in reverse order for (int i = sizeof xVal / sizeof *xVal - 1; i > -1; i--){ CGContextAddLineToPoint

Most efficient way to draw part of an image in iOS

被刻印的时光 ゝ 提交于 2019-11-26 18:47:02
问题 Given an UIImage and a CGRect , what is the most efficient way (in memory and time) to draw the part of the image corresponding to the CGRect (without scaling)? For reference, this is how I currently do it: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect frameRect = CGRectMake(frameOrigin.x + rect.origin.x, frameOrigin.y + rect.origin.y, rect.size.width, rect.size.height); CGImageRef imageRef = CGImageCreateWithImageInRect(image_.CGImage, frameRect

Curve text on existing circle

末鹿安然 提交于 2019-11-26 18:12:22
For an application I am building I have drawn 2 circles. One a bit bigger than the other. I want to curve text between those lines, for a circular menu I am building. I read most stuff about curving a text that you have to split up your text in characters and draw each character on it's own with the right angle in mind (by rotating the context you are drawing on). I just can't wrap my head around on how to get the right angles and positions for my characters. I included a screenshot on what the menu, at the moment, look like. Only the texts I added by are loaded from an image in an UIImageView

How can I draw an arrow using Core Graphics?

穿精又带淫゛_ 提交于 2019-11-26 15:04:57
问题 I need to draw line with arrow on its end in my Draw app. I'm not good in trigonometry, so can't solve this problem. The user put his finger on the screen and draw the line in any direction. So, the arrow should appear on the line end. 回答1: UPDATE I've posted a Swift version of this answer separately. ORIGINAL This is a fun little problem. First of all, there are lots of ways to draw arrows, with curved or straight sides. Let's pick a very simple way and label the measurements we'll need: We

Drawing formulas with Quartz 2d

£可爱£侵袭症+ 提交于 2019-11-26 14:33:48
问题 In my iPhone App I'd like to draw a few formulas. How can I manage that with quartz 2d? Is there a way to build formulas like for example in latex? Or are there any existing frameworks? Thanks. 回答1: As the developer of an iPhone application which does just that, trust me when I say typesetting equations is not a trivial undertaking. In my case, I used Core Animation layers to construct the sub-elements of a parsed equation. The equations are constructed hierarchically, and the operations that

How to use CGAffineTransformMakeRotation?

北慕城南 提交于 2019-11-26 11:01:44
问题 I want to draw text use Quartz 2D. The \"menu\" direction is wrong. I want \"Menu\" still readable and have 45 degree with X-axis. Below is my code: CGContextSelectFont(context, \"Arial\", 12, kCGEncodingMacRoman); CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetRGBFillColor(context, 0, 0, 0, 1); // 6 CGContextSetRGBStrokeColor(context, 0, 0, 0, 1); CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0)); CGContextSetTextMatrix(context,

UIView with a Dashed line

橙三吉。 提交于 2019-11-26 10:31:46
问题 What I have: To create this line, I basically have an UIView and I do the following: void setLayerToLineFromAToB(CALayer *layer, CGPoint a, CGPoint b, CGFloat lineWidth) { CGPoint center = { 0.5 * (a.x + b.x), 0.5 * (a.y + b.y) }; CGFloat length = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); CGFloat angle = atan2(a.y - b.y, a.x - b.x); layer.position = center; layer.bounds = (CGRect) { {0, 0}, { length + lineWidth, lineWidth } }; layer.transform = CATransform3DMakeRotation

Curve text on existing circle

社会主义新天地 提交于 2019-11-26 06:14:45
问题 For an application I am building I have drawn 2 circles. One a bit bigger than the other. I want to curve text between those lines, for a circular menu I am building. I read most stuff about curving a text that you have to split up your text in characters and draw each character on it\'s own with the right angle in mind (by rotating the context you are drawing on). I just can\'t wrap my head around on how to get the right angles and positions for my characters. I included a screenshot on what