围绕圆周(iOS)旋转矩形?

时间:2013-02-08 01:04:11

标签: ios uiview geometry

我正在尝试围绕圆圈旋转矩形。到目前为止,我在各个地方(主要是https://stackoverflow.com/a/4657476/861181)汇总了一些代码后,我能够围绕它的中心轴旋转矩形。

如何让它围绕圆圈旋转?

以下是我所拥有的: 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

#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

这里是试图澄清。我正在从地图上的坐标创建矩形。这是在主视图中创建该矩形的函数。基本上它是屏幕的中间位置:

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


    }


}

以下是我想要实现的草图:

enter image description here

1 个答案:

答案 0 :(得分:-1)

<强>简介
旋转始终围绕(0,0)进行 你已经知道的:
要围绕矩形的中心旋转,请将矩形转换为原点,旋转并平移回来。

现在提出您的问题:
要围绕圆的中心点旋转,只需移动矩形的中心,使圆圈处于(0,0)然后旋转,然后向后移动。

开始将矩形定位在12点,中心线为12.

1)如上所述,你总是围绕0,0旋转,所以将圆心移动到0,0

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

2)按角度旋转

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

3)后退

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

将这3个旋转矩阵连接到一个组合矩阵,并将其应用于矩形。

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

应用

rectangle.transform = tCombo;

您可能还应该阅读Quartz docu中关于转换矩阵的章节。

此代码仅使用文本编辑器编写,因此期望功能名称略有不同。