iOS中如何制作图像

时间:2017-10-31 11:30:09

标签: ios swift uiimageview uikit-dynamics

我需要做的一件事就是让火球掉下来。我可以使用正方形,但我怎样才能使它与我的图像一起工作?我用来让广场倒塌的网站在这里:https://www.raywenderlich.com/76147/uikit-dynamics-tutorial-swift

这是我对图片的尝试:

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var fireball: UIImageView!

    var animator: UIDynamicAnimator!
    var gravity: UIGravityBehavior!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background23.png")!)

        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [fireball])
        animator.addBehavior(gravity)

        collision = UICollisionBehavior(items: [fireball])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }

    @IBOutlet weak var imageToMove: UIImageView!

    @IBAction func buttonmoveleft(_ sender: Any) {
        self.imageToMove.frame.origin.x += 20
    }

    @IBAction func buttonmoveright(_ sender: Any) {
        self.imageToMove.frame.origin.x -= 20
    }
}

用方块:

import UIKit

class SecondViewController: UIViewController {

    var animator: UIDynamicAnimator!
    var gravity: UIGravityBehavior!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background23.png")!)

        let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        square.backgroundColor = UIColor.gray
        view.addSubview(square)

        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [square])
        animator.addBehavior(gravity)

        collision = UICollisionBehavior(items: [square])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }

    @IBOutlet weak var imageToMove: UIImageView!

    @IBAction func buttonmoveleft(_ sender: Any) {
        self.imageToMove.frame.origin.x += 20
    }

    @IBAction func buttonmoveright(_ sender: Any) {
        self.imageToMove.frame.origin.x -= 20
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用' self'传递对象。 marker(对象属性):

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var fireball: UIImageView!

    var animator: UIDynamicAnimator!
    var gravity: UIGravityBehavior!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background23.png")!)

        animator = UIDynamicAnimator(referenceView: self.view)
        gravity = UIGravityBehavior(items: [self.fireball])

        collision = UICollisionBehavior(items: [self.fireball])
        collision.translatesReferenceBoundsIntoBoundary = true

        animator.addBehavior(collision)
        animator.addBehavior(gravity)
    }

    @IBOutlet weak var imageToMove: UIImageView!

    @IBAction func buttonmoveleft(_ sender: Any) {
        self.imageToMove.frame.origin.x += 20
    }

    @IBAction func buttonmoveright(_ sender: Any) {
        self.imageToMove.frame.origin.x -= 20
    }
}