UIImageView cornerradius and CGrect - ios

I am making a game where you touch a ball and have to pass some bars. However the ball must not touch those bars. The ball is a gif-image. My problem is: The ball is a circle but the uiimageview is a square. If the corners of the uiimageview (which is not the ball image) touch the bars it says that the ball touches the bar. even if I do that
// imageMover is the UIImageView
// the radius of the ball is 30.0
[[imageMover layer] setMasksToBounds:YES];
[[imageMover layer] setCornerRadius:30.0f];
there is no change. I think i need a rounded frame (CGRect) but how do you "create" a rounded frame?

try this
imageMover.layer.shouldRasterize = YES;
imageMover.clipsToBounds = YES;
imageMover.layer.cornerRadius = 10.0f; // what number you want

you cannot creat a rounded frame.
but you can create an UIImageView with rounded corners.
Import the QuartzCore (#import ) header and play with the layer property of the UIImageView.
imageMover.layer.cornerRadius = yourRadius;
imageMover.clipsToBounds = YES;
see the CALayer class reference for detail:
https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html
thanks!

Related

Draw a plus (+) sign using CALayer

I am not good with CALayer but I need to draw a plus (+) sign and I don't want to use an image as I want to animate the drawing. Any help?
After all the down votes, I was able to do it myself. Here's how for others who might need this
CGFloat height = 2.f;
CGFloat width = 3.f;
CGFloat cornerRadius = 1.f;
CALayer *hLayer = [CALayer layer];//this is the left - right stroke
hLayer.frame = CGRectMake(0, CGRectGetMidY(self.bounds)-(height/2), width, height);
hLayer.cornerRadius = cornerRadius;
CALayer *vLayer = [CALayer layer];// this is the top - bottom stroke
vLayer.frame = CGRectMake(CGRectGetMidX(self.bounds) - (height/2), -3,height, width);
vLayer.cornerRadius = cornerRadius;
[self.layer addSublayer:hLayer];
[self.layer addSublayer:vLayer];
As luk2302 says, use a CAShapeLayer.
You can install a CGPath into a CAShapeLayer. You can get a CGPath from any UIBezierPath. (It has a CGPath property that lets you get to the underlying CGPath object for any UIBezierPath.)
I suggest reading up on UIBezierPath. It has methods moveToPoint and addLineToPoint.
You'd move to the top of your plus, add a line down, then move to the left of your plus and add a line across.
Note that you can also animate images, depending on the type of animation you are after. What kind of animation do you need to do?
Another simple way would be to use two UIView with the same background color and add them as a subview to another UIView. Then you can use the UIView animateWithDuration:... method to do your simple animations.

iOS: draw a UIImage of circle at a UIView's center

I have a circle image with radius 50, and now I want to put the circyle's center the same as my View's center like this:
- (void)drawRect:(CGRect)rect {
UIImage *arrow = [UIImage imageWithPDFNamed:#"circle" atSize:CGSizeMake(50, 50)];
[arrow drawAtPoint:self.center];
}
But drawAtPoint is not drawing at the center.
I want to know how to locate the center of the image? so I can draw it?
From Apple's documentation for UIImage
This method draws the entire image in the current graphics context,
respecting the image’s orientation setting. In the default coordinate
system, images are situated down and to the right of the specified
point. This method respects any transforms applied to the current
graphics context, however.
So offset the center with half the image width and height to draw the center of the image on the center of your view.
- (void)drawRect:(CGRect)rect {
CGSize imageSize = CGSizeMake(50,50);
UIImage *arrow = [UIImage imageWithPDFNamed:#"circle" atSize:imageSize];
[arrow drawAtPoint:CGPointMake(self.center.x-imageSize.width/2., self.center.y-imageSize.height/2.)];
}
You don't need to draw it, just assign it's center to your view's center
arrowsuperview.center = self.center
Try this. This is how you find the center of a subview inside a view
childView.center = CGPointMake(parentView.bounds.size.width / 2, parentView.bounds.size.height / 2);
try this..
UIImageView *circleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
circleImageView.image = yourCircleImage;
circleImageView.center = self.view.center;
[circleImageView setContentMode:UIViewContentModeCenter];
[self.view addSubview:circleImageView];
Calculate the center of the UIView using following code and draw the image using the drawAtPoint function.
- (void)drawRect:(CGRect)rect
{
CGPoint ptCenter = {rect.size.width / 2.0f, rect.size.height / 2.0f};
[arrow drawAtPoint:ptCenter];
}
Another simple approach:
You can also achieve the result using storyboard/xib and Auto Layout. Instead of drawing the image in drawRect use an UIImageView to hold your image and position the imageview using Autolayout constraints.

Create a complex CGRect

I have a view with a border of 10 pixels drawn on the method.
I need to update the border color and I use [self setNeedsDisplay] to make it redraw
the view.
Since I need to update only the border I want to use : [self setNeedsDisplayInRect:rect] so it will draw only the border.
How can I get a rect of only the border with out the other areas of the view?
Thanks
Shani
You can't because a CGRect is rectangle, so it is a convex shape that can't have holes in it.
But you can decompose the border into four rectangles and call [self setNeedsDisplayInRect:rect] four times.
Also, if you import QuartzCore, you can probably use the property borderColor of the view's layer:
#import <QuartzCore/QuartzCore.h>
// ...
view.layer.borderWidth = 10;
view.layer.borderColor = [UIColor redColor].CGColor;
// And to change it later
view.layer.borderColor = [UIColor greenColor].CGColor;
You could get four CGRects around each part of the border (top, right, bottom, and left) and call the method four times with each of them.

Custom Button frame doesn't look as good as Round Rect UIButton

I'm trying to draw a custom button frame as follows:
UIBezierPath *stroke = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:RECT_CORNECR_RADIUS];
[stroke stroke];
But for some reason the corner curves look thinker than the sides. If you look at the UIButton's default frame it's very uniform. A is a UIButton, B is a custom button.
Any ideas how I can make it more like the UIButton.
You are stroking the bounds of your button. This will draw your line centred over the edge the view, so half of the thickness of the line is outside the bounds and is not drawn. This is why it is full thickness in the corners. Use CGRectInset on your bounds rectangle (inset by half the thickness of your line) and stroke THAT rect.
The problem you have is probably due to antialiasing. You can try to change the antialiasing settings of CoreGraphics before drawing your beizerPath.
An easier solution is to use the CALayer of your button and its cornerRadius property. It would be easier to draw a rounded corner
If self is your custom button:
self.layer.cornerRadius = RECT_CORNER_RADIUS;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [UIColor blackColor].CGColor;
Of course don't forget to import the QuartzCore framework and import its header for this to work (#import )

Corner radius issue with UIButton

I want to add a corner radius to a UIButton. It is working fine but a problem occurs when I add image to it.
It does not round its corners with images, the image is shown in full rectangle form.
Please see the image, I have used the corner radius with red color and the output is as follow:
Please help.
Did you try to use set the masksToBounds: property? Fore example:
CALayer *layer = [myView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:8.0];
That should do the trick.
you use -
myButton.imageView.layer.cornerRadius = 5;
but make sure that your image size is exact same as button size. its working for me.
yourButton.layer.cornerRadius = 10 //this value should be half of your button's height to make a circle
yourButton.clipsToBounds = true //this clips everything outside of bounds

Resources