2009-06-27 70 views
11

我有一個UIScrollView它有一個UIImageView。我想在此imageView上顯示引腳。當我將引腳添加爲ImageView的子視圖時,除了縮放引腳上的縮放變換時,一切都非常棒。我不希望這種行爲,並希望我的針腳保持不變。UIScrollView ImageView頂部針腳

所以我選擇將引腳添加到位於ImageView頂部的另一個視圖,並且也是UIScrollView的子視圖。這裏的想法,如果你會想象的是有一個圖層懸停在地圖上,不會縮放,但顯示在我繪製它們的地方。

如果ImageView縮放,則添加到圖層視圖時的引腳不會進行縮放。然而,問題隨後變成了針腳的位置與原始原點x/y不匹配,因爲ImageView已經進行了比例變換。

基本上這是一個帶有引腳的地方的自定義地圖。我正在嘗試讓引腳浮起來,而不是放大和縮小我的ImageView,但記住放大時發生的位置。

一些代碼:

scrollView = [[UIScrollView alloc] initWithFrame:viewRect]; 

scrollView.delegate = self; 
scrollView.pagingEnabled = NO; 
scrollView.scrollsToTop = NO; 
[scrollView setBackgroundColor:[UIColor clearColor]]; 
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview 
scrollView.bounces = YES; 
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; 

imageViewMap = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]]; 

imageViewMap.userInteractionEnabled = YES; 

viewRect = CGRectMake(0,0,imageViewMap.image.size.width,imageViewMap.image.size.height); 

//viewRect = CGRectMake(0,0,2976,3928); 

[scrollView addSubview:imageViewMap]; 

[scrollView setContentSize:CGSizeMake(viewRect.size.width, viewRect.size.height)]; 

iconsView = [[UIView alloc] initWithFrame:imageViewMap.frame]; 

[scrollView addSubview:iconsView]; 

代碼稍後添加引腳上的一些事件。

[iconsView addSubview:pinIcon]; 

我被困在試圖TP找出如何讓我的腳在地圖上懸停不動,當規模發生了。

回答

9

我喜歡將所有引腳保留在專用於引腳(iconsView)的視圖中的想法,但我決定將它們添加爲您稱爲imageViewMap的子視圖。

將QuartzCore.framework文件導入到您的項目中。

調用你的視圖控制器MyViewController。

#import <QuartzCore/QuartzCore.h> 
@interface MyViewController : UIViewController <UIScrollViewDelegate> 
@property (retain) UIScrollView *scrollView; 
@property (retain) UIImageView *imageView; 

//等

@implementation MyViewController 

@synthesize scrollView; 
@synthesize imageView; 

我假設你有一個銷視圖或稱爲DropPinViewController視圖控制器。如果你只是使用圖像,它可以是一個UIImageView,但我的針腳有標籤,所以我用了一個xib。針應該指向xib的底部中心,因爲我們要在那裏放置一個錨點。

在你的MyViewController的viewDidLoad中,添加你的pin視圖作爲imageView的子視圖。將imageView作爲子視圖添加到scrollView。設置scrollView的內容大小。

scrollView.delegate = self; 

使用這些委託方法:

- (void)scrollViewDidZoom:(UIScrollView *)aScrollView 
{ 
    for (UIView *dropPinView in imageView.subviews) {  
    CGRect oldFrame = dropPinView.frame; 
    // 0.5 means the anchor is centered on the x axis. 1 means the anchor is at the bottom of the view. If you comment out this line, the pin's center will stay where it is regardless of how much you zoom. I have it so that the bottom of the pin stays fixed. This should help user RomeoF. 
    [dropPinView.layer setAnchorPoint:CGPointMake(0.5, 1)]; 
    dropPinView.frame = oldFrame; 
    // When you zoom in on scrollView, it gets a larger zoom scale value. 
    // You transform the pin by scaling it by the inverse of this value. 
    dropPinView.transform = CGAffineTransformMakeScale(1.0/scrollView.zoomScale, 1.0/scrollView.zoomScale); 
} 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return imageView; 
} 
+0

這太棒了我有點生病。我有許多其他令人費解的方法可以做到這一點...非常簡單。我切換到這個,它很好。 – malaki1974 2013-07-03 18:42:20

3

因此,我已經實施的一件事尚未解決我的問題,但我認爲這是正確的道路是從這篇文章。

Anchor a UIView

我有我的看法層次結構,如下。

UIScrollView 
- UIImageView (map image) 
- IconsView (layer image to hold icons) 

下一步,我需要解決的是讓我的腳加入到IconsView在同一地點停泊時UIImageView被放大和縮小。

我有一個for循環,通過我所有的銷,它們和IconsView子視圖,並更新了自己的原點X Y.這是在UIScrollView代表的scrollViewDidScroll方法去。

問題是這種技術在設備上的可怕表現,因爲當地圖上有很多引腳時,需要發生太多的處理。

0

幾個建議:

1)你會因爲它限制了它試圖通過默認顯示的引腳數的谷歌地圖應用程序通知。即使有更多可能的匹配,它也只會顯示最接近中心點的結果。

2)您可以在任何滾動或縮放操作期間暫時隱藏引腳,以便界面保持響應並在圖像視圖停止移動時重新繪製它們。3)如果您必須顯示大量的針腳併爲它們設置動畫,您還可以考慮使用CABasicAnimations同時爲針腳和背景設置動畫,使用變換背景的相同數學轉換針腳位置規模。這可以通過更平滑地擴展動畫並在相當低的水平上運行動畫來平滑顯示,但會引入其他挑戰和侷限。

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1

巴尼

0

我有同樣的問題,我覺得手動校正針的位置不是很漂亮的想法。然後,我寧願將我的針腳拖到代表函數viewForZoomingInScrollView:中返回的視圖中,並在縮放完成後更新子視圖(標記)大小。

但是,它讓我感到這在Google地圖應用中是可行的,但我無法重新創建它。

還有一件事: 任何人都可以解釋我行爲展覽當我添加子視圖到UIScrollView不是'viewForZoomingInScrollView'..?例如:

UIImageView* marker = [[UIImageView alloc] initWithImage: myMarkerImage]; 
[marker setCenter: CGPointMake(250,150)]; 
[myScrollView addSubview: marker]; 

對於初始視圖和平移時,這似乎工作正常。縮放時,這些子視圖不會調整大小,這實際上是我們的目標,但問題在於UIView框架原點以某種相當奇怪的方式在滾動視圖中移動。看起來,如果放大,marker.frame.size.origin總是朝着左上角(超級觀點的(0,0))移動,產生我們實際上縮小的印象。這也很奇怪,因爲「縮放中心」應該始終位於屏幕的中心,不是嗎?好吧,我現在不明白。

凹凸。 ;)

0

感謝David百靈。你的代碼除了我不得不註釋掉的一行之外。我的項目有一個UIScrollView,然後是一個顯示地圖的UIImageView,最後是檢測到雙擊手勢後代碼定位的引腳(按鈕)。你的代碼幫助我考慮了縮放比例。雖然限制,當我捏縮放,每個別針不「粘」到繪圖的特定部分,我雙擊。當我註釋掉這一行時,我偶然發現瞭解決方案:

//[dropPinView.layer setAnchorPoint:CGPointMake(0.5,1)];

我沒有完整的理解,但我的代碼現在可以工作!我希望別人能從我的經歷中受益。謝謝大衛!