-2

How to get x1 &x2 origin?

self.spinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    float x1,x2,y1,y2;
    x1=self.view.frame.origin.x;
    x2=self.view.frame.origin.x;
    y1=self.view.frame.origin.y;
    y2=self.view.frame.origin.y;



    self.spinner.center=CGPointMake((x1+x2)/2,(y1+y2)/2);

     [self.spinner startAnimating];
     [self.view addSubview:self.spinner];

i have a spinner,i try to place a spinner in mid point ,i know the concept but how to get that x1&x2 origin really i have confused with this...

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47

3 Answers3

1

I think you are not making point by

float x1,x2,y1,y2;
x1=self.view.frame.origin.x;
x2=self.view.frame.origin.x;
y1=self.view.frame.origin.y;
y2=self.view.frame.origin.y;

If you have to put indicator in the center of view,

self.spinner.center = self.view.center;
Tushar J.
  • 556
  • 4
  • 16
  • its working for me ,but how to get the origin,really i am like to know that ,i will accept your answer with in 2 minutes.. – Kishore Kumar Sep 14 '15 at 10:48
  • If you still want to get the center of the frame there is other way like, `CGFloat midX = CGRectGetMidX(self.view.frame);` OR `CGFloat midX = self.view.frame.width / 2.0;` Hope it will help you. – Tushar J. Sep 14 '15 at 10:50
  • Ohhh... sorry for last comment. I thought you want other way to calculate center. To calculate origin, you are doing right. `self.view.frame.origin.x;` is correct. But why you are taking it twice ? Its not making sense at all. Its like, 0+0/2.0 !! – Tushar J. Sep 14 '15 at 10:55
1

You have misunderstood how a rectangle is represented, your formula is based on using two corners but NSRect stores one corner, origin, and the width and height, size - see the documentation. There are also convenience macros for calculating the various properties (max coord etc.) of a rectangle - see the docs as above.

However, for this particular task you might wish to use UIView's center property, which will save you the math.

CRD
  • 52,522
  • 5
  • 70
  • 86
0
NSRect

is created with

x,y,width & height

It's also has the centre property, which is a

CGPoint

. To work it out manually,

center.x = origin.x + size.width/2.

.. And similarly for

center.y

.

bilo-io
  • 597
  • 3
  • 17