2017-07-06 3 views
0

Ich habe UICollectionView, in dem die Center-Zelle wie unten gezeigt Bild hervorgehoben wird.UICollectionView markierte Zelle mit Schatten

enter image description here

i verwendet haben folgenden Code Schatten um die Mitte Zelle zu machen.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell : STSuggestStyleCell = collectionView.dequeueReusableCell(for: indexPath) 

    if indexPath.row == 4{ 

     cell.imageProfile.layer.cornerRadius = 2.0 
     cell.imageProfile.layer.borderWidth = 1.0; 
     cell.imageProfile.layer.borderColor = UIColor.clear.cgColor 
     cell.imageProfile.layer.masksToBounds = true; 

     cell.imageProfile.layer.shadowColor = UIColor.lightGray.cgColor; 
     cell.imageProfile.layer.shadowOffset = CGSize(width: 2.0, height: 2.0); 
     cell.imageProfile.layer.shadowRadius = 2.0; 
     cell.imageProfile.layer.shadowOpacity = 2.0; 
     cell.imageProfile.layer.masksToBounds = false; 
     cell.imageProfile.layer.shadowPath = UIBezierPath(roundedRect: cell.imageProfile.bounds, cornerRadius: cell.imageProfile.layer.cornerRadius).cgPath 

        cell.imageProfile.backgroundColor = UIColor.white 


    } 

    return cell 
} 

Aber es funktioniert nicht richtig.

Kann mir jemand sagen, wie man das obige Design mit UICollectionView erreicht?

Antwort

0

Blick auf die rote Linie für die Zelle in der Mitte

enter image description here

Die Zelle selbst hat keine Schatteneffekte. Der Schatten ist auf den Nachbarzellen. Was Sie also tun müssen, ist, die Zelle zu bitten, andere Zellen zu "überlagern".

Rufen Sie einfach cell.clipToBounds = true an, damit Ihre Center-Zelle Nachbarzellen überlagern kann. Stellen Sie dann sicher, dass Sie Ihren Schatteneffekt aus der Mittelzelle ausbreiten.

+0

Ich habe bereits obige Lösung versucht. es funktioniert nicht. –

+0

@ Aman.Sanghani Können Sie den Schatten sehen? Oder der Schatten in der Zelle? –

+0

ja, es hat funktioniert. Vielen Dank:) –

0

Vielleicht müssen Sie zwei verschiedene Zellen erstellen. wegen seiner Wiederverwendungseigenschaft.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if indexPath.row == 4{ 
     let cell : STSuggestStyleCell = collectionView.dequeueReusableCell(for: indexPath) 
     cell.imageProfile.layer.cornerRadius = 2.0 
     cell.imageProfile.layer.borderWidth = 1.0; 
     cell.imageProfile.layer.borderColor = UIColor.clear.cgColor 
     cell.imageProfile.layer.masksToBounds = true; 

     cell.imageProfile.layer.shadowColor = UIColor.lightGray.cgColor; 
     cell.imageProfile.layer.shadowOffset = CGSize(width: 2.0, height: 2.0); 
     cell.imageProfile.layer.shadowRadius = 2.0; 
     cell.imageProfile.layer.shadowOpacity = 2.0; 
     cell.imageProfile.layer.masksToBounds = false; 
     cell.imageProfile.layer.shadowPath = UIBezierPath(roundedRect: cell.imageProfile.bounds, cornerRadius: cell.imageProfile.layer.cornerRadius).cgPath 

     cell.imageProfile.backgroundColor = UIColor.white 
     return cell 
    }else{ 
     let cell : STSuggestNomalCell = collectionView.dequeueReusableCell(for: indexPath) 
     return cell 
    } 
} 
Verwandte Themen