注释视图上的 CGAffineTransformMakeRotation
CGAffineTransformMakeRotation on AnnotationViews
我正在尝试通过CGAffineTransformMakeRotation
根据方向调整一些注释视图的旋转。
然而,我对该函数的测试给出了奇怪的结果:如果我应用 M_PI/2
的旋转,如代码示例中所示,它会略微向下,如果我使用 M_PI
,它会一直指向下方,在 M_PI/4
处,它指向左侧底部。如何计算旋转而不是指向以弧度为单位的输入角度?这是我使用的代码块:
BOOL oldVerticalInverted=NO;
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiandsToDegrees(x) (x * 180.0 / M_PI)
-(void)adjustAnnotationFor:(CLLocationCoordinate2D)start ToCoordinates:(CLLocationCoordinate2D)end completion:(void (^)(void))completion{
float newCoordinatesAngle=[Line angleFromCoordinate:start toCoordinate:end];
float newAngle=newCoordinatesAngle-M_PI/4;
float angle2Rotate=newAngle-self.angle;
angle2Rotate=M_PI; //testing
NSLog(@"oldAngle=%f newAngle=%f angle to rotate=%f %@", radiandsToDegrees(self.angle), radiandsToDegrees(newAngle), radiandsToDegrees(angle2Rotate), cos(self.angle)<0?@"inverted": @"straight");
self.angle=newAngle;
NSLog(@"setting angle to %f cos=%f", self.angle, cos(self.angle));
if (view!=nil){
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:.5 animations:^{
BOOL verticallyInverted=cos(self.angle)<0;
int invertVertical=oldVerticalInverted==verticallyInverted?1:-1;
oldVerticalInverted=verticallyInverted;
CGAffineTransform affineInvert=CGAffineTransformMakeScale(1, invertVertical);
CGAffineTransform rotateAffine=CGAffineTransformMakeRotation(angle2Rotate);
view.transform=CGAffineTransformConcat(rotateAffine, affineInvert);
}
completion :^(BOOL finished){
if (completion) completion();
}];
});
}
}
事实上,出于我忽略的原因,在动画中指定角度以使注释视图适当旋转就足够了:这是更新的代码,包括一个块,用于在指向时反转注释左边:
BOOL oldVerticalInverted=否;
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiandsToDegrees(x) (x * 180.0 / M_PI)
-(void)adjustAnnotationFor:(CLLocationCoordinate2D)start ToCoordinates:(CLLocationCoordinate2D)end completion:(void (^)(void))completion{
__block float newAngle=[Line angleFromCoordinate:start toCoordinate:end];
__block float angle2Rotate=self.angle;
__block BusAnnotation* blockSelf=self;
__block BOOL blockOldInverted=oldVerticalInverted;
NSLog(@"setto angle a %f cos=%f", self.angle, cos(self.angle));
if (view!=nil){
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1 animations:^{
blockSelf.angle=newAngle;
BOOL verticallyInverted=cos(blockSelf.angle)<0;
int invertVertical=blockOldInverted==verticallyInverted?1:-1;
blockOldInverted=verticallyInverted;
CGAffineTransform affineInvert=CGAffineTransformMakeScale(1, invertVertical);
view.transform=CGAffineTransformConcat(CGAffineTransformIdentity, affineInvert);
}
completion :^(BOOL finished){
if (completion) completion();
}];
});
}
}
唯一剩下的问题是如何反转我在 annotationView 中刻录的文本,当指向左侧时该文本会反转:这就是我制作后一部分的方式:
-(UIImage *)burnText:(NSString *)text ofBackColor:(UIColor*)color intoImage:(UIImage *)img inRect:(CGRect)aRect font:(UIFont*) font{
UIGraphicsBeginImageContextWithOptions(img.size,NO,0.0);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByTruncatingTail;
textStyle.alignment=NSTextAlignmentCenter;
NSDictionary *dictionary = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: textStyle,
NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: color};
[text drawInRect:aRect withAttributes:dictionary];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
theImage.accessibilityValue=text;
return theImage;
}
我正在尝试通过CGAffineTransformMakeRotation
根据方向调整一些注释视图的旋转。
然而,我对该函数的测试给出了奇怪的结果:如果我应用 M_PI/2
的旋转,如代码示例中所示,它会略微向下,如果我使用 M_PI
,它会一直指向下方,在 M_PI/4
处,它指向左侧底部。如何计算旋转而不是指向以弧度为单位的输入角度?这是我使用的代码块:
BOOL oldVerticalInverted=NO;
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiandsToDegrees(x) (x * 180.0 / M_PI)
-(void)adjustAnnotationFor:(CLLocationCoordinate2D)start ToCoordinates:(CLLocationCoordinate2D)end completion:(void (^)(void))completion{
float newCoordinatesAngle=[Line angleFromCoordinate:start toCoordinate:end];
float newAngle=newCoordinatesAngle-M_PI/4;
float angle2Rotate=newAngle-self.angle;
angle2Rotate=M_PI; //testing
NSLog(@"oldAngle=%f newAngle=%f angle to rotate=%f %@", radiandsToDegrees(self.angle), radiandsToDegrees(newAngle), radiandsToDegrees(angle2Rotate), cos(self.angle)<0?@"inverted": @"straight");
self.angle=newAngle;
NSLog(@"setting angle to %f cos=%f", self.angle, cos(self.angle));
if (view!=nil){
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:.5 animations:^{
BOOL verticallyInverted=cos(self.angle)<0;
int invertVertical=oldVerticalInverted==verticallyInverted?1:-1;
oldVerticalInverted=verticallyInverted;
CGAffineTransform affineInvert=CGAffineTransformMakeScale(1, invertVertical);
CGAffineTransform rotateAffine=CGAffineTransformMakeRotation(angle2Rotate);
view.transform=CGAffineTransformConcat(rotateAffine, affineInvert);
}
completion :^(BOOL finished){
if (completion) completion();
}];
});
}
}
事实上,出于我忽略的原因,在动画中指定角度以使注释视图适当旋转就足够了:这是更新的代码,包括一个块,用于在指向时反转注释左边: BOOL oldVerticalInverted=否; #define degreesToRadians(x) (M_PI * x / 180.0) #define radiandsToDegrees(x) (x * 180.0 / M_PI)
-(void)adjustAnnotationFor:(CLLocationCoordinate2D)start ToCoordinates:(CLLocationCoordinate2D)end completion:(void (^)(void))completion{
__block float newAngle=[Line angleFromCoordinate:start toCoordinate:end];
__block float angle2Rotate=self.angle;
__block BusAnnotation* blockSelf=self;
__block BOOL blockOldInverted=oldVerticalInverted;
NSLog(@"setto angle a %f cos=%f", self.angle, cos(self.angle));
if (view!=nil){
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1 animations:^{
blockSelf.angle=newAngle;
BOOL verticallyInverted=cos(blockSelf.angle)<0;
int invertVertical=blockOldInverted==verticallyInverted?1:-1;
blockOldInverted=verticallyInverted;
CGAffineTransform affineInvert=CGAffineTransformMakeScale(1, invertVertical);
view.transform=CGAffineTransformConcat(CGAffineTransformIdentity, affineInvert);
}
completion :^(BOOL finished){
if (completion) completion();
}];
});
}
}
唯一剩下的问题是如何反转我在 annotationView 中刻录的文本,当指向左侧时该文本会反转:这就是我制作后一部分的方式:
-(UIImage *)burnText:(NSString *)text ofBackColor:(UIColor*)color intoImage:(UIImage *)img inRect:(CGRect)aRect font:(UIFont*) font{
UIGraphicsBeginImageContextWithOptions(img.size,NO,0.0);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByTruncatingTail;
textStyle.alignment=NSTextAlignmentCenter;
NSDictionary *dictionary = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: textStyle,
NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: color};
[text drawInRect:aRect withAttributes:dictionary];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
theImage.accessibilityValue=text;
return theImage;
}