0

現在、CGAffineTransformMakeRotation を使用して MKAnnotationView の画像プロパティを回転させています。ただし、この場合、吹き出しの吹き出しだけでなく、ビュー全体が回転します。そのため、 MKAnnotationView Only Rotate the Image プロパティ内の提案に従い、現在 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];
4

5 に答える 5

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;
于 2014-06-16T20:29:39.603 に答える
2

ここですべての答えを試しましたが、どれも完全に機能しませんでしたが、別の解決策を見つけました!

これを行う最も簡単な方法は、のプロパティをUIImage設定する前に、それ自体を回転させることだと思います。何百もの注釈がある場合、これはパフォーマンスが低下する可能性がありますが、(私の場合のように) いくつかのものを回転させる必要がある場合は、問題なく機能します。imageMKAnnotationView

UIImages を回転させるヘルパー クラスを見つけ、それを使用して UIImage を回転させてから、 のimageプロパティを設定しましたMKAnnotationView

ヘルパー クラスはこちら: http://www.catamount.com/forums/viewtopic.php?f=21&t=967

ここで、コールアウト バブルを犠牲にすることなく、マップ上で MKAnnotationView の UIImage プロパティを回転するには、次の手順を実行します。

- (MKAnnotationView *)mapView:(MKMapView *)pMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MyAnnotationView *annotationView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"WayPoint"]; 

    annotationView.image = [annotationView.image imageRotatedByDegrees:45.0];

    annotationView.canShowCallout = YES;

    return annotationView;
}
于 2013-08-04T19:56:39.577 に答える
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)
于 2015-12-03T19:46:05.323 に答える
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];
    }
}
于 2012-11-30T21:06:01.353 に答える