Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to draw a process indicator (not a progress indicator) with dynamic changing of stroke and fill colors according to constraints. Im finding it really difficult to draw the shape that is required. Any guidance would be great.
How could i draw this? the UI should be similar to progress view but with circles in between lines. Thanks
You should be able to draw this sort of thing using CAShapeLayers and QuartzCore.
Add the CoreGraphics.framework to your library.
At the top of your class
#import <QuartzCore/QuartzCore.h>
Here is an example of something that will look like this 0-0-0 (assuming this is what you are looking for).
// this will be a parent layer container
CALayer *progressIndicator = [[CALayer alloc] init];
// the first bubble
CAShapeLayer *bubble1 = [[CAShapeLayer alloc] init];
bubble1.fillColor = [UIColor lightGrayColor];
bubble1.strokeColor = [UIColor darkGrayColor];
bubble1.strokeWidth = 1;
bubble1.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,50,50)].CGPath;
[progressIndicator addSublayer:bubble1];
// the first connecting line
CAShapeLayer *line1 = [[CAShapeLayer alloc] init];
line1.strokeColor = [UIColor darkGrayColor];
line1.strokeWidth = 2;
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(50, 25)];
[linePath addLineToPoint:CGPointMake(75, 25)];
line1.path = linePath.CGPath;
[progressIndicator addSublayer:line1];
// ... And so on for the next 2 bubbles and 1 line. ...//
// Add it to your view.
[self.view.layer addSublayer:progressIndicator];
You can animated most properties on these objects as well (like opacity) to give your progress bar the affect of slowly filling over time.
Related
Inside UICollectionViewCell I add CAShapeLayer circle inside initWithCoder;
So here I didn't do anything unusual, its very simple. But when I launch the app, the circle is sometimes visible, but sometimes is not.
I logged the circle where I saw that the color, opacity, path and frame is set properly. But on the screen there's no circle. If I begin scrolling collection view, the circle appears right away. Also if I touch any other button or scroll on the screen. I even try to make a screenshot for you to see it, but at the moment I take a screenshot, the circle appears right away. So I can't show the problem to you.
I thing it's possible that this is a mistake made inside the iOS. Is there a way to force to redraw the circle.
here is my initializor
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
CAShapeLayer *circle = [CAShapeLayer layer];
circle.contentsScale = SCREEN_SCALE;
circle.rasterizationScale = 2.0 * SCREEN_SCALE;
circle.shouldRasterize = YES;
circle.opacity = 1.0;
circle.backgroundColor = [UIColor clearColor].CGColor;
circle.fillColor = [UIColor grayColor].CGColor;
CGRect frame = CGRectMake(0, 0, 50, 50);
circle.frame = frame;
circle.path = [UIBezierPath bezierPathWithOvalInRect:frame].CGPath;
[self.layer addSublayer:circle];
}
return self;
}
I think I didnt do anything wrong, there didnt do anything else with the circle elsewhere in the code.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to draw a circle on a UIView. The position of the view will be some values dynamically send ie,
for example if the height of the view is 100 and the value is 60 the point should be plotted at the 60th position inside the view
this is the code used to display two views
UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)];
img.backgroundColor=[UIColor blackColor];
[self.view addSubview:img];
UIView *circle=[[UIView alloc]initWithFrame:CGRectMake(10,20,50,50)];
circle.alpha=0.5;
circle.layer.cornerRadius=50;
circle.backgroundColor=[UIColor whiteColor];
[img addSubview:circle];
How can i achieve this ? Anybody please help..
---edit---
You can make UIView circular like this :
circle.layer.cornerRadius = imageView.bounds.size.width/2;
circle.layer.masksToBounds = YES;
To move the circle to new place, you can do following :
circle.center = CGPointMake (newCenterX,newCenterY);
Try this,
- (void)drawCricleAtPoint:(CGPoint)center inView:(UIView *)superview withRadius:(CGFloat)radius
{
CGPoint originOfSquare = CGPointMake((center.x-radius), (center.y-radius));
CGRect squareFrame = CGRectZero;
squareFrame.origin = originOfSquare;
squareFrame.size = CGSizeMake(2*radius, 2*radius);
UIView *circle = [[UIView alloc] initWithFrame:squareFrame];
circle.layer.cornerRadius = radius; //half of the width if enclosing square
[circle setBackgroundColor:[UIColor lightGrayColor]];
[superview addSubview:circle];
}
Try with this
-(void)drawCircle{
UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)];
img.backgroundColor=[UIColor blackColor];
[self.view addSubview:img];
UIView *circle=[[UIView alloc]initWithFrame:CGRectMake(10,20,50,50)];
circle.alpha=0.5;
circle.layer.cornerRadius=circle.frame.size.width/2;
circle.backgroundColor=[UIColor whiteColor];
[img addSubview:circle];
}
note: change only the line
circle.layer.cornerRadius=circle.frame.size.width/2;
you just simple store the value of x and y which is dynamically come. so try it. it may be help you.
UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)];
img.backgroundColor=[UIColor blackColor];
[self.view addSubview:img];
UIView *circle=[[UIView alloc]initWithFrame:CGRectMake(x,y,50,50)];
circle.alpha=0.5;
circle.layer.cornerRadius=50;
circle.backgroundColor=[UIColor whiteColor];
[img addSubview:circle];
Is it possible to mask certain area of a view, so that content of that part became invisible/hidden.
I am using a web view and wanted to remove the top round corner of view, like below attached image -
Problem -
I want to remove/hide all the content which is below red area.
What I have tried -
I have tried to add a image view on top of web view and added a masking layer on top of it, but that doesn't seems to be working for me -
UIImageView *maskImage = [[UIImageView alloc] init];
maskImage.frame = CGRectMake(256, -10, 64, 64);
maskImage.image = [UIImage imageNamed:#"maskingImage"];
[self.webView addSubview: maskImage];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = whitefoldMaskImage.frame;//CGRectMake(0, 0, 50, 100);
CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
maskLayer.path = path;
CGPathRelease(path);
maskImage.layer.mask = maskLayer;
EDIT 1 --
As per suggestion of Mundi, I have tried opaque view on top of web view.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(256, -10, 64, 64)];
view.opaque = YES;
[self.webView addSubview:view];
But that haven't worked too.
I have also gone through following threads on SO, but haven't found any solution -
CALayer: add a border only at one side
Simply mask a UIView with a rectangle
I want to remove/hide all the content which is below red area.
Simply add a opaque view above the area you want to hide. No need for a mask. E.g.
UIView *cover = [[UIView alloc] initWithFrame:topRightCornerFrame];
cover.backgroundColor = [UIColor whiteColor];
[webView addSubView:cover];
I'd to create a UITextField like find iPhone.
I used the follow code for the UITextField:
UIBezierPath *maskPathBotton = [UIBezierPath bezierPathWithRoundedRect:txtSenha.bounds
byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight
cornerRadii:CGSizeMake(8.0, 8.0)];
txtSenha.borderStyle=UITextBorderStyleNone;
CAShapeLayer *maskLayerBotton = [CAShapeLayer layer];
maskLayerBotton.frame = txtSenha.bounds;
maskLayerBotton.path = maskPathBotton.CGPath;
maskLayerBotton.fillColor=[UIColor whiteColor].CGColor;
maskLayerBotton.strokeColor=[UIColor lightGrayColor].CGColor;
maskLayerBotton.lineWidth=1.5;
[txtSenha.layer addSublayer:maskLayerBotton];
But the result wasn't so good.
In the corners the strokes are a little more different than the other lines. How can I create the same UITextField. Sorry for my English and for no image, I don't have enough reputation to upload an image.
EDIT:
UITextField from Find Iphone
And this, is that I could.
if you want rounded corner try this
textField.layer.cornerRadius = 10;
Be sure to include #import <QuartzCore/QuartzCore.h> to access this.
or
remove this line from your code
textField.borderStyle = UITextBorderStyleRoundedRect;
instead of
[textField setBackgroundColor:[UIColor clearColor]];
[textField.layer setBorderColor:[UIColor grayColor].CGColor];
[textField.layer setBorderWidth:1.0];
I need to rotate an UIImageView:
UIImageView *img = [[UIImageView alloc] init];
img.layer.borderWidth = 3.0;
img.layer.borderColor = UIColorFromRGB(0xffffff).CGColor;
img.layer.shadowColor = UIColorFromRGB(0x000000).CGColor;
CATransform3D transform = CATransform3DMakeRotation ([Helper degreesToRadians:(5)], 1, 1, 1);
img.layer.shadowRadius = 2.0;
img.layer.shouldRasterize = YES;
The problem is that the border is not smooth at all:
Try adding a transparent border (1px may be) around your image. See this link.
Check UIImage+Alpha section in that page.
I see most of the answer for this problem is to add 1px transparent border but it only works if we are not using any border around UIImageView, if UIImageView itself has the border and we rotate it then the border is not smooth. I am stil trying to figure out on how to fix that issue.
HERE'S WHAT I DID TO FIX THIS ISSUE
UIImage *thumbnail = [photo thumbnailImage:thumbnailSize.width
transparentBorder:2
cornerRadius:0
interpolationQuality:kCGInterpolationHigh];
CGPoint randomPoint = [self getRandomOriginPoint];
UIImageView *thumbnailView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, thumbnail.size.width, thumbnail.size.height)];
[thumbnailView.layer setBorderWidth:2];
[self setContentMode:UIViewContentModeCenter];
[thumbnailView.layer setBorderColor:[UIColor whiteColor].CGColor];
[thumbnailView.layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
[thumbnailView.layer setShadowRadius:3.0];
[thumbnailView.layer setShadowOpacity:1.0];
thumbnailView.layer.shouldRasterize = YES;
I used UIImage+Resize.h from here to get the thumbnail image in my code, so with this even after you rotate image with any angle the border will look perfectly fine!
Without having to use categories, solution that made the fewest presumptions
#import <QuartzCore/QuartzCore.h>
We will use the following variable
UIView *myView;
This has been confirmed to work on UIImageViews / UIViews of all types
myView.layer.shouldRasterize = YES; //the single line that solved this problem from the above
You should not need to add a border of any type for this to work.
CATransform3D rotate = CATransform3DIdentity;
myView.layer.shouldRasterize = YES;
rotate = CATransform3DRotate(rotate,M_PI_4/8,0.0,0.0,1);
myView.layer.transform = rotate;
Note: This answer is provided to consolidate the information into a single answer that removes some of the other requirements. In this example I use M_PI_4/8 as it's a nice viewing angle for if you have a Stickie Note sort of view that you are implementing.