2011-11-14 2 views
0

問題必要:既存のUIViewの上にスタッキングビュー...コードレビューは

  1. を、カードデッキの画像(大きな画像)から背景画像
  2. をロードの一部(1枚のカードをカット大きな画像)と背景画像の上にカードが

次のコードは動作します

  • 場所。それはまさに私が必要とするものです。私はあなたに尋ねます。

    1. 私はそれを正しくやっていますか?これはそれが行われている方法ですか?
    2. 私が実際に使用するすべての手順は必要ですか?私は強い疑い私はそれがより複雑な、それは

  • はお時間をいただき、ありがとうございますする必要が行われてきた

    // Define overall UIView area and create a view 
    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
    
    // Gain access to background image 
    UIImage *backgroundImage = [UIImage imageNamed:@"green_background.jpg"]; 
    
    // Put background image on top of a 
    UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage]; 
    
    // So now we have the view of some size with some image bahind it 
    [background addSubview:myImageView];  
    
    
    // ##################################################################### 
    
    CGRect positionOnParentView = CGRectMake(40, 40, 50, 130); 
    
    CGImageRef bigImage = [UIImage imageNamed:@"cards.png"].CGImage;  
    CGImageRef partOfABigImage = CGImageCreateWithImageInRect(bigImage, CGRectMake(0, 0, 200, 50)); 
    
    // get an image to be displayed 
    UIImage *partOfImage = [UIImage imageWithCGImage:partOfABigImage]; 
    
    // Put it on it's onw view 
    UIImageView *cardImage = [[UIImageView alloc] initWithImage:partOfImage]; 
    
    // create a UIview and add image 
    UIView *card = [[UIView alloc] initWithFrame:positionOnParentView]; 
    [card addSubview:cardImage]; 
    
    [background addSubview:card]; 
    
    [[self view] addSubview:background]; 
    

    答えて

    1

    このビュー階層を作成している:

    UIView (self.view) 
        UIView (background) 
        UIImageView (backgroundImage - backgroundImage) 
        UIView (card) 
         UIImageView (cardImage - partOfABigImage) 
    

    UIImageViewは、サブビューを持つことができます。だからあなたはそれらの中間的な意見をすべて必要としますか?この単純な階層で十分でしょうか?

    UIImageView (self.view - backgroundImage) 
        UIImageView (cardImage - partOfABigImage) 
    
    1

    物事のカップル:たぶん、あなただけのdidnの

    1. 」あなたのリリースステートメントを含めるが、あなたがallocと呼ぶアイテムをリリースしていることを確認してください。また、それらを自動リリースすることもできます。たとえば、

      UIView * background = [[UIView alloc] initWithFrame:CGRectMake(0、0、320、460)] autorelease];

    2. はい、あなたが実際に行っているコードの量を減らす唯一の方法は、各カードに自分のイメージを与えて大きなイメージ内で切り抜く必要がないようにすることです。

    +0

    自動参照カウント(ARC)を見て、リリース情報が必要ないと思うかもしれません;) – Faser

    関連する問題