0

我正在尝试围绕圆圈旋转矩形。到目前为止,在整理了我在各个地方找到的一些代码(主要在这里: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

覆盖选择视图.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];


    }


}

这是我要实现的目标的草图:

在此处输入图像描述

4

1 回答 1

-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 文档中关于变换矩阵的章节。

此代码仅使用文本编辑器编写,因此函数名称可能略有不同。

于 2013-02-08T01:17:51.773 回答