I have 4 views and i am drawing circles inside these views.The user is able to move these views.How can i get the position of each view?
If your views are instance variables in your class you can use view.frame or view.center but your question is quite low on details so I'm just shooting in the dark.
CGPoint origin = view.origin;
CGPoint center = view.center;
CGRect frame = view.frame;
All these things can be used to get what you want.
Related
Hi in one of my application i had created a circle using drawrect method on UIView object.Now my concern i want draw a highlight border around the circle for that actually i used
myView.layer.borderWidth =3.0;
myView.layer.borderColor=[UIColor colorWithRed:myView.patternRed green:myView.patternGreen blue:myView.patternBlue alpha:1.0].CGColor;
But due to this code what is happening is a border is creating around the view and it's looks a rectangle, but i want to create a border around the circle itself. So if anyone know how to implement this functionality please let me know. Thanks in advance.
Try this
myView.layer.cornerRadius = 80.0f;
It'll curve your view into a circle.
Thanks.
you have to set some radius for the corners of that view so add this line
myView.layer.cornerRadius=20;
play with the numeric value to match your requirements
I hope it helps
This Works for me:
UIView *myView =[[UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];
[self createRoundUIView:myView:80];
-(void)createRoundUIView:(UIView *)inputView sizeDiameter:(float)diameterSize;
{
CGPoint saveCenter = inputView.center;
CGRect frame = CGRectMake(inputView.frame.origin.x, inputView.frame.origin.y, diameterSize, diameterSize);
roundedView.frame = frame;
roundedView.layer.cornerRadius = diameterSize / 2.0;
roundedView.center = saveCenter;
}
I have a custom UIView class which I add a UIScrollView on top of. What i am trying to do is add a gradient to the UIView's layer so that is visible above the scroll view. Currently i am doing it this way:
[self.layer insertSublayer:self.leftShadowLayer atIndex:[self.layer.sublayers count]];
[self.layer insertSublayer:self.rightShadowLayer atIndex:[self.layer.sublayers count]];
but this seems a little hackish and prone to issues for some reason. Is there any other preferred way of adding a sublayer is the top layer in a view regardless of what else is added to the view's layers?
Thx
Draw the gradient using CGGradientCreateWithColorComponents() or CGGradientCreateWithColors().
The documentation lies at-
CGGradientRef guide
See the sample code to draw a CALayer, which u can add to a UIView's layer property.
-(void)drawInContext:(CGContextRef)theContext
{
// self.opacity=.5;
CGMutablePathRef thePath = CGPathCreateMutable();
//CGPathMoveToPoint(thePath,NULL,100.0f,200.f);
CGPoint aOnePoint=CGPointMake(cgpoint.x,tipPoint.y);
CGPoint aTwoPoint=CGPointMake(cgpoint.x+5,460);
CGPoint aThreePoint=CGPointMake(320,100);
CGPoint aFourPoint=CGPointMake(320,tipPoint.y-25);
CGPoint points[]={aOnePoint,aTwoPoint,aFourPoint};
CGPathAddLines(thePath, NULL,points,3);
CGContextBeginPath(theContext);
CGContextAddPath(theContext, thePath);
//CAGradientLayer here...using the CGCreateGradient methods.
CGContextSetLineWidth(theContext,2.0f);
CGSize theShadowSize = CGSizeMake(4.0f,4.0f);
CGContextSetShadowWithColor(theContext, theShadowSize,3,[UIColor darkGrayColor].CGColor);
CGContextSetFillColorWithColor(theContext,[UIColor redColor].CGColor);
CGContextFillPath(theContext);
CFRelease(thePath);
}
this way you can draw your own layer and add it to .layer property of a view.
Notice that we have to override the - (void)drawInContext:(CGContextRef)theContext to have a custom layer. May need to call
Hope that helps..
I have 4 views and i am drawing circles inside these views.The user is able to move these views.How can i get the position of each view relative to the window(i mean relative to 320*480)?
I want to draw and fill a polygon using the position of views.
You can use the frame property of the UIView to retrieve its location and size. See the class reference for more information:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
Example:
... = myView.frame.origin.x; //x-coord
... = myView.frame.origin.y; //y-coord
... = myView.frame.size.width; //width
... = myView.frame.size.height; //height
You can grab the position in the following way:
CGPoint positionOfAView = view.frame.origin;
or if transforms were applied:
CGPoint positionOFAView = view.bounds.origin;
Alternatively, you may want to grab the center:
CGPoint centerOfAView = view.center;
See this answer, too.
You have posted the same question again.(your first question) Before anybody answer's this question the same way it was answered earlier. Can you please tell,
Are you drawing the Circles in the view using CoreGraphics?
When you say 4 different views, how they are managed? Are they displayed at the same time?
Are there 4 View objects added on 1 ViewController?
You can get the position using
CGRect frame = [myView frame];
But remember it will send coordinates and origin based on its parent view
And after making changes
myView.frame = frame;
Hope this helps....
If you want to just move a view (without changing it size), consider using the center property, it might be more convenient.
Here's an example, assuming your circle structure is built in a certain way :)
myView.center = CGPointMake(circle.x + circle.width/2, circle.y + circle.height/2);
I'd like to move a view from a scrollview to a uiview.
I'm having trouble changing it's center(or frame) so that it remains in the same position in screen (but in a different view, possibly the superview of scrollview).
How should I convert the view's center/frame?
Thank you.
EDIT:
CGPoint oldCenter = dragView.center;
CGPoint newCenter = [dragView convertPoint: oldCenter toView: self.navigationView.contentView];
dragView.center = newCenter;
[self.navigationView.contentView addSubview: dragView];
I can also use (NSSet*) touches since i'm in touchesBegan:
I was having hard time to make it work but the doc wasn't so clear to me.
You can use convertPoint:toView: method of UIView. It is used to convert a point from one view's coordinate system to another. See Converting Between View Coordinate Systems section of UIView class reference. There are more methods available.
-edit-
You are using the wrong point when calling convertPoint: method. The given point should be in dragView's coordinate system where as dragView.center is in its superview's coordinate system.
Use the following point and it should give you the center of dragView in its own coordinate system.
CGPoint p;
p = CGPointMake(dragView.bounds.size.width * 0.5, dragView.bounds.size.height * 0.5);
My goal is simple; I want to create a program that displays an UIImage, and when swiped from bottom to top, displays another UIImage. The images here could be a happy face/sad face. The sad face should be the starting point, the happy face the end point. When swiping your finger the part below the finger should be showing the happy face.
So far I tried solving this with the frame and bounds properties of the UIImageview I used for the happy face image.
What this piece of code does is wrong, because the transition starts in the center of the screen and not the bottom. Notice that the origin of both frame and bounds are at 0,0...
I have read numerous pages about frames and bounds, but I don't get it. Any help is appreciated!
The loadimages is called only once.
- (void)loadImages {
sadface = [UIImage imageNamed:#"face-sad.jpg"];
happyface = [UIImage imageNamed:#"face-happy.jpg"];
UIImageView *face1view = [[UIImageView alloc]init];
face1view.image = sadface;
[self.view addSubview:face1view];
CGRect frame;
CGRect contentRect = self.view.frame;
frame = CGRectMake(0, 0, contentRect.size.width, contentRect.size.height);
face1view.frame = frame;
face2view = [[UIImageView alloc]init];
face2view.layer.masksToBounds = YES;
face2view.contentMode = UIViewContentModeScaleAspectFill;
face2view.image = happyface;
[self.view addSubview:face2view];
frame = CGRectMake(startpoint.x, 0, contentRect.size.width, contentRect.size.height);
face2view.frame = frame;
face2view.clipsToBounds = YES;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint movepoint = [[touches anyObject] locationInView: self.view];
NSLog(#"movepoint: %f %f", movepoint.x, movepoint.y);
face2view.bounds = CGRectMake(0, 0, 320, 480 - movepoint.y);
}
The UIImages and UIImageViews are properly disposed of in the dealloc function.
Indeed, you seem to be confused about frames and bounds. In fact, they are easy. Always remember that any view has its own coordinate system. The frame, center and transform properties are expressed in superview's coordinates, while the bounds is expressed in the view's own coordinate system. If a view doesn't have a superview (not installed into a view hierarchy yet), it still has a frame. In iOS the frame property is calculated from the view's bounds, center and transform. You may ask what the hell frame and center mean when there's no superview. They are used when you add the view to another view, allowing to position the view before it's actually visible.
The most common example when a view's bounds differ from its frame is when it is not in the upper left corner of its superview: its bounds.origin may be CGPointZero, while its frame.origin is not. Another classic example is UIScrollView, which frequently modifies its bounds.origin to make subviews scroll (in fact, modifying the origin of the coordinate system automatically moves every subview without affecting their frames), while its own frame is constant.
Back to your code. First of all, when you already have images to display in image views, it makes sense to init the views with their images:
UIImageView *face1view = [[UIImageView alloc] initWithImage: sadface];
That helps the image view to immediately size itself properly. It is not recommended to init views with -init because that might skip some important code in their designated initializer, -initWithFrame:.
Since you add face1view to self.view, you should really use its bounds rather than its frame:
face1view.frame = self.view.bounds;
Same goes for the happier face. Then in -touchesMoved:… you should either change face2view's frame to move it inside self.view or (if self.view does not contain any other subviews besides faces) modify self.view's bounds to move both faces inside it together. Instead, you do something weird like vertically stretching the happy face inside face2view. If you want the happy face to slide from the bottom of self.view, you should initially set its frame like this (not visible initially):
face2view.frame = CGRectOffset(face2view.frame, 0, CGRectGetHeight(self.view.bounds));
If you choose to swap faces by changing image views' frames (contrasted with changing self.view's bounds), I guess you might want to change both the views' frame origins, so that the sad face slides up out and the happy face slides up in. Alternatively, if you want the happy face to cover the sad one:
face2view.frame = face1view.frame;
Your problem seems to have something to do with the face2view.bounds in touchesMoved.
You are setting the bounds of this view to the rect, x:0, y:0, width:320, height:480 - y
x = 0 == left on the x axis
y = 0 == top on the y axis
So you are putting this image frame at the upper left corner, and making it fill the whole view. That's not what you want. The image simply becomes centered in this imageView.