Цвет тени UICollectionViewCell
В новом UICollectionView я не вижу, как добавить тень в 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]()
Ответы
Ответ 1
Вы забываете установить masksToBounds
на UIView
на NO
. Это должно работать:
- (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;
}
Ответ 2
[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.masksToBounds = NO;
Ответ 3
Скорее всего, ваша проблема лучше всего решена с помощью существующего ответа на Как рисовать тень под UIView?
чтобы быть конкретным для ваших обстоятельств, у вас, вероятно, будет код, который будет делать то, что делает следующий код (в зависимости от того, где вы получаете свой CollectionView и someIndexPath, чтобы указать на интересующую вас ячейку):
UICollectionViewCell* collectionViewCell
= [collectionView dequeueReusableCellWithReuseIdentifier:DEFINED_IDENTIFIER forIndexPath:someIndexPath];
collectionViewCell.layer.shadowPath = [UIBezierPath bezierPathWithRect:collectionViewCell.bounds].CGPath;
есть, очевидно, другие способы получить ячейку. важная вещь - вторая строка, чтобы установить shadowPath.
Ответ 4
Вы не устанавливаете свойство shadowOffset на уровне.
myCell.layer.shadowOffset = CGSizeMake(10,10);
Ответ 5
Перейдите в CustomCollectionviewCell.m и попробуйте добавить это:
- (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;
}