move uiimage inside a bezier curve - objective-c

I have drawn a circle using bezier curve, I am using this circle as a mask to a uiimage view. Now, how can i move the image inside the circle without moving the circle using touches.
here is my code.
CAShapeLayer *maskLayer = [CAShapeLayer layer];
aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(220, 220)
radius:170
startAngle:0
endAngle:DEGREES_TO_RADIANS(360)
clockwise:YES];
maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
maskLayer.path = [aPath CGPath];
maskLayer.masksToBounds=YES;
imageView1.layer.mask = maskLayer;
[self.view addSubview:imageView1];

The coordinates of the mask is the same as the coordinates of your views layer = it moves along with the view.
You can add the image inside another view and mask that view instead. Then you can move the image inside the other view and the mask will stay the same.
Alternatively (but really the same solution) you could keep the mask on that layer and add a sublayer with the image and move that instead.

Related

Draw line CAShapeLayer layer color by rect region

The Problem I facing right now is that after I draw the dots and lines by CAShapeLayer, I need to modify both of their colors by changing with different for rect regions. The regions I set right now have four portions, and following are the sample codes:
- (void)drawChartWithRecord:(Chart *)Chart
lineWidth:(CGFloat)lineWidth
radius:(CGFloat)radius
interval:(CGFloat)interval
rect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
float start_angle = 0;
float end_angle = (float) (2 * M_PI);
CAShapeLayer *avg = [CAShapeLayer layer];
avg.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(X,Y) radius:radius startAngle:start_angle endAngle:end_angle clockwise:YES].CGPath;
CGContextMoveToPoint(context, X, Y);
CGContextAddLineToPoint(context, pastX, pastY);
CGContextSetStrokeColorWithColor(context, LINE_COLOR);
CGContextSetLineWidth(context, lineWidth);
CGContextStrokePath(context);
[self.layer addSublayer:avg];
}
Upon with the line and dots codes; however, I didn't find out any method to import the rect to the same CAShapeLayer. If I make a new layer as
CAShapeLayer *mask = [CAShapeLayer layer];
There would be a problem that If I use
[self.layer addSublayer:mask];
Two of the sublayers would mix, and I don't want to change my background color.
Thanks for everyone's help : )
Unfortunately, I was wrong about my question.
The thing I want could be easy to solve by the CAGradientLayer & CAShapeLayer. Once you finish your own CAShapeLayer, by using
[self.gradientLayer setMask:avgCircle];
you can easily add the the your own gradient color to the line shape CAShapeLayer
Be sure add addSublayer: for both CAGradientLayer & CAShapeLayer

Don't display subviews outside of UIBezierPath

I have one UIView which is drawn with:
- (void)drawRect:(CGRect)rect
{
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.widthOfLine, self.heightOfLine)];
[bezierPath fill];
}
and whose frame is centered within the iOS window. I want to place a bunch of smaller (5px by 5px) views within this view randomly, which I am doing fine with arc4random() function and drawing the views. However, I can't seem to figure out how to cut off all views outside the bezierPathWithOvalInRect, it will just display them everywhere in the first UIView's frame. Anyone know what to do for this?
edit:
For clarity, I have defined another view as:
- (void)drawRect:(CGRect)rect
{
UIBezierPath *drawPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.xPosition, self.yPosition, self.width, self.width)];
[[UIColor whiteColor] setFill];
[drawPath fill];
}
and I am adding a large number of these to the first view as subviews, but I want them to not be displayed outside the oval bezier path. Is there a way to a) only add them within the oval bezier path as opposed to within the entire frame, or b) a way to clip all that are outside the oval from the view?
In order to clip all subviews to a particular UIBezierPath, you'll want to set the mask property of the view's layer property. If you set the layer.mask to a CAShapeLayer of that path, then all subviews will be cropped to that path.
I use the following code to do just that in my app:
// create your view that'll hold all the subviews that
// need to be clipped to the path
CGRect anyRect = ...;
UIView* clippedView = [[UIView alloc] initWithFrame:anyRect];
clippedView.clipsToBounds = YES;
// now create the shape layer that we'll use to clip
CAShapeLayer* maskingLayer = [CAShapeLayer layer];
[maskingLayer setPath:bezierPath.CGPath];
maskingLayer.frame = backingImageHolder.bounds;
// set the mask property of the layer, and this will
// make sure that all subviews are only visible inside
// this path
clippedView.layer.mask = maskingLayer;
// now any subviews you add won't show outside of that path
UIView* anySubview = ...;
[clippedView addSubview:anySubview];

Navigation bar stops working after applying a layer mask

I used a CAShapeLayer to create a mask, which I gave round corners at the top left/right then applied it to my navigationController's navigationBar.
It works like a charm, but I've now noticed I can't use buttons on the navigation bar!
Here's the code I use (in viewDidLoad):
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.navigationController.navigationBar.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(6.0, 6.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.navigationController.navigationBar.bounds;
maskLayer.path = maskPath.CGPath;
self.navigationController.navigationBar.layer.mask = maskLayer;
You would need to set the buttons programmatically after setting the layer mask.

Draw image into area between two concentric circles in Xcode / objective-c

I have an image (its actually circular) and wish to only draw the part of that image that corresponds to the area between two concentric circles, the outer one is fixed, the inner one can adjust in size.
The outer concentric circle will always correspond to the outside edge of the image - but a "hole" needs to be left in the drawn image - corresponding to the size of the smaller concentric circle.
I'm trying to do this in objective-c for iOS - to draw a variable-sized "viewfinder" control.
Any help much appreciated.
Sounds like the standard case to use CGContextEOClip. Haven't checked this code, but something like:
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, outerCircle);
CGPathAddEllipseInRect(path, NULL, innerCircle);
CGContextEOClip(ctxt);
//Draw your stuff
The easiest and most performant solution would probably be to use a UIImageView and apply a CAShapeLayer as the mask of its layer.
Example:
UIImage *image = ...;
CGFloat ringWidth = 20.0;
CGRect outerCircleRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGRect innerCircleRect = CGRectInset(outerCircleRect, ringWidth, ringWidth);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:outerCircleRect];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:innerCircleRect]];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.fillRule = kCAFillRuleEvenOdd;
shapeLayer.path = [path CGPath];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.layer.mask = shapeLayer;
[self.view addSubview:imageView];
You'll have to include the QuartzCore framework for this to work.
When you change the radius of the inner circle, you can simply assign a new path to the shape layer. This can even be animated.

How to restore rounded corners for a UITableViewCell filled with an image?

I have a UITableViewCell, and I want to use the image property to fill it with an image. When it is first in a grouped UITableView, I want it to have the standard rounded corners.
Unfortnately, the image fills the rounded corners as well.. Is there any way to retain them without using a transparent image?
You can round the corners of any view programmatically by using its layer property. If you play about with the cornerRadius property of the layer you should be able to achieve the results you want.
#include <QuartzCore/QuartzCore.h>
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:myImage];
imgView.layer.cornerRadius = 10.0;
If you just want to round some of the corners, you should look at the UIBezierPath API and use the path to mask your image. This isn't tested but it should point you in the right direction:
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageView.frame;
maskLayer.path = path;
imageView.layer.mask = maskLayer;
I just made my non-transparent images have the rounded corner. Take a screen shot to get the dimensions of the rounded corner then use that as the basis for your image.