I'm implementing some charts with some user interactions, like long-pressing to view the full data under the finger, two fingers to draw a line, and other stuff.
For a start, how would I implement the kind of interaction the iphone stocks app does in the landscape mode of the price chart?
Manipulate a CALayer for the yellow vertical line and the yellow dot? Are those coming from images or rendered? Use a UIView (or UIImageView) instead? Or render them in drawRect?
I think it's really just an implementation detail so it's up to you how to do it. I would guess that they're doing everything in -drawRect, however, you could implement it with Core Animation layers if you wanted to.
If you're already drawing the graph with -drawRect, then it would make sense to stick with that, however, if you just want to overlay layers for the yellow dot and the vertical yellow line you can just use regular CALayer s and change their position in response to -touchesMoved.
// Create a line layer with a 1px width and yellow bg
CALayer *lineLayer = [CALayer layer];
[lineLayer setBounds:CGRectMake(0.0f, 0.0f, 1.0f, parentViewHeight)];
[lineLayer setPosition:CGPointMake(currentX, parentViewHeight/2.0f)];
[lineLayer setBackgroundColor:[[UIColor yellowColor] CGColor]];
[[parentView layer] addSublayer:lineLayer];
// Create a dot layer with the contents set to a CGImageRef
UIImage *dotImage = [UIImage imageNamed:#"yellowdot.png"];
CALayer *dotLayer = [CALayer layer];
[dotLayer setBounds:CGRectMake(0.0f, 0.0f, [dotImage size].width,
[dotImage size].height)];
[dotLayer setPosition:[lineLayer position]];
[dotLayer setContents:[dotImage CGImage]];
[[parentView layer] addSublayer:dotLayer];
You'll need to make these layers ivars of your class so that when you receive your touch events, you can update their positions.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
CGPoint current = [[touches anyObject] locationInView:parentView];
[lineLayer setPosition:CGPointMake(current.x, [lineLayer position].y)];
[dotLayer setPosition:CGPointMake(current.x, [self yForStockPriceAtPoint:current])];
[super touchesMoved:touches withEvent:event];
}
Related
I have a custom UIView which is supposed to be a round rectangle which, when you begin editing a UILabel inside, the round rectangle will grow its shape (CABasic animation on a CAShapeLayer mask path and outline path). So to be clear, the custom UIView is organized like this:
MyCustomView has a ClipView.
ClipView uses drawRect to draw the background of the round rectangle. (clipview == view that gets clipped)
Then, I call the following code to do the CAShapeLayer manipulation:
//0. CLIP
shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = self.bounds;
[shapeLayer setBounds:shapeRect];
[shapeLayer setPosition:CGPointMake((shapeRect.size.width)/2.0f, (shapeRect.size.height)/2.0f)];
[shapeLayer setFillColor:[[UIColor colorWithRed:0 green:1 blue:0 alpha:1.0f] CGColor]];
[shapeLayer setStrokeColor:[[UIColor redColor] CGColor] ];
[shapeLayer setLineWidth:1];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setOpacity:1];
CGRect shapeLayerPathRect = CGRectMake(0, 0, self.bounds.size.width, 44.0f);
shapeLayer.path = [self roundRectPathForRectangle:shapeLayerPathRect andRadius:CORNER_RADIUS];
[[self.clipView layer] setMask:shapeLayer];
//1. draw the shaded outline of a round rectangle
outlineLayer = [self topDownShadowShapeLayer]; //setup for my CAShapeLayer, involving shadow stuff, line color, etc
outlineLayer.path = [self roundRectPathForRectangle:shapeLayerPathRect andRadius:CORNER_RADIUS];
[self.clipView.layer addSublayer:outlineLayer];
Although the animation the proceeds is the quickest approach (stretching both CAShapeLayers), the entire element (MyCustomView) causes everything to be a lot slower! For example, UIView animations that go on, also when MyCustomView is inside a ScrollView it is VERY jumpy.
I'm mostly concerned about the UIScrollView problem. Why is it so choppy for a UIScrollView to translate these layers around? Is there a good solution / what's another approach that can achieve the same effect?
I want to make the glow effect spread outside of thisView.
I used this code to make Half-Rounded rect cornered UIVew.
Round two corners in UIView
Here is my code.
+ (UIView *)makeUpperHalfRoundedView:(UIView *)view {
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = view.bounds;
UIBezierPath *roundedPath =
[UIBezierPath bezierPathWithRoundedRect:maskLayer.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(8.f, 8.f)];
maskLayer.fillColor = [[UIColor blackColor] CGColor];
maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
maskLayer.path = [roundedPath CGPath];
// maskLayer.masksToBounds = NO;
//Don't add masks to layers already in the hierarchy!
UIView *superview = [view superview];
[view removeFromSuperview];
view.layer.mask = maskLayer;
[superview addSubview:view];
return view;
}
And there are two image buttons in this view closed to the boundary.
If a touch event happen on buttons, glow effect shows(showsTouchWhenHighlighted)
Before using this piece of code, I just used
thisView.layer.cornerRadius = 8;
thisVIew.layer.masksToBounds = NO;
and glow effect spread outside of 'thisView'.
But after using CAShapeLayer in 'thisView' to masking half-roused rect, the glow effect are masked by boundary.
What you're seeing is exactly how a mask is supposed to work. You are applying the mask to the layer of the superview. Let's call that view superview (just as in your code). Okay, so what will that do? It will mask superview and all its sublayers. That includes its subviews. Its subviews are actually its sublayers.
So, since the buttons are subviews of superview, they are masked like everything else inside superview.
If you don't want that to happen, don't make the buttons subviews of superview. They can sit on top of superview without being its subviews, for example.
I want to draw on UIImageView, following is my code for drawing. My image view mode is aspect fit. When i draw using the following code, since i am using drawinrect and giving the rect of UIImageView so all the images are scaled down to the size to the `imageview. I want to draw the image of size of image and not image view.Since i am detecting the touch point on the imageview so the point of tap and actual drawing on image is different. Can any body tell me how to draw on image directly. and how to calculate this shift or difference in the point of touch on image rather than image view.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentTouch = [[touches anyObject] locationInView:self];
UIGraphicsBeginImageContext(self.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.image.size.width, self.bounds.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, _brushSize);
CGContextBeginPath(context) ;
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context,currentTouch.x,currentTouch.y) ;
CGContextSetAlpha( context , 1 );
CGContextSetStrokeColorWithColor(context, [_brushColor CGColor]);
CGContextStrokePath(context) ;
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = currentTouch;
}
You shouldn't really be us classing UIImageView. It isn't intended to be subclassed.
A better approach to this would be to have a UIView instead of the UIImageView.
Then two possible ways is to either...
Draw the UIImage directly into the UIView inside drawRect but this will give you the same problem that you are currently having.
Or...
In the UIView have a UIImageView. Resize this so that it is the correct size/shape for the UIImage. (Ie do the scaling yourself) then over the UIImageView put a second UIView (and subclass this) this second one will be capped DrawingView or something. You can now do all your drawing inside here. And all your touch detection will be in here too. So you no longer need to convert the touch points into different drawing points.
I want to place some "handles" on a UIView I have on-screen.
So, I'm creating more UIViews, one at each corner of the UIView's frame rectangle, and I simply want to draw a circle that "fills" the rectangle that is the frame of the "handle" UIView.
Here's what I mean:
How I create the "HandleView":
CGPoint upperLeft = CGPointMake([[self viewToMove] frame].origin.x - 5, [[self viewToMove] frame].origin.y - 5);
HandleView *uL = [[HandleView alloc] initWithFrame:CGRectMake(upperLeft.x, upperLeft.y, 10, 10)];
[self setUpperLeftHandle:uL];
[uL release];
[[self view] addSubview:[self upperLeftHandle]];
drawRect: for HandleView:
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor orangeColor] set];
CGContextFillEllipseInRect(context, [self frame]);
}
What I'm getting is just a set of black rectangles where I place the HandleViews. I'm not sure why they're black, but I can change the color by changing the [HandleView backgroundColor] property. I cannot, though, get anything to DRAW on this view. Calling setNeedsDisplay doesn't seem to make any difference.
Also, the drawRect: method IS being called, so there's a problem with the code there, probably not anywhere else.
It's been a while since I've messed with custom drawing, but I don't remember it being this hard.
What am I missing?
Thanks!!!
Update:
Modified code to this:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 100, 100, 100, 1.0);
CGContextFillEllipseInRect(context, rect);
}
Now, I see the circle, but only where it overlays another view. Where it's just sitting on top of the "background" UIView, I get nothing I can see. Also, even though the color is (100, 100, 100), it shows up white/light-light-gray.
What's the deal?
This might work:
- (void)drawRect:(CGRect)rect {
// Drawing code
[[UIColor orangeColor] setFill];
[[UIBezierPath bezierPathWithOvalInRect:rect] fill];
}
The problem is with the following line:
CGContextFillEllipseInRect(context, [self frame]);
You need to change this to the following:
CGContextFillEllipseInRect(context, [self bounds]);
Origin of frame is the left-top corner location of view in the parent. So, it can be any value within the limits of the size of parent view.
So, while drawing with frame, you will be offsetting Fillellipse rect location which happens to be beyond the visible rect of the handle view.
Hence, you were not able to see anything.
As for the color showing up as white, it's because CGContextSetRGBFillColor() expects values from 0.0 to 1.0 for the color component values (not 0-255). So, by passing 100, you were effectively passing 1.0, which was creating a white color.
i want to get only those pixels which are inside an irregular shape..using core graphics not openGL.here is an image where i have drawn an irregular shape.
and here is the drawing code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(drawLineClicked)
{
UITouch *touch = [touches anyObject];
if ([touch view] == EditImageView)
{
lastPoint = [touch locationInView:self.view];
[self drawLines:10.0 andColorWithRed:1.0 Green:0.0 Blue:0.0 Alpha:1.0];
[self.view setNeedsDisplay];
currentPoint = lastPoint;
}
}
}
-(void)drawLines:(CGFloat)withWidth andColorWithRed:(CGFloat)red Green:(CGFloat)green Blue:(CGFloat)blue Alpha:(CGFloat)alpha
{
UIGraphicsBeginImageContext(Image.size);
[EditImageView.image drawInRect:CGRectMake(0, 0, Image.size.width, Image.size.height)];
ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetAllowsAntialiasing(ctx,TRUE);
CGContextFlush(ctx);
//sets the line width for a graphic context
CGContextSetLineWidth(ctx,withWidth);
//set the line colour
CGContextSetRGBStrokeColor(ctx, red, green, blue, alpha);
CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y);
CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);
CGContextStrokePath(ctx);
EditImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Here's just tossing out an idea:
Instead of drawing the red line directly on the image, I'd duplicate the image, add it as a new layer on top of the original image, and give it a totally transparent mask (so the new layer is "invisible").
Then when the user draws the red line, use that to build a path as a mask on the invisible layer. When the path is finished, fill in the path with black (on the mask), to make that portion totally opaque. You could maybe then resize the top layer (that's been masked) to be the bounding rectangle of the drawn path.
The opaque pixels on the topmost layer will then be the ones that were bounded by the drawn path, and you can then do what you please with it (draw it to a new UIImage, etc).
Hope that makes sense...
Take a look at my old sample code project here: Cropped Image
It's for Cocoa, not Cocoa Touch, but the idea is the same. You can crop an image by compositing using the SourceIn compositing mode.