2013-10-02 48 views
1

我正在尋找某種委託方法,將子視圖添加到視圖時會觸發。我需要這個,因爲iOS7地圖上的指南針視圖在開始旋轉時添加到mapview中(它以前不存在且未隱藏)。但是,當我嘗試操作regionWillChangeAnimated中的指南針視圖時,它不會觸發,直到用戶放開地圖。尋找子視圖ChangeChange委託方法

基本上我需要沿線的任何東西: 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; 
} 

無法正常工作或

+0

@Mike_V沒有你設法讓你的應用接受與您的代碼訪問MKCompassView私有類呀?我需要爲公共應用程序做同樣的事情。 – pnizzle

回答

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); 

} 
+0

我會試試這個,並接受答案,如果最終爲我們鍛鍊。謝謝! –

+0

請參閱我的編輯 –

+0

啊我看你想做什麼:)請保存一個指向addSubview中的視圖並實現layoutViews ...我編輯我的代碼在一秒鐘 –

0

我不知道這是否會工作,買你可以嘗試使用密鑰值與MapView類的camera,其中有一個heading特性觀察。

+0

試過'[self.mapView.camera addObserver:self forKeyPath:@「heading」options:NSKeyValueObservingOptionNew | NSKey()* NSKey()* NSKey()* NSKey()* NSKey()* NSKey(){NSKeyValueObservingOptionOld context:nil];''和' - (void)observeValueForKeyPath:(NSString *)keyPath ofObject: @「,keyPath); }'但沒有打印 –

+0

謝謝你的嘗試。太糟糕了,它沒有奏效,但很高興知道地圖相機上沒有KVO。 –

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 
}