Clip UITableView Cell's Separator - iphone

I've been trying to build a view that looks similar to the "stocks" application (surprise). What I would like is to have the scroller bar outside of my rounded table view, shown in the image link below, but clip the cell separators. You can see how they overlap the scroller bar, which is annoying. I can't figure out how Apple put the scroller bar in a spot that looks like it is outside their table. Any ideas? (sorry, can't imbed images yet because I'm a new user).
Scroller Problem Image

You may draw your own separators in drawRect method of UITableViewCell:
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextSetLineWidth(ctx, 1.0f);
CGContextMoveToPoint(ctx, 0, self.bounds.size.height);
CGContextAddLineToPoint(ctx, self.bounds.size.width - 20, self.bounds.size.height);
CGContextStrokePath(ctx);
[super drawRect:rect];
}

Related

Multicolor line drawing

I am creating a drawing app. One of the features is multicolour line drawing. It should work like user touch up the screen and leads on it drawing a line. Colour of the line changes smoothly. Like that http://www.examples.pavelgatilov.com/Screen%20Shot%202013-09-22%20at%208.37.42%20PM.png
I tried several approaches, but haven't been lucky.
My line drawing method is below:
- (void) drawLineFrom:(CGPoint)from to:(CGPoint)to width:(CGFloat)width
{
self.drawColor = toolColor;
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1.0f, -1.0f);
CGContextTranslateCTM(ctx, 0.0f, -self.frame.size.height);
if (drawImage != nil) {
CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
CGContextDrawImage(ctx, rect, drawImage.CGImage);
}
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, width);
CGContextSetStrokeColorWithColor(ctx, self.drawColor.CGColor);
CGContextMoveToPoint(ctx, from.x, from.y);
CGContextAddLineToPoint(ctx, to.x, to.y);
CGContextStrokePath(ctx);
CGContextFlush(ctx);
drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
drawLayer.contents = (id)drawImage.CGImage;
}
Thanks for your help
Depending on exactly what colours, how they change and what effect you want / what happens with turns in the line, you may want to look at some combination of:
CGContextDrawLinearGradient with masking to the users drawn path.
colorWithPatternImage:
CGContextDrawLinearGradient drawn behind another layer and drawing transparency into the top layer with kCGBlendModeClear

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]];

drawing rectangle on button click in viewcontroller class in objective c IOS5

simply I am beginner in developing on ipad and I need to draw rectangle at point x,y with width and height when i clicked or touched button .
I searched on google but i didn't find anything working in button handler
Create rectangleButton in viewDidLoad method and write -(void)rectangleButtonSelected method in your ViewController.m. And also create class RectangleView of UIView.
-(void)rectangleButtonSelected{
RectangleView *temp = [[RectangleView alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
temp.center = CGPointMake(arc4random()%100, arc4random()%200);
NSLog(#"The Main center Point : %f %f",temp.center.x,temp.center.y);
[self.view addSubview:temp];
}
Implement - (void)drawRect:(CGRect)rect method in RectanglerView.m
- (void)drawRect:(CGRect)rect
{
// Drawing code
context =UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// And draw with a blue fill color
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, self.bounds);
CGContextStrokePath(context);
// Close the path
CGContextClosePath(context);
// Fill & stroke the path
CGContextDrawPath(context, kCGPathFillStroke);
}
I hope it will be helpful to you
You actually need to do this with state. There's only one place you're allowed to do drawing, and that's in drawRect:. So, when the user clicks the button, you need to set some instance variable, like _isShowingRectangle or something, then call setNeedsDisplay on your custom UIView where you're doing your drawing.
Then, in your custom view's drawRect:, you can check if that state variable is set, and draw (or not draw) the rectangle. The code for drawing depends on which graphics layer you're using, but it's probably going to be something like
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctxt,[[UIColor blackColor] CGColor]);
CGContextSetLineWidth(ctxt,3.0);
CGContextStrokeRect(ctxt,CGRectMake(10,20,100,50));

iphone uiview circle?

Newbie Q.
I wish to subclass a UIView so that it renders a circle.
How is that done in an iPhone?
In the drawRect: method do:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f); // white color
CGContextFillEllipseInRect(ctx, CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)); // a white filled circle with a diameter of 100 pixels, centered in (60, 60)
UIGraphicsPopContext();
}

How to do a radar animation in the iPhone SDK?

Does anyone know how to animate a radar animation like the image below?
alt text http://img197.imageshack.us/img197/7124/circle0.png
With it growing outwards? I have to draw the circle using Quartz or some other way?
To draw a static circle in a UIView subclass:
- (void)drawRect:(CGRect)rect
{
CGRect rect = { 0.0f, 0.0f, 100.0f, 100.0f };
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 2.0f);
CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context);
}
From there you just need to animate it with a timer, vary the size of the rect, and add more circles.