1

我正在寻找将子视图添加到视图时触发的某种委托方法。我需要这个,因为 iOS7 地图上的指南针视图在它开始旋转时被添加到地图视图中(它以前不存在并且未隐藏)。但是,当我尝试在其中操作指南针视图时,regionWillChangeAnimated它不会触发,直到用户放开地图。

基本上我需要任何类似的东西: subviewWasAdded,,subviewsDidChange或类似的东西。

编辑:在接受 Daij-Djan 的建议后,我制作了 MPOMapView.m 和 MPOMapView。

#import "MPOMapView.h"

@implementation MPOMapView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)addSubview:(UIView *)view {
    [super addSubview:view];

    [self moveCompass:&view];
}


- (void)moveCompass:(UIView **)view {

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
        return;
    }

    if(![*view isKindOfClass:NSClassFromString(@"MKCompassView")]) {
        return;
    }

    //Gets here
    NSLog(@"Was the compass");

    float width = (*view).frame.size.width;
    float height = (*view).frame.size.height;

    (*view).frame = CGRectMake(40, self.frame.size.height - 50, width, height);

    //Frame doesn't change
    NSLog(@"Frame should change");
}

@end

该方法被调用,但框架不会改变。我也试过:

- (void)addSubview:(UIView *)view {
    [super addSubview:view];

    [self moveCompass:view];
}


- (void)moveCompass:(UIView *)view {

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
        return;
    }

    if(![view isKindOfClass:NSClassFromString(@"MKCompassView")]) {
        return;
    }

    //Gets here
    NSLog(@"Was the compass");

    float width = view.frame.size.width;
    float height = view.frame.size.height;

    view.frame = CGRectMake(40, self.frame.size.height - 50, width, height);

    //Frame doesn't change
    NSLog(@"Frame should change");
}

但这也不起作用。最后....

- (void)addSubview:(UIView *)view {
    [super addSubview:[self movedCompass:view]];
}


- (UIView *)movedCompass:(UIView *)view {

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
        return view;
    }

    if(![view isKindOfClass:NSClassFromString(@"MKCompassView")]) {
        return view;
    }

    //Gets here
    NSLog(@"Was the compass");

    float width = view.frame.size.width;
    float height = view.frame.size.height;

    view.frame = CGRectMake(40, self.frame.size.height - 50, width, height);

    //Frame doesn't change
    NSLog(@"Frame should change");

    return view;
}

也不起作用

4

3 回答 3

2

Daij-Djan 发布了正确答案,感谢他!

这只是对我觉得代码太多的改进:

- (void)layoutSubviews 
{
    [super layoutSubviews];

    for(id view in self.subviews) 
    {
        if([view isKindOfClass:NSClassFromString(@"MKCompassView")]) 
            [self moveCompass:view]];
    }
}

- (void)moveCompass:(UIView *)view 
{
    //change compass frame as shown by Daij-Djan
}
于 2013-12-04T07:55:35.083 回答
2

没有这样的代表。

您所能做的就是继承 MKMapView 并实现 insertSubview 和 addSubview 并构建您自己的解决方案。

看到更好的方法

- (void)layoutSubviews {
    [super layoutSubviews];

                if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
                     return;
                }

    for(id view in self.subviews) {
                if(![view isKindOfClass:NSClassFromString(@"MKCompassView")]) {
                     continue;
                }

                [self moveCompass:view]];
    }
}

- (void)moveCompass:(UIView *)view {
    //Gets here
    NSLog(@"Was the compass");

    float width = view.frame.size.width;
    float height = view.frame.size.height;

    //view.frame = CGRectMake(40, self.frame.size.height - 50, width, height);
    view.frame = CGRectMake(40, self.bounds.size.height - 50, width, height);

}
于 2013-10-02T20:38:12.713 回答
0

我不知道它是否会起作用,购买您可以尝试将 Key Value Observing 与camera具有heading属性的 mapview 一起使用。

于 2013-10-02T21:06:32.370 回答