๐ŸŽ iOS/UI

[iOS/UI] Swift ์•จ๋ฒ” ์‚ฌ์ง„ ๊ณ ๋ฅธ ๊ฐฏ์ˆ˜ ์ฐจ๋ก€๋กœ ๋ฒˆํ˜ธ ๋ถ€์—ฌํ•˜๊ธฐ

Fomagran ๐Ÿ’ป 2020. 4. 1. 21:47
728x90
๋ฐ˜์‘ํ˜•

์šฐ์„  ๋‚˜๋Š” ์•„๋ž˜ ์นดํ†ก ์‚ฌ์ง„๊ณ ๋ฅด๊ธฐ์™€ ๊ฐ™์ด ํƒญํ•œ ์ˆœ์„œ๋Œ€๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ€์—ฌ๋˜๋ฉด์„œ ์‚ฌ์ง„๋ฐฐ์—ด์— ์‚ฌ์ง„์ด ๋“ค์–ด๊ฐ€๊ฒŒ ํ•˜๊ณ 

๋‹ค์‹œ ํƒญํ•  ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ์‚ฌ์ง„์„ ์‚ญ์ œํ•ด์ฃผ๊ณ  ๊ทธ๋ณด๋‹ค ํฐ ๋ฒˆํ˜ธ๋“ค์€ ์‚ญ์ œํ•œ ๊ฐฏ์ˆ˜๋งŒํผ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์•„์ ธ์•ผํ–ˆ๋‹ค.

์šฐ์„  2๊ฐœ์˜ ๋ฐฐ์—ด์ด ํ•„์š”ํ•œ๋ฐ ํ•˜๋‚˜๋Š” ์‚ฌ์ง„์„ ๋‹ด์„ ๋ฐฐ์—ด๊ณผ ํ•˜๋‚˜๋Š” ๋ช‡ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ์„ ํƒ๋˜์—ˆ๋Š”์ง€ ๋‹ด์„ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค.

1
2
    var photos = [UIImage]()
    var photoarray = [Int]()

๊ทธ ๋‹ค์Œ cellForItemAt์—

ํฌํ† ์–ด๋ ˆ์ด๊ฐ€ ๋น„์–ด์žˆ์ง€์•Š๋‹ค๋ฉด !photoarray.isEmpty{ for๋ฌธ์„ ์ด์šฉํ•ด photoarray์•ˆ์— ์žˆ๋Š” ์ˆซ์ž๋“ค์„ ์ฐจ๋ก€๋กœ indexPath์™€ ๋น„๊ตํ•ด์„œ ๋งž๋‹ค๋ฉด ํ•ด๋‹น ์ˆซ์ž๋ผ๋ฒจ(cell.number)๋ฅผ i๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. ๊ทธ๋Ÿฌ๋ฉด photoarray์•ˆ์— ์ˆœ์„œ๋Œ€๋กœ ํ…์ŠคํŠธ๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  cell.number.isHidden์„ ํ’€์–ด์ฃผ๋ฉด ์ˆซ์ž๊ฐ€ ๋ณด์ด๊ฒŒ ๋œ๋‹ค. borderWidth์™€ borderColor๋Š” ํ•ด๋‹น ์…€์„ ๋ˆŒ๋ €์„ ๋•Œ ํ…Œ๋‘๋ฆฌ์˜ ์ƒ‰๊น”๊ณผ ๋‘๊ป˜๋ฅผ ์ •ํ•ด์ค€ ๊ฒƒ์ด๋‹ค,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collection.dequeueReusableCell(withReuseIdentifier: "CustomPhotoCell"for: indexPath) as! CustomPhotoCell
        cell.image.image = fetchimages[indexPath.item]
        cell.image.contentMode = .scaleAspectFill
        cell.layer.borderWidth = 0
        cell.number.isHidden = true
         if !photoarray.isEmpty{
               for i in 0...photoarray.count-1 {
                   if indexPath.item == photoarray[i] {
                     cell.layer.borderColor = UIColor(displayP3Red: 162/255, green: 32/255, blue: 58/255, alpha: 1).cgColor
                        cell.layer.borderWidth = 3
                        cell.number.isHidden = false
                       cell.number.text = "\(i)"
                   }
               }
        }
        cell.number.layer.masksToBounds = true
        return cell
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

์…€์„ ์„ ํƒํ–ˆ์„ ๋•Œ didSelectItemAt์— 

๋งŒ์•ฝ ํ•ด๋‹น ์ˆซ์ž๋ผ๋ฒจ์ด ์ˆจ๊ฒจ์ ธ ์žˆ๋‹ค๋ฉด (๋‚˜๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ตœ๋Œ€ 8๋Œ€๊นŒ์ง€ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋†จ๊ธฐ ๋•Œ๋ฌธ์— photos์•ˆ์— ์žˆ๋Š” ์‚ฌ์ง„์ด 8๊ฐœ ๋ฏธ๋งŒ์ด๋ผ๋ฉด ๋„ฃ์—ˆ๋‹ค.) photoarray์— ํ•ด๋‹น indexPath.item์„ ๋„ฃ์–ด์ฃผ๊ณ  photos์—” ํ•ด๋‹น image๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด

photos์—์„œ ํ•ด๋‹น cell์ด ์–ด๋–ค ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ณ  ๊ทธ ์ˆซ์ž์— ๋งž๊ฒŒ ์‚ญ์ œํ•ด์ค€๋‹ค. (photoarray๋„ ๋งˆ์ฐฌ๊ฐ€์ง€)

1
2
3
4
5
6
7
8
9
10
11
12
 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collection.cellForItem(at: indexPath) as! CustomPhotoCell
        if cell.number.isHidden == true {
            if photos.count < 8 {
            photoarray.append(indexPath.item)
            photos.append(cell.image.image!)
            }
        } else{
            photos.remove(at:Int(cell.number.text!)!)
            photoarray.remove(at: Int(cell.number.text!)!)
        }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

์œ„์™€ ๊ฐ™์ด ํƒญํ–ˆ์„ ๊ฒฝ์šฐ photoarray์—” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ €์žฅ๋˜๊ณ  ์‚ญ์ œ๋œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•