How can i call this method in self.view? - iphone

I am making this method for drawing line now what I want is to call it in another methods how can I do that, please help me out of this that how can I add this in self.view?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGFloat dashArray[] = {2,6,4,2};
CGContextSetLineDash(context, 3, dashArray, 4);
CGContextMoveToPoint(context, 10, 200);
CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
CGContextStrokePath(context);
}

Mark the view as needing a redraw:
[self.view setNeedsDisplay];
See reference.

[self.view setNeedsDisplay];
This will call your draw rect method.

Related

Custom Cell Selected

I am drawing a custom cell, to achieve a certain look I want. However, I want to perform a different drawing based on if the cell is select. I really do not just want the default colors.
I changed content's views background color the view in this method:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
However, it just did not appear correctly, mainly it did not take into account the accessary and just colored it until the accessory indicator. Is there a better way to accomplish this?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Background
CGContextSetFillColorWithColor(context, CELL_BACKGROUND_COLOR);
CGContextMoveToPoint(context, 0.0f, 0.0f);
CGContextAddLineToPoint(context, rect.size.width, 0.0f);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
CGContextAddLineToPoint(context, 0.0f, rect.size.height);
CGContextClosePath(context);
CGContextFillPath(context);
// Top line
CGContextSetStrokeColorWithColor(context, CELL_TOP_LINE_COLOR);
CGContextSetLineWidth(context, CELL_LINE_WIDTH);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextMoveToPoint(context, 0.0f, 0.0f);
CGContextAddLineToPoint(context, rect.size.width, 0.0f);
CGContextStrokePath(context);
//Bottom line
CGContextSetStrokeColorWithColor(context, CELL_BOTTOM_LINE_COLOR);
CGContextSetLineWidth(context, CELL_LINE_WIDTH);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextMoveToPoint(context, 0.0f, rect.size.height);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
CGContextStrokePath(context);
}
I think you should modify your drawRect method and change how your color are set depending of the isSelected property. Changing the content's view background when the setSelected method will probably don't change anything since it will be overriding by the drawRect method.
I'm using this :
if(self.highlighted || self.selected) {
//set text color
textColor = [UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:0 alpha:1.0];
//set background color
[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] set];
CGContextFillRect(context, r);
}
Also check selectionStyleproperty of your cell.
I found a solution that works great, it also draws underneath the default accessory views, like the accessory indicator. I created custom view's called BackgroundView and SelectedBackgroundView, I just use the drawRect method to create a custom drawing. It works great, and performance seems to be okay. If anyone wants to see the full code let me know.
[cell setBackgroundView:[[BackgroundView alloc] init]];
[cell setSelectedBackgroundView:[[SelectedBackgroundView alloc] init]];

Pushing/Popping Graphics Context

I've been going through the Standford University IOS lessons and I am stuck on understanding what UIGraphicsPushContext and UIGraphicsPopContext does (lesson 4). From the notes, it is explained that these methods can be used to avoid affecting the current graphics states from other utility methods. Here is the example provided in the notes:
- (void)drawGreenCircle:(CGContextRef)ctxt {
UIGraphicsPushContext(ctxt);
[[UIColor greenColor] setFill];
// draw my circle
UIGraphicsPopContext();
}
- (void)drawRect:(CGRect)aRect {
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor redColor] setFill];
// do some stuff
[self drawGreenCircle:context];
// do more stuff and expect fill color to be red
}
However, when I try to test this, I don't seem to get this result. Below is a simple test case I've made that gets the current context in drawRect, sets the color, calls a utility method that sets the color to green, returns to drawRect and draws a line. I would expect based on the notes on the Standford site that the line would be red since I pushed/popped the context in my drawGreenCircle. (I realize I am not actually drawing any circle in drawGreenCircle) But I get a green line in drawRect. It seems that my drawGreenCircle method did change the color and did not revert it back to red. I would like to know what I am missing about this concept.
- (void)drawGreenCircle:(CGContextRef)ctxt {
//Doesn't actually draw circle --Just testing if the color reverts
//to red in drawRect after this returns
UIGraphicsPushContext(ctxt);
[[UIColor greenColor] setStroke];
UIGraphicsPopContext();
}
- (void)drawRect:(CGRect)aRect {
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor redColor] setStroke];
// do some stuff
[self drawGreenCircle:context];
CGContextBeginPath(context);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 500, 100);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}
You need CGContextSaveGState and CGContextRestoreGState instead. The following code works:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor greenColor] setFill];
[[UIColor blackColor] setStroke];
[self drawBlueCircle:context];
CGContextMoveToPoint(context, 55, 5);
CGContextAddLineToPoint(context, 105, 100);
CGContextAddLineToPoint(context, 5, 100);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}
- (void)drawBlueCircle:(CGContextRef)context {
CGContextSaveGState(context);
[[UIColor blueColor] setFill];
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 100, 100));
CGContextDrawPath(context, kCGPathFillStroke);
CGContextRestoreGState(context);
}
Source

drawRect is not working In Iphone

Hi i m adding UIView to window.
this view has drawRect as-
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
}
but this code is not drawing any line.
so any help for this??????
use
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
after your code.
I think Marcelo Cantos is right. You should set stroke color.
Maybe you can try this to set color.
[[UIColor blackColor] set];

create a non-rectangular UIView

I need to create a view (a uibutton) of strange shape.
Is this possible with quartz2d? i used this code for buil my UIView subclass
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
}
but this is a draw inside a standard UIView (rectangular UIView)

Why I can't draw something on my UIView?

This is my Main View, I just want it draw some thing to test, but I find that my UI don't have any thing, here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100.0f, 100.0f);
CGContextAddLineToPoint(context, 200.0f, 200.0f);
CGContextStrokePath(context);
}
This is the sample code I copied online. I assume the code is correct, it does not have any error, but nothing appear. Or... ... this code should not paste on viewDidLoad?
viewDidLoad doesn't have a context. You will need to create an image context, do your drawing, produce an image, then add it to the view like so:
UIGraphicsBeginImageContext();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100.0f, 100.0f);
CGContextAddLineToPoint(context, 200.0f, 200.0f);
CGContextStrokePath(context);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
[imgView release];
Edit:
viewDidLoad is a UIViewController method, not a UIView method. I assume this code is in your controller, am I correct? Also, viewDidLoad is only called after loading a view from a nib. Did you use a nib (xib built with Interface Builder) or did you create your view programmatically?
To draw on the view using your code, you need to override -drawRect: instead of -viewDidLoad.
- (void)drawRect:(CGRect)rect {
[super drawRect:rect]; // not necessary
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100.0f, 100.0f);
CGContextAddLineToPoint(context, 200.0f, 200.0f);
CGContextStrokePath(context);
}