我试图通过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();
}];
});
}
}
答案 0 :(得分:0)
事实上,由于我忽略的原因,足以在动画中为注释视图指定适当旋转的角度:这是更新的代码,包括用于在向左指向时反转注释的块: 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{
__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;
}