UIView Animation 예제

Posted on 2021-11-22 by GKSRUDTN99
Swift&Xcode Swift Animation

UIView Animation 예제

80 x 80 사이즈의 View가 좌우로 움직이는 animation
import UIKit

class ViewController: UIViewController {
  @IBOutlet weak var object: UIView!

  override func viewDidLoad() {
    super.viewDidLoad()
    let xOrigin: CGFloat = (UIScreen.main.bounds.width / 2) + 10
    let yOrigin: CGFloat = (UIScreen.main.bounds.height / 2) - 40
    object.frame = CGRect(x: xOrigin, y: yOrigin, width: 80, height: 80)
    start()
  }

  private func start() {
    var frame = object.frame
    let xPoint: CGFloat = (UIScreen.main.bounds.width / 2) - 90
    let yPoint: CGFloat = (UIScreen.main.bounds.height / 2) - 40
    frame.origin = CGPoint(x: xPoint, y: yPoint)

    UIView.animate(withDuration: 1.0,
                   delay: 0,
                   options: [.repeat, .autoreverse],
                   animations: {
      self.object.frame = frame
    }, completion: nil)
  }
}
Animation이 가능한 속성들
  • frame, bounds, center, transfrom, alpha, backgroundColor