简体   繁体   English

在圆的圆周上旋转矩形(iOS)?

[英]Rotating rectangle around circumference of a circle (iOS)?

I am trying to rotate the rectangle around the circle. 我正在尝试围绕圆旋转矩形。 So far after putting together some code I found in various places (mainly here: https://stackoverflow.com/a/4657476/861181 ) , I am able to rotate rectangle around it's center axis. 到目前为止,在将我在各个地方(主要在这里: https : //stackoverflow.com/a/4657476/861181 )上找到的一些代码组合在一起之后,我就能够围绕其中心轴旋转矩形。

How can I make it rotate around the circle? 如何使其绕圆旋转?

Here is what I have: OverlaySelectionView.h 这是我所拥有的:OverlaySelectionView.h

#import <QuartzCore/QuartzCore.h>

@interface OverlaySelectionView : UIView {
@private
    UIView* dragArea;
    CGRect dragAreaBounds;

    UIView* vectorArea;
    UITouch *currentTouch;
    CGPoint touchLocationpoint;
    CGPoint PrevioustouchLocationpoint;

}
@property    CGRect vectorBounds;
@end

OverlaySelectionView.m OverlaySelectionView.m

#import "OverlaySelectionView.h"

@interface OverlaySelectionView()
@property (nonatomic, retain) UIView* vectorArea;
@end

@implementation OverlaySelectionView

@synthesize vectorArea, vectorBounds;
@synthesize delegate;

- (void) initialize {

    self.userInteractionEnabled = YES;
    self.multipleTouchEnabled = NO;
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateVector:)];

    panRecognizer.maximumNumberOfTouches = 1;
    [self addGestureRecognizer:panRecognizer];

}

- (id) initWithCoder: (NSCoder*) coder {
    self = [super initWithCoder: coder];
    if (self != nil) {
        [self initialize];
    }
    return self;
}

- (id) initWithFrame: (CGRect) frame {
    self = [super initWithFrame: frame];
    if (self != nil) {
        [self initialize];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    if (vectorBounds.origin.x){

    UIView* area = [[UIView alloc] initWithFrame: vectorBounds];
    area.backgroundColor = [UIColor grayColor];
    area.opaque = YES;

    area.userInteractionEnabled = NO;
    vectorArea = area;
    [self addSubview: vectorArea];
    }

}


- (void)rotateVector: (UIPanGestureRecognizer *)panRecognizer{


    if (touchLocationpoint.x){

            PrevioustouchLocationpoint  = touchLocationpoint;
    }

    if ([panRecognizer numberOfTouches] >= 1){
        touchLocationpoint = [panRecognizer locationOfTouch:0 inView:self];
    }

    CGPoint origin;
    origin.x=240;
    origin.y=160;
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
    CGAffineTransform newTransform =CGAffineTransformScale(vectorArea.transform, 1, 1);
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
    CGFloat newAngle = currentRotation- previousRotation;

    newTransform = CGAffineTransformRotate(newTransform, newAngle);
    [self animateView:vectorArea toPosition:newTransform];

}


-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
    CGPoint result;
    CGFloat x = secondPoint.x-firstPoint.x;
    CGFloat y = secondPoint.y-firstPoint.y;
    result = CGPointMake(x, y);
    return result;
}


-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
    [UIView setAnimationsEnabled:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.0750];
    vectorArea.transform = newTransform;
    [UIView commitAnimations];

}



@end

here is attempt to clarify. 这是尝试澄清。 I am creating the rectangle from a coordinates on a map. 我正在根据地图上的坐标创建矩形。 Here is the function that creates that rectangle in the main view. 这是在主视图中创建该矩形的函数。 Essentially it is the middle of the screen: 本质上,它是屏幕的中间部分:

overlay is the view created with the above code. overlay是使用以上代码创建的视图。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{

    if (!circle){
    circle = [MKCircle circleWithCenterCoordinate: userLocation.coordinate radius:100];

        [mainMapView addOverlay:circle];
        CGPoint centerPoint = [mapView convertCoordinate:userLocation.coordinate toPointToView:self.view];

        CGPoint upPoint = CGPointMake(centerPoint.x, centerPoint.y - 100);

        overlay = [[OverlaySelectionView alloc] initWithFrame: self.view.frame];

        overlay.vectorBounds = CGRectMake(upPoint.x, upPoint.y, 30, 100);

        [self.view addSubview: overlay];


    }


}

Here is the sketch of what I am trying to achieve: 这是我要实现的目标的草图:

在此处输入图片说明

Introduction 介绍
A rotation is always done around (0,0). 始终围绕(0,0)进行旋转。
What you already know: 您已经知道的内容:
To rotate around the center of the rectangle you translate the rect to origin, rotate and translate back. 要绕矩形的中心,你翻译RECT原点,旋转和平移回来。

Now for your question: 现在为您的问题:
to rotate around a center point of a circle, simply move the center of the rectangle such that the circle is at (0,0) then rotate, and move back. 若要绕圆的中心点旋转,只需移动矩形的中心,使圆位于(0,0),然后旋转,然后向后移动。

start positioning the rectangle at 12 o clock, with the center line at 12. 开始将矩形定位在12点,中心线在12点。

1) as explained you always rotate around 0,0, so move the center of the circle to 0,0 1)说明你总是绕0,0,所以圆的中心移动到0,0

  CGAffineTransform trans1 =  CGAffineTransformTranslation(-circ.x, -circ.y);

2) rotate by angle 2)旋转角度

CGAffineTransform transRot =  CGAffineTransformRotation(angle); // or -angle try out.

3) Move back 3)向后移动

CGAffineTransform transBack = CGAffineTransformTranslation(circ.x, circ.y);

Concat these 3 rotation matrices to one combibed matrix, and apply it to the rectangle. 的concat这些3个旋转矩阵,以一个矩阵combibed,并将其应用到该矩形。

CGAffineTransformation tCombo = CGAffineTransformConcat(trans1, transRot);
tCombo = CGTransformationConcat(tCombo, transback);

Apply 应用

rectangle.transform = tCombo;

You probably should also read the chapter about Transformation matrices in Quartz docu. 您可能还应该阅读有关Quartz docu中的转换矩阵的章节。

This code is written with a text editor only, so expect slighly different function names. 此代码仅使用文本编辑器编写,因此期望的函数名称略有不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM