Created
April 20, 2018 08:04
-
-
Save godrm/61f06c95fb0368e850fe88a111f2fc60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// HelloAnimation | |
// | |
// Created by JK on 19/04/2018. | |
// Copyright © 2018 JK. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController, UIDynamicAnimatorDelegate { | |
@IBOutlet weak var imageView: UIImageView! | |
@IBOutlet weak var button: UIButton! | |
var animator : UIDynamicAnimator! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
animator = UIDynamicAnimator(referenceView: self.view) | |
createDisplayLink() | |
} | |
@IBAction func buttonTouched(_ sender: Any) { | |
imageView.animationImages = [ #imageLiteral(resourceName: "1.png"),#imageLiteral(resourceName: "2.png"),#imageLiteral(resourceName: "3.png"),#imageLiteral(resourceName: "4.png"),#imageLiteral(resourceName: "5.png") ] | |
imageView.animationDuration = 0.5 | |
imageView.startAnimating() | |
setDynamics() | |
//------------ (80, 80) | |
UIView.animate(withDuration: 1) { [unowned self] in | |
//frame 속성 바꾸기 | |
// self.imageView.frame = CGRect(x: 10, y: 10, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height) | |
self.imageView.transform = CGAffineTransform.init(rotationAngle: 3.14/4.0) | |
self.imageView.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5) | |
self.imageView.transform = CGAffineTransform.identity | |
} | |
} | |
func createDisplayLink() { | |
let displaylink = CADisplayLink(target: self, | |
selector: #selector(step)) | |
displaylink.add(to: .current, | |
forMode: .defaultRunLoopMode) | |
} | |
@objc func step(displaylink: CADisplayLink) { | |
print(displaylink.timestamp) | |
} | |
func setDynamics() { | |
let square = UIView(frame: CGRect(x: 110, y: 100, width: 100, height: 100)) | |
square.backgroundColor = UIColor.gray | |
self.view.addSubview(square) | |
let gravity = UIGravityBehavior(items: [square]) | |
animator.addBehavior(gravity) | |
let barrier = UIView(frame: CGRect(x: 0, y: 300, width: 130, height: 20)) | |
barrier.backgroundColor = UIColor.red | |
self.view.addSubview(barrier) | |
let collision = UICollisionBehavior(items: [square]) | |
collision.translatesReferenceBoundsIntoBoundary = true | |
animator.addBehavior(collision) | |
let rightEdge = CGPoint(x: barrier.frame.origin.x + barrier.frame.size.width, y: barrier.frame.origin.y) | |
collision.addBoundary(withIdentifier: "barrier" as NSString, from: barrier.frame.origin, to: rightEdge) | |
animator.delegate = self | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment