嗨,我正在制作一个应用程序,你可以装饰一个圣诞树用小玩意我写了代码,以便能够在视图周围拖动它但我用完了小玩意我怎么做它所以我可以有很多的小玩意穿上圣诞树。我曾尝试多次复制一个小玩意,但后来将它们全部添加到代码中并且它不起作用它同时拖拽它们。如果你在图片中看到它有中间的小玩意但我想要它拖一个但是一个仍然在那里穿上
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var red_bauble_1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let gesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.wasDragged(_ :)))
red_bauble_1.addGestureRecognizer(gesture)
red_bauble_1.isUserInteractionEnabled = true
}
@objc func wasDragged (_ gesture: UIPanGestureRecognizer) {
let translation = gesture.translation(in: self.view)
let red_bauble_1 = gesture.view
red_bauble_1?.center = CGPoint(x: red_bauble_1!.center.x + translation.x, y: (red_bauble_1?.center.y)! + translation.y)
gesture.setTranslation(CGPoint.zero, in: self.view)
}
}
答案 0 :(得分:1)
如果要将可变数量的饰品添加为子视图并想要保留对它们的引用,只需为它们添加一个数组,例如:
def DetectGae():
"""Determine whether or not we're running on GAE.
This is based on:
https://developers.google.com/appengine/docs/python/#The_Environment
Returns:
True iff we're running on GAE.
"""
server_software = os.environ.get('SERVER_SOFTWARE', '')
return (server_software.startswith('Development/') or
server_software.startswith('Google App Engine/'))
然后你可以在将它们添加到树中时将你的饰品添加到该数组中,并且你有一个数组来跟踪所有这些。
例如:
var ornaments = [UIView]() // or, if they're image views, `[UIImageView]()`
产量: