I've looked at a dozen SO questions on this topic, and none of the answers have worked for me. Maybe this will help get me back on the right path.
Imagine this setup:
I want to get the center coordinates of the UIButton relative to the UIView.
In other words, the UIButton center may be 215, 80 within the UITableViewCell, but relative to the UIView they should be more like 260, 165. How do I convert between the two?
Here's what I've tried:
[[self.view superview] convertPoint:button.center fromView:button]; // fail
[button convertPoint:button.center toView:self.view]; // fail
[button convertPoint:button.center toView:nil]; // fail
[button convertPoint:button.center toView:[[UIApplication sharedApplication] keyWindow]]; // fail
I could do it the hard way by looping through all of the button's superviews and adding up the x and y coordinates, but I suspect that's overkill. I just need to find the right combination of covertPoint settings. Right?
button.center is the center specified within the coordinate system of its superview, so I
assume that the following works:
CGPoint p = [button.superview convertPoint:button.center toView:self.view]
Or you compute the button's center in its own coordinate system and use that:
CGPoint buttonCenter = CGPointMake(button.bounds.origin.x + button.bounds.size.width/2,
button.bounds.origin.y + button.bounds.size.height/2);
CGPoint p = [button convertPoint:buttonCenter toView:self.view];
Swift 4+
let p = button.superview!.convert(button.center, to: self.view)
// or
let buttonCenter = CGPoint(x: button.bounds.midX, y: button.bounds.midY)
let p = button.convert(buttonCenter, to: self.view)
Swift 5.2 You need to call convert from the button, not the superview. In my case I needed width data so I converted the bounds instead of just center point. The code below works for me:
let buttonAbsoluteFrame = button.convert(button.bounds, to: self.view)
Martin answer is correct. For developers using Swift, you can get the position of an object (button, view,...) relative to the screen by using:
var p = obj.convertPoint(obj.center, toView: self.view)
println(p.x) // this prints the x coordinate of 'obj' relative to the screen
println(p.y) // this prints the y coordinate of 'obj' relative to the screen
Here is Swift 3 update of #Pablo's answer, which off course worked great in my case.
if let window = UIApplication.shared.keyWindow {
parent.convert(child.frame.origin, to: window)
}
in swift 2.2 worked for me:
var OrignTxtNomeCliente:CGPoint!
if let orign = TXT_NomeCliente.superview, let win = UIApplication.sharedApplication().keyWindow {
OrignTxtNomeCliente = orign.convertPoint(TXT_NomeCliente.frame.origin, toView: win)
}
Related
As the question says,I want to get the position of a uibutton as shown in the image and I have to show a pop up menu from that uibutton(in the tableviews view).
I went through Question1 and Question2.But these are giving me different locations.
I use the code
CGRect windowRect = [_tableView convertRect:button.frame fromView:tempCell];
temp cell is my selected cell.
I am using kxMenu for popUp menu,and I use the following code.
[KxMenu showMenuInView:_tableView
fromRect:windowRect
menuItems:menuItems];
So,I need to get the windowRect correctly.Anybody have any idea?
If you use
[_tableView convertRect:button.frame fromView:tempCell];
It will convert frame of the button wrt tempcell to button frame wrt _tableView
If you want to get the frame of button wrt the window, you should do either
[button convertRect:button.bounds toView:nil];
or
[button.superView convertRect:button.frame toView:nil];
By default, the nestedView.frame will give you the origin points respective to the immediate parent. Here is the Swift 5 version of this answer:
let frame: CGRect? = nestedView.superview?.convert(nestedView.frame, to: nil)
Objective-C version is as follows:
CGRect frame = [nestedView.superView convertRect:nestedView.frame toView:nil];
This will give you the frame of the nestedView relative to the superview's superview. Hope this helps.
I'm trying to get a UIButton to scale but remain at its original center point, and I'm getting perplexing results with CGAffineTransformScale.
Here's the function in my UIButton subclass:
-(void)shrink {
self.transform = CGAffineTransformMakeScale(0.9,0.9);
}
With this code, the button scales to the top-left corner, but when I add code to try to set the anchor point (either of the following lines), the button gets relocated off screen somewhere:
[self.layer setAnchorPoint:CGPointMake(self.frame.size.width/2, self.frame.size.height/2)];
//same result if I use bounds instead of frame
[self.layer setAnchorPoint:self.center];
Interestingly, this line causes the view to move down and to the right some distance:
[self.layer setAnchorPoint:CGPointMake(0, 0)];
Sorry, I know there are many posts on this topic already but I honestly read and tried at least a dozen and still couldn't get this to work. I'm probably just missing something incredibly simple.
The default anchor point is 0.5, 0.5. That is why setting it to 0, 0 moves the view down and to the right. Also, if you don't want the center to move, you need to re-adjust the center after scaling it.
Just readjust its position after shrinking it.
-(void)shrink {
CGPoint centerPoint = self.center;
self.transform = CGAffineTransformMakeScale(0.9,0.9);
self.center = centerPoint;
}
I'm using Xcode 5.1 and I have several UILabels that I need to swap positions. I have Auto Layout turned off. I need these labels to swap position during a swipe gesture. I'm already swapping the position of two UIContainer objects, you'll see that in the code, and I need the labels to travel along with the container. Here's my code:
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
//Get the center of each view container
CGPoint targetHomeCenter = _vistingTeamContainer.center;
CGPoint targetVisitorCenter = _homeTeamContainer.center;
CGPoint targetHomeNameCenter = _visitingTeamName.center;
CGPoint targetVisitorNameCenter = _homeTeamName.center;
//Get the center of each past score label
CGPoint targetHomeGame1 = self.visitGame1.center;
CGPoint targetHomeGame2 = self.visitGame2.center;
CGPoint targetHomeGame3 = self.visitGame3.center;
CGPoint targetVisitGame1 = self.homeGame1.center;
CGPoint targetVisitGame2 = self.homeGame2.center;
CGPoint targetVisitGame3 = self.homeGame3.center;
//Create the animation and swap positions of the controllers
[UIView animateWithDuration:0.7f
delay:0.0f
usingSpringWithDamping:0.7f
initialSpringVelocity:0.5f
options:UIViewAnimationOptionCurveEaseInOut
animations:^(){
//Move the containers
_homeTeamContainer.center = targetHomeCenter;
_homeTeamName.center = targetHomeNameCenter;
_vistingTeamContainer.center = targetVisitorCenter;
_visitingTeamName.center = targetVisitorNameCenter;
//Now move the past scores
self.homeGame1.center = targetVisitGame1;
self.homeGame2.center = targetVisitGame2;
self.homeGame3.center = targetVisitGame3;
self.visitGame1.center = targetHomeGame1;
self.visitGame2.center = targetHomeGame2;
self.visitGame3.center = targetHomeGame3;
}
completion:NULL];
};
The UIContainers move just fine, as expected. The UILabels don't move at all.
I have also tried CGPoint targetHomeGame1 = _visitGame1.center but that doesn't do anything either. Any help would be appreciated. Thanks!
CGPoint targetVisitGame1 = _homeGame1.center;
self.homeGame1.center = targetVisitGame1;
Why would this want to move? There is no change in point's coordinates.
I really love the way foursquare designed venue detail view. Especially the map with venue location in the "header" of view ... How was it done? Details are obviously some uiscrollview (maybe uitableview?) and behind it (in the header) there is a map so when you scroll up the map is beeing uncovered as the scroll view bounces... does anyone has an idea how to do this?
Here's the way I manage to reproduce it:-
You need a UIViewController with a UIScrollView as its view. Then, the content of the UIView you add to your scrollview should look like this :-
- The frame of the MKMapView have a negative y position. In this case, we can only see 100pts of the maps in the default state (before dragging).
- You need to disable zooming and scrolling on your MKMapView instance.
Then, the trick is to move down the centerCoordinate of the MKMapView when you drag down, and adjust its center position.
For that, we compute how much 1point represent as a delta latitude so that we know how much the center coordinate of the map should be moved when being dragged of x points on the screen :-
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView* scrollView = (UIScrollView*)self.view;
[scrollView addSubview:contentView];
scrollView.contentSize = contentView.frame.size;
scrollView.delegate = self;
center = CLLocationCoordinate2DMake(43.6010, 7.0774);
mapView.region = MKCoordinateRegionMakeWithDistance(center, 1000, 1000);
mapView.centerCoordinate = center;
//We compute how much latitude represent 1point.
//so that we know how much the center coordinate of the map should be moved
//when being dragged.
CLLocationCoordinate2D referencePosition = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
CLLocationCoordinate2D referencePosition2 = [mapView convertPoint:CGPointMake(0, 100) toCoordinateFromView:mapView];
deltaLatFor1px = (referencePosition2.latitude - referencePosition.latitude)/100;
}
Once those properties are initialized, we need to implement the behavior of the UIScrollViewDelegate. When we drag, we convert the move expressed in points to a latitude. And then, we move the center of the map using the half of this value.
- (void)scrollViewDidScroll:(UIScrollView *)theScrollView {
CGFloat y = theScrollView.contentOffset.y;
// did we drag ?
if (y<0) {
//we moved y pixels down, how much latitude is that ?
double deltaLat = y*deltaLatFor1px;
//Move the center coordinate accordingly
CLLocationCoordinate2D newCenter = CLLocationCoordinate2DMake(center.latitude-deltaLat/2, center.longitude);
mapView.centerCoordinate = newCenter;
}
}
You get the same behavior as the foursquare app (but better: in the foursquare app, the maps recenter tends to jump, here, changing the center is done smoothly).
The example above is nice. If you need more help, I think they're using something very similar to RBParallaxTableViewController. https://github.com/Rheeseyb/RBParallaxTableViewController
It's essentially the same effect that Path uses for its header photo.
Yonel's answer is nice, but I found a problem as I have a pin at the center of the map. Because the negative Y, the point is hidden under my UINavigationBar.
Then, I didn't set the Negative Y, and I correct my mapView.frame according the scroll offset.
My mapView is 320 x 160
_mapView.frame = CGRectMake(0, 160, 320, -160+y);
Hope this helps someone.
I have a UIImageView that I rotate around its center:
imageHorizon.layer.anchorPoint = CGPointMake(0.5, 0.5);
imageHorizon.transform = CGAffineTransformRotate(imageHorizon.transform, angleToRotate*(CGFloat)(M_PI/180));
Sometimes I also move this image to the left or right and then rotate again. I would like to keep the rotation center all the time on the same point (which is actually the center of the super view). How can I do that ?
cheers,
self.imgView.layer.anchorPoint = CGPointMake(0.0,1.0);
self.imgView.layer.position = CGPointMake(100,200.0);
CGAffineTransform cgaRotateHr = CGAffineTransformMakeRotation(-(3.141/4));
[self.imgView setTransform:cgaRotateHr];
This is an older question, but the other solutions did not work well for me, so I came up with another solution:
Rotating an image is essentially just a normal rotation with a translation applied, ensuring that the point you want to rotate around is still in the same spot after the rotation. To do this, calculate the position's CGPoint in your image before the rotation, get the position after the rotation, and apply the difference as a translation on the image, "snapping" it into the right position. Here is the code that I've been using:
Keep in mind that the translation should be applied via CGAffineTransform, not moving the .center, because the translation will need to be relative to the rotation, and CGAffineTransformTranslate() takes care of that.
// Note: self is the superview of _imageView
// Get the rotation point
CGPoint rotationPointInSelf = self.center; // or whatever point you want to rotate around
CGPoint rotationPointInImage = [_imageView convertPoint:rotationPointInSelf fromView:self];
// Rotate the image
_imageView.transform = CGAffineTransformRotate(_imageView.transform, angle);
// Get the new location of the rotation point
CGPoint newRotationPointInImage = [_imageView convertPoint:rotationPointInSelf fromView:self];
// Calculate the difference between the point's old position and its new one
CGPoint translation = CGPointMake(rotationPointInImage.x - newRotationPointInImage.x, rotationPointInImage.y - newRotationPointInImage.y);
// Move the image so the point is back in it's old location
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, -translation.x, -translation.y);
You can make the image a subview of another view and then rotate the superview to get that effect. Another approach is to set the anchorPoint property as described in the docs.
I'm using this code to rotate around the point (0,0).
Maybe it help you figure out how to active what you want.
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
CGRect frame_smallView = CGRectMake(-width, -height, width, height);
UIView *smallView = [[UIView alloc] initWithFrame:frame_smallView];
smallView.backgroundColor = darkGrayColor;
// Select x and y between 0.0-1.0.
// The default is (0.5f,0.5f) that is the center of the layer
// (1.0f,1.0f) is the right bottom corner
smallView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);
// Rotate around this point
smallView.layer.position = CGPointMake(0, 0);
[self.view insertSubview:smallView belowSubview:self.navBar];
[UIView animateWithDuration:1
animations:^{
smallView.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
[self.navigationController popViewControllerAnimated:NO];
}];