2012-09-26 10 views
10

新しいUICollectionViewでは、UICollectionViewCellに影を追加する方法がありません。私はこれについてどうやって行くのですか?別のビューを追加しますか?UICollectionViewCell影色

[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowPath = [UIBezierPath bezierPathWithRect:rect].CGPath; 
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowColor = [UIColor yellowColor].CGColor; 
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowRadius = .5; 
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowOpacity = .1; 

enter image description here

+2

は、それは '呼び出すことは非常に非効率的ではないです[self.collectionViewのcellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint: [認識者locationInView:[自己表示]]]] '複数回ですか? –

答えて

1

問題が最良のあなたの状況に特異的であるようにHow do I draw a shadow under a UIView?

に既存の回答で解決される可能性が最も高い、あなたはおそらく次のコードが何をするだろうコードを持っているでしょう(興味のあるセルを指すように、collectionViewとsomeIndexPathがどこにあるかによって異なります)。

明らかに細胞を得る他の方法があります。重要なのはshadowPathを設定するための2行目です。

+0

これは、セルの全体に影響を及ぼす要素の周りに影を作成していません。上記をご覧ください。 – BDGapps

+0

hmmm ...元の質問は "新しいUICollectionViewでは、UICollectionViewCellに影を追加する方法はわかりません。セル内の要素の周りにシャドウを配置する場合は、UICollectionViewCellのサブビューを収集し、それぞれのシャドウの設定を個別に実行する必要があります。 –

0

レイヤーのshadowOffsetプロパティを設定していません。

myCell.layer.shadowOffset = CGSizeMake(10,10); 
+0

それはまだセル全体に設定する白いビューに影を作成していません。それはただの変形された背景だけの真の影ではありません。 – BDGapps

2
[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.masksToBounds = NO; 
42

あなたはNOUIViewmasksToBoundsを設定し忘れています。 CustomCollectionviewCell.mに

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath]; 

    cell.layer.masksToBounds = NO; 
    cell.layer.borderColor = [UIColor whiteColor].CGColor; 
    cell.layer.borderWidth = 7.0f; 
    cell.layer.contentsScale = [UIScreen mainScreen].scale; 
    cell.layer.shadowOpacity = 0.75f; 
    cell.layer.shadowRadius = 5.0f; 
    cell.layer.shadowOffset = CGSizeZero; 
    cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath; 
    cell.layer.shouldRasterize = YES; 

    return cell; 
} 
+0

これは遅くなりませんか?私たちがCell Subclassでそれを行うことができるかどうか? – CalZone

+0

@CalZoneもちろん可能です。 – user3344977

+1

これをテストした後、 'contentScale'が設定されているにもかかわらず、イメージが間違った解像度でラスタライズされることがあることに気付きました。 'cell.layer.rasterizationScale = [UIScreen mainScreen] .scale;'これを修正します。 – DaGaMs

0

移動し、これを追加しよう:これは動作するはず

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     //////// make shadow of total view 
     self.clipsToBounds = NO; 
     self.layer.masksToBounds = NO; 
     self.layer.shadowRadius = 5; 
     self.layer.shadowOpacity = 0.5; 
     self.layer.shadowColor = [UIColor blackColor].CGColor; 
     self.layer.shadowOffset = CGSizeMake(0, 1); 
     self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath; 

     // make radius of the cell 
     self.layer.cornerRadius = 5; 

    } 
    return self; 
}