簡體   English   中英

將對象添加到多個視圖

[英]Adding object to multiple views

我有一個UIView的子類,叫做PinView,它包含一個圖像。 基本上PinView會多次添加到我的應用程序,然后我對PinView對象執行轉換。 問題是當我添加很多PinView時,我的應用程序變得遲鈍,因為我正在改變每個PinView。

理想情況下,我希望有一個“靜態”PinView多次添加到UIView,但我只需要轉換一次。 目前這似乎不起作用。 每次我將靜態PinView添加到我的UIView時,它只會出現在UIView中一次(因為它只能有一個我正在猜測的超級視圖)。

我很難找到解決這個問題的最佳方法 - 如何使用指向PinView的單個指針,將其多次添加到UIView並能夠對PinView執行轉換,並將其傳遞給顯示的PinViews在我的UIView? (順便說一下,所有PinView的變換總是相同的)。

我認為這將是獲得性能改進的最佳方式,如果不是這樣,請告訴我。

更新:

- (void)layoutSubviews
{
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0/self.zoomValue, 1.0/self.zoomValue);
    NSMutableArray *mut = nil;
    PinView *pinView = nil;
    CallOutView *callOut = nil;

    //get all dictionary entries
    for(NSString *identifier in self.annotationsDict.allKeys){
    //get the array from dictionary
        mut = [(NSArray *)([self.annotationsDict objectForKey:identifier]) mutableCopy];        
        //change layout if not nil
        if([[mut objectAtIndex:PIN] isKindOfClass:[PinView class]]){
            pinView = ((PinView *)[mut objectAtIndex:PIN]);
            pinView.transform = transform;
            [mut replaceObjectAtIndex:PIN withObject:pinView];            
        }
        if([[mut objectAtIndex:CALL_OUT] isKindOfClass:[CallOutView class]]){
            callOut = ((CallOutView *)[mut objectAtIndex:CALL_OUT]);
            callOut.transform = transform;
            [mut replaceObjectAtIndex:CALL_OUT withObject:callOut];
            if(pinView !=nil)callOut.center = CGPointMake(pinView.center.x, pinView.center.y - pinView.frame.size.height);
        }

        [self updateAnnotationsKey:identifier forObject:[NSArray arrayWithArray:mut]];

        mut = nil;
        pinView = nil;
        callOut = nil;

    }


}

更新:

刪除了上面的內容,現在只需:

- (void)layoutSubviews
{
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0/self.zoomValue, 1.0/self.zoomValue);

    for(UIView *view in self.subviews){
        view.transform = transform;
    }


}

我擔心這不可能。 每個UIView實例只能添加到屏幕一次。

如果您的所有視圖都有類似的轉換,那么使用CAReplicatorLayer這樣的系統可能會更加幸運,這是一個自動創建具有不同轉換的CALayers副本的系統。

只有當您的視圖都以網格或圓形或其他方式排列時,這才有效。 如果它們只是隨機點綴,它將無濟於事。

如果你試圖繪制超過100個視圖,你可能只是碰到了iOS上Core Animation的基本性能上限。

下一個嘗試的方法是使用OpenGL繪制你的引腳,可能使用像SparrowCocos2D這樣的庫來簡化用OpenGL繪制多個轉換圖像(我推薦Sparrow因為它與其他UIKit控件更好地集成--Cocos更合適對於游戲)。

更新:

這段代碼是不必要的:

    mut = [(NSArray *)([self.annotationsDict objectForKey:identifier]) mutableCopy];

    if([[mut objectAtIndex:PIN] isKindOfClass:[PinView class]]){
        pinView = ((PinView *)[mut objectAtIndex:PIN]);
        pinView.transform = transform;
        [mut replaceObjectAtIndex:PIN withObject:pinView];            
    }

下面的代碼就足夠了,因為設置轉換不會修改指向對象的指針,所以它會更新數組中的對象,即使數組不可變,並且數組對象被聲明為'id'所以它們不會如果將它們分配給已知類型的變量,則需要進行強制轉換。

    mut = [self.annotationsDict objectForKey:identifier];

    if([[mut objectAtIndex:PIN] isKindOfClass:[PinView class]]){
        pinView = [mut objectAtIndex:PIN];
        pinView.transform = transform;
    }

我還認為你可以刪除isKindOfClass:檢查你是否只對那些對象類型使用那些數組索引。 這似乎是一個很好的預防措施,但是如果你在一個循環中反復這樣做,它會帶來性能損失。

但對於10個觀點,我根本不會指望這一點很慢。 您是否在不移動標注中心的情況下嘗試過它。 那表現更好嗎? 如果是這樣,你可以將其限制為當前可見的標注,或者使用CGAffineTransformTranslate移動它們而不是設置中心(可能會更快一些)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM