Questions tagged [drawrect]

Method in UIView/NSView class. Draws the receiver’s image within the passed-in rectangle.

Signature

- (void)drawRect:(CGRect)rect

rect - The portion of the view’s bounds that needs to be updated. The first time your view is drawn, this rectangle is typically the entire visible bounds of your view. However, during subsequent drawing operations, the rectangle may specify only part of your view.

The default implementation of this method does nothing. Subclasses that use native drawing technologies (such as Core Graphics and UIKit) to draw their view’s content should override this method and implement their drawing code there.

1051 questions
114
votes
6 answers

Animate drawing of a circle

I'm looking for a way to animate the drawing of a circle. I have been able to create the circle, but it draws it all together. Here is my CircleView class: import UIKit class CircleView: UIView { override init(frame: CGRect) { …
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
61
votes
16 answers

UIView with shadow, rounded corners and custom drawRect

I have to create a custom UIView that will have round corners, a border, a shadow and its drawRect() method is overridden to provide custom drawing code with which several straight lines are drawn into the view (I need to use a fast, lightweight…
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
47
votes
2 answers

How to draw an UIImage or directly in -drawRect:?

I have an UIImage which I want to draw on a UIView. But instead of creating an UIImageView and adding this as a subview, I want to overwrite -drawRect: and draw my UIView directly. For example, my code looks like: - (void)drawRect:(CGRect)rect { …
HelloMoon
42
votes
1 answer

How to fill a path with gradient in drawRect:?

filling a path with a solid color is easy enough: CGPoint aPoint; for (id pointValue in points) { aPoint = [pointValue CGPointValue]; CGContextAddLineToPoint(context, aPoint.x, aPoint.y); } [[UIColor redColor] setFill]; [[UIColor blackColor]…
Derrick
  • 2,356
  • 5
  • 32
  • 43
41
votes
7 answers

Draw text along circular path in Swift for iOS

I am looking for some up to date help/hints on how to draw simple single line strings around the edge of a circle using Swift2 for iOS9. I see quite dated examples involving old ObjC fragments, and oft limited to OS X only. Is this even possible in…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
40
votes
2 answers

drawRect on top of subviews

I have subclassed UIView and added a drawRect method to it. Then I define a view using this custom class and add subviews to it. The problem is that drawRect seem to draw stuff under the subviews (hence not visible). I want stuff that drawRect…
RawMean
  • 8,374
  • 6
  • 55
  • 82
38
votes
6 answers

Most efficient way to draw part of an image in iOS

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 { …
hpique
  • 119,096
  • 131
  • 338
  • 476
35
votes
5 answers

iOS draw filled Circles

Not a graphics programmer here, so I'm trying to stumble through this. I'm trying to draw 9 filled circles, each a different color, each with a white border. The UIView's frame is CGRectMake (0,0,60,60). See attached image. The problem is I'm…
RegularExpression
  • 3,531
  • 2
  • 25
  • 36
32
votes
5 answers

How can I tint a UIImage with gradient?

I searched everywhere but didn't find the solution. I have image 1. How can I programatically tint them with gradient to get images 2 and 3? Here are those images: Tints that I applied to them via Photoshop are simple 2-color linear gradients. And…
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
29
votes
9 answers

iOS invert mask in drawRect

With the code below, I am successfully masking part of my drawing, but it's the inverse of what I want masked. This masks the inner portion of the drawing, where I would like to mask the outer portion. Is there a simple way to invert this…
OWolf
  • 5,012
  • 15
  • 58
  • 93
25
votes
2 answers

CGContextSaveGState vs UIGraphicsPushContext

There are two drawRect methods: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); // do drawing here CGContextRestoreGState(context); } And -…
Vadym
  • 1,067
  • 1
  • 13
  • 24
24
votes
2 answers

Drawing colored text in UIView's -drawRect: method

I am trying to draw colored text in my UIView subclass. Right now I am using the Single View app template (for testing). There are no modifications except the drawRect: method. The text is drawn but it is always black no matter what I set the color…
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
23
votes
1 answer

draw outside of UIView's bounds from drawRect

My question is very similar to this one Not drawing outside bounds when clipToBounds=NO which received no clear answer. Basically I have a UIView, and I want to draw a line from the center of it, to the edge of the screen. Calculating where these…
Kenny Winker
  • 11,919
  • 7
  • 56
  • 78
23
votes
5 answers

How to Call drawRect programmatically in objective c

How to Call drawRect programmatically in objective c ? I want to call drawrect method of a view in my UItabbarcontroller. How i can do this ? Thanks in advance.. Edit I have to call when the view is not visible currently. It will be the first time…
S.P.
  • 5,427
  • 11
  • 56
  • 83
23
votes
2 answers

Can't stroke path after filling it

The code below nicely creates a rounded rectangle that is defined by a CGRect (rectRect). It fills fine, but I am not getting a stroke. Any ideas why I can't see the stroke? -(void)drawRect:(CGRect)rect { CGContextRef ctx =…
OWolf
  • 5,012
  • 15
  • 58
  • 93
1
2 3
70 71