开发者

Coordinates won't change when rotated Objective c

开发者 https://www.devze.com 2023-01-21 05:06 出处:网络
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.

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.

-(开发者_如何学Cvoid) 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!


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);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号