views:

48

answers:

1

Hi,

I am rotating a wheel which various subviews (UIImageViews and UIButtons) However when I ask it to display each and every centre of every subview, it is giving me the same value everytime.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {   
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];

    int len = [allTouches count]-1;

    UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
    CGPoint location = [touch locationInView:[self superview]];
    float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );

    totalRadians = theAngle;

    [self rotateImage:theAngle];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}

-(void) rotateImage:(float)angleRadians{

    self.transform = CGAffineTransformMakeRotation(angleRadians);
    CATransform3D rotatedTransform = self.layer.transform;
    self.layer.transform = rotatedTransform;    
    int count = 0;
    for (UIView *subview in self.subviews)
    {
    count++;    
        //these values are always the same
    NSLog(@"%i %f %f", count, subview.center.x, subview.center.y);
    }
}

May someone please tell me why the values are always coming the same even after being rotated and placed in a different position?

Thanks!

+1  A: 

To go off what was being said in the comments, center is expressed within the superview bounds. Bounds are not affected by rotation, {0,0} is always the upper left of the view.

To get what you want, you need to apply the rotation to the center point,

rotatedCenter = CGPointApplyAffineTransform(subview.center, self.transform);

Joshua Weinberg
thnx so this in other words will find the correct coordinates of the center according to the transformation whihc has happened?
Lily
It should. This will also work for any transformation not just the rotation.
Joshua Weinberg
wow this looks good. thanks. one last question please. if i am receiving negative numbers, is this correct? or am i doing something wrong and thus yielding an incorrect result?
Lily
negatives certainly are possible
Joshua Weinberg
but this does not get the coordinates of the object with respect to the entire page, correct? just with respect to itself
Lily