2017-04-06 1 views
1

UICollectionView의 셀을 선택하거나 선택 취소 한 상태에서 이미지의 새로운 색을 설정하려고합니다. 때마다 내가 작동하는 이미지의 색조 색을 설정하지 않지만, 나는 그것의 기본 blue 색 싶지 않아요. 그래서 내가 뭐하는 거지 것은 :이미지의 색조가 변경되고 싶지 않습니다.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let currentCell: UICollectionViewCell? 
     switch collectionView { 
     case myCollectionView: 
      print("clicked") 
      currentCell = myCollectionView.cellForItem(at: indexPath)! 
       currentCell?.tintColor = UIColor.white 

     case colorsCollectionView: 
      break 
     default: 
      break 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
     let currentCell: UICollectionViewCell? 
     switch collectionView { 
     case vehiclesCollectionView: 
      print("deselected") 
      currentCell = myCollectionView.cellForItem(at: indexPath)! 
      currentCell?.tintColor = UIColor.groupTableViewBackground 
     case colorsCollectionView: 
      break 
     default: 
      break 
     } 
    } 

사람이 말할 수 다음 didselected 및 해제 기능에 등

let borderWidth = itemBorder.frame.width 
      let borderHeight = itemBorder.frame.height 

      myImage = UIImageView(frame: CGRect(x: 0, y: 0, width: borderWidth - 1/4 * borderWidth, height: borderHeight - 1/5 * borderHeight)) 
      myImage.image = UIImage(named: myCollection[indexPath.row])?.withRenderingMode(.alwaysTemplate) 
      myImage.tintColor = UIColor.groupTableViewBackground 
      myImage.center = CGPoint(x: tmpCell.bounds.width/2, y: tmpCell.bounds.height/2) 
      myImage.contentMode = .scaleAspectFit 
      tmpCell.contentView.addSubview(myImage) 

과 :

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 함수에서, 나는 코드로 내 이미지를 inicialazing하고 나 뭐 잘못 됐니? 미리 감사드립니다.

솔루션

솔루션 그래서 basiclly 만 선택 또는 선택 해제 방법으로이 문제를 추가 할 필요가 내가없는 이미지에 셀을 가리키고 색상의 색조를 업데이트하고 싶어 할 때마다 :

currentCell = myCollectionView.cellForItem(at: indexPath)! 
      let image = currentCell?.contentView.subviews[1] as! UIImageView 
      image.tintColor = UIColor.white 

답변

1

나는 당신이 currentCell?.tintColor을 설정하는 것을 볼하지만 난 당신이 아마 내가 볼 또한 currentCell?.vehicleImageView.image.tintColor

을 설정해야합니다 가정하여 vehicleImage (이 이름은 아마도 vehicleImageView이어야합니다)과 myImage (UIImage)이 contentView에 서브 뷰로 추가되었으므로 조금 혼란 스럽습니다. UIView 서브 클래스를 서브 뷰로 추가하는 것만 가능하다고 생각했습니다.

나는 당신이 image.tintColor를 설정하고에 cell.myImageView.tintColor을 변경할 수있는 사용자 정의 UICollectionViewCellmyImageView라는 출구를 만들 제안하여 didSelectdidDeselect

스토리 보드에서 그들을 추가하지 않으면, 당신은 여전히를 만들 수 있습니다 vehicleImageView이라는 속성이있는 UICollectionViewCell의 하위 클래스 cellForRow에서 필요에 따라 프레임 및 이미지를 설정할 수 있습니다. 이제 didSelectdidDeselect에서 참조 할 수있는 속성을 cell.vehicleImageView.image.tintColor으로 갖게됩니다.

속성이있는 하위 클래스를 만들지 않으려면 기본적으로 모든 하위보기를 반복하고 이미지보기를 찾고 이미지 tintColor을 설정해야합니다. UICollectionViewCelltintColor을 설정하면이 문제를 해결하지 않습니다. imageView.image

호프가 도움이 될 것입니다.

+0

이 맞습니다. 모든 이름이 바뀌지 않았습니다. 이거 할거야! – yerpy

+0

수락하고 upvoting 주셔서 감사합니다! –

+0

죄송합니다. 작동하지 않습니다. C – yerpy

관련 문제