旋转MKAnnotationView的显示图像

时间:2012-11-30 20:35:24

标签: ios ios6 mkmapview mkannotationview cgaffinetransform

我目前正在使用CGAffineTransformMakeRotation旋转MKAnnotationView的图像属性。但是,在这种情况下,整个视图都会旋转,以及标注气泡。所以我按照MKAnnotationView Only Rotate the Image property中的建议,现在正在创建一个UIImageView并将其作为子视图添加到MKAnnotationView。图像显示和旋转现在没有问题。但是,现在MKAnnotationView不响应触摸事件。我需要它像以前一样表现 - 点击/触摸它应该显示标注气泡。任何想法我需要做什么?

我尝试的初始方式(旋转标注气泡):

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
aView.image = [UIImage imageNamed:@"mapPin.png"];
float newRad = degreesToRadians(degreesToRotate);
[aView setTransform:CGAffineTransformRotate(CGAffineTransformIdentity, newRad)];

使用UIImageView并添加为子视图:

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]];
iv.userInteractionEnabled = YES;
[aView addSubview:iv];
aView.canShowCallout = YES;

更新 我试图添加一个手势识别器,虽然它不起作用。当我点击地图上MKAnnotationView中的UIImageView时,不会调用imageTapped方法。这是在方法内: - (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id)annotation

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];

UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]];
iv.userInteractionEnabled = YES;
iv.exclusiveTouch = NO;

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[iv addGestureRecognizer:tapGesture];

[aView addSubview:iv];

5 个答案:

答案 0 :(得分:2)

我在这里尝试了所有的答案,但没有一个完全奏效,但我发现了一个不同的解决方案!

我认为最简单的方法是在设置UIImage的{​​{1}}属性之前轮换image。如果你有数百和数百个注释,这可能表现不佳,但如果你只需要旋转一些东西(比如我的情况),它就可以正常工作。

我找到了一个帮助类来旋转UIImages,然后在设置MKAnnotationView的{​​{1}}属性之前用它来旋转UIImage。

助手课程在这里:http://www.catamount.com/forums/viewtopic.php?f=21&t=967

现在,要在地图上旋转MKAnnotationView的UIImage属性而不牺牲标注气泡,请执行以下操作:

image

答案 1 :(得分:2)

我意识到这是一个非常古老的问题,但我最近遇到了这个问题,它有一个非常简单的解决方案。使用图像视图时注释视图变得不可点击的唯一原因是,当没有设置图像时,MKAnnotationView的大小为(0,0)。图像视图是可见的,因为注释视图不会剪切到边界。我所要做的就是在注释视图上设置框架,使其再次可以点击。

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation  reuseIdentifier:@"Pin"];
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]];
[aView addSubview:iv];
annotationView.frame = (CGRect) {
            .origin = CGPointZero,
            .size = iv.frame.size
        };
aView.canShowCallout = YES;

答案 2 :(得分:0)

尝试对图像视图执行类似操作,以查看是否可以将其触摸事件传递给注释。

//Do this before you add the imageview as a subview
self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[self.imageView addGestureRecognizer:tapGesture];



- (void) imageTapped:(UITapGestureRecognizer *)tapGesture {
    MKAnnotationView * annotationView =  (MKAnnotationView *)tapGesture.view.superview;
    if (annotationView.selected) {
        [annotationView setSelected:NO animated:YES];
    } else {
        [annotationView setSelected:YES animated:YES];
    }
}

答案 3 :(得分:0)

您可以使用以下Swift扩展方法来提供帮助。最受欢迎的答案会导致使用链接的帮助程序库模糊图像。

extension UIImage {

    func rotated(degrees degrees : Int) -> UIImage {
        // calculate the size of the rotated view's containing box for our drawing space
        let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height))
        let rotation = CGFloat(degrees) * CGFloat(M_PI) / 180
        let t = CGAffineTransformMakeRotation(rotation);
        rotatedViewBox.transform = t;
        let rotatedSize = rotatedViewBox.frame.size;
        UIGraphicsBeginImageContextWithOptions(rotatedSize, false, 0.0);
        let context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, rotatedSize.width/2, rotatedSize.height/2);
        CGContextRotateCTM(context, rotation);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), self.CGImage);
        let rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return rotatedImage;
    }
}

用法:

annotationView.image = UIImage(named: "my_image")!.rotated(degrees: 45)

答案 4 :(得分:-2)

//只需在添加子视图之前设置图像属性,它就像魅力一样

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]];

aView.image = [UIImage imageNamed:@"mapPin.png"];   //   just this line

[aView addSubview:iv];
aView.canShowCallout = YES;