๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป/iOS

Swift Card Flip ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„ํ•˜๊ธฐ

reujusong 2020. 9. 12. 23:50

iOS ์•ฑ์„ ๋งŒ๋“ค๋•Œ, ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ ์šฉ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด๊ธธ๋ž˜ ์†Œ๊ฐœํ•ด๋ณธ๋‹ค.

 

 

 

1. Xcode - Single View App ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

2. Asset ์— ์ „ํ™˜์‹œํ‚ฌ ์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•œ๋‹ค.

 

Image: ๋‚ฎ ์‚ฌ์ง„ / Image-1: ๋ฐค ์‚ฌ์ง„

 

3. Storyboard๋กœ ๊ฐ€์„œ ์ด๋ฏธ์ง€๊ฐ€ ๋ณด์—ฌ์งˆ ImageView์™€ Button์„ ์ƒ์„ฑํ•œ๋‹ค.

 

 

4. Contraints๋ฅผ ๊ฑธ์–ด์ค€๋‹ค. (์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ View๋“ค์„ ๊ณ ์ •์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.)

5. command+option+control+enter ํ‚ค๋กœ Assistant Editor๋ฅผ ์ผœ๊ณ  control ์„ ๋ˆ„๋ฅธ ์ƒํƒœ๋กœ ImageView๋ฅผ ๋ณด์กฐํŽธ์ง‘๊ธฐ๋กœ ๋Œ์–ด์˜จ๋‹ค.

 

 

6. Button์€ ๋งˆ์šฐ์Šค ์šฐํด๋ฆญ์„ ํ•œ ํ›„ Touch Up Inside ๋ฅผ ๋Œ์–ด์„œ ๋ณด์กฐ ํŽธ์ง‘๊ธฐ์— ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

7. ViewController์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    var isDayImage: Bool = true

    override func viewDidLoad()
    {
        super.viewDidLoad()
        imageView.image = UIImage(named: "Image")
    }

    @IBAction func buttonClicked(_ sender: Any) {
        if isDayImage {
            isDayImage = false
            let toImage = UIImage(named: "Image-1")
            imageView.image = toImage
            UIView.transition(with: imageView, duration: 0.3, options: .transitionFlipFromLeft, animations: nil, completion: nil)
        }
        else {
            isDayImage = true
            let toImage = UIImage(named: "Image")
            imageView.image = toImage
            UIView.transition(with: imageView, duration: 0.3, options: .transitionCrossDissolve, animations: nil, completion: nil)
        }
    }
}

 

์• ๋‹ˆ๋ฉ”์ด์…˜์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ๋ถ€๋ถ„์€

 

UIView.transition(with: imageView, 
        duration: 0.3, 
                 options: .transitionFlipFromLeft, 
                 animations: nil, 
                 completion: nil)

 

์ธ๋ฐ, imageView๋ฅผ ๊ฐ€์ง€๊ณ  duration์˜ ์‹œ๊ฐ„๋™์•ˆ options์— ํ•ด๋‹นํ•˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ง„ํ–‰ํ•œ๋‹ค. .transitionFlipFromLeft ๋Š” ์™ผ์ชฝ์—์„œ ์นด๋“œ๊ฐ€ ๋’ค์ง‘์–ด์ง€๋Š” ์•ก์…˜์ด๊ณ , .transitionCrossDissolve ๋Š” ํฌ๋ฏธํ•˜๊ฒŒ ๋ณ€ํ™”ํ•˜๋Š” ์•ก์…˜์ด๋‹ค.

 

์ด ์™ธ์—๋„ ์—ฌ๋Ÿฌ ์˜ต์…˜๋“ค์ด ์žˆ๋‹ค.

 

 

์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ๋ฅผ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์—๋Š” transition ๋ง๊ณ ๋„ animate๋ผ๋Š” ํƒ€์ž… ๋ฉ”์†Œ๋“œ๋„ ์žˆ๋‹ค. ์ฐจ์ด๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ์ง€ ๋ชจ๋ฅด๋‹ˆ ํ•™์Šต ํ›„์— ๋‹ค๋ฃจ์–ด ๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค..

 

 

 

 

์ฐธ๊ณ 
๊ณต์‹ ๋ฌธ์„œ

'๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป > iOS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Core Animations ํ•™์Šตํ•˜๊ธฐ - PieGraph  (0) 2020.12.11
UIKit๋ž€?  (0) 2020.07.12
Cocoa Touch ํ”„๋ ˆ์ž„์›Œํฌ๋ž€?  (0) 2020.07.08
AVFoundation, Timer๋ž€?  (0) 2020.07.08
UIButton, UISlider, UILabel์ด๋ž€?  (0) 2020.07.07