swift 快速奔跑的兔几 本节的内容是:UIDynamic:UI和重力

前端之家收集整理的这篇文章主要介绍了swift 快速奔跑的兔几 本节的内容是:UIDynamic:UI和重力前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var dynamicImageView: UIImageView!
    var dynamicAnimator = UIDynamicAnimator()

    override func viewDidLoad() {
        super.viewDidLoad()

        let img1 = UIImage(named:"littleYellowMan.jpg")
        dynamicImageView.image = img1

        dynamicAnimator = UIDynamicAnimator(referenceView: self.view)

        let gravityBehavior = UIGravityBehavior(items: [self.dynamicImageView])
        dynamicAnimator.addBehavior(gravityBehavior)

        let collisionBehavior = UICollisionBehavior(items: [self.dynamicImageView])
        collisionBehavior.translatesReferenceBoundsIntoBoundary = true
        dynamicAnimator.addBehavior(collisionBehavior)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

运行结果:图片掉落下去,撞到屏幕的边界

原文链接:https://www.f2er.com/swift/325518.html

猜你在找的Swift相关文章