gpt4 book ai didi

ios - 为什么我的雪没有落到地上?

转载 作者:行者123 更新时间:2023-11-29 10:22:54 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序(快速),背景是下雪。唯一的问题是我添加了重力动画,但是雪只是停留在原处。

这是我的代码:

ViewController.swift

import UIKit

@IBDesignable
class ViewController: UIViewController {

@IBInspectable var BgColor:UIColor = UIColor.whiteColor()

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = BgColor
/*listSubviewsOfView(self.view)*/ /*Not needed to answer this*/
var snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
snow.opaque = false
self.view.addSubview(snow)

let animator = UIDynamicAnimator(referenceView: self.view)
let gravity = UIGravityBehavior(items: [snow])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
animator.addBehavior(gravity)
/*
var snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
snow.opaque = false
snow.viewHeight = UIScreen.mainScreen().bounds.height
snow.addSubview(snow)
let animator = UIDynamicAnimator(referenceView: view)
let gravity = UIGravityBehavior(items: [snow])
animator.addBehavior(gravity)
*/
}


/*Not needed to solve this*/
/*
func listSubviewsOfView(views: UIView) {
var index = 0
let randomNumbers = [Int](1...24).shuffle()
for view in views.subviews
{
if let _ = view.restorationIdentifier
{
view.setValue(String(Int(randomNumbers[index])), forKey: "updateText")
index++
}
if index == randomNumbers.count {
break
}
}
}
*/

}

雪.swift:

import Foundation
import UIKit

@IBDesignable
class Snow:UIView
{

var viewHeight = CGFloat(0)
/*
required init(coder aDecoder: NSCoder) {
//Initilse UIView
super.init(coder: aDecoder)!
}
*/

override func drawRect(rect: CGRect) {
let path = UIBezierPath(ovalInRect: rect)
UIColor.whiteColor().setFill()
path.fill()
}
}

问题是,为什么我的雪在 (0,0) 处停留在屏幕顶部而不是掉下来,即使我已经告诉它有重力影响?

最佳答案

制作 View Controller 的 animatorgravity 属性。

import UIKit

@IBDesignable

class ViewController: UIViewController {

@IBInspectable var BgColor:UIColor = UIColor.whiteColor()

var animator: UIDynamicAnimator? = nil;
let gravity = UIGravityBehavior()

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = UIColor.blackColor()

let snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
snow.opaque = false
self.view.addSubview(snow)

animator = UIDynamicAnimator(referenceView:self.view);
animator?.addBehavior(gravity)

gravity.addItem(snow)
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction

}
}

关于ios - 为什么我的雪没有落到地上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33964668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com