我创建了一个按钮来添加UIImageView作为UIView(命名为backgroundView)的子视图,然后我希望所有UIImageViews都可以在UIView中拖动(但不能从它中拖出),所以我使用“backgroundView.cliptobounds = true“但它不起作用。
那么如何在添加子视图后通过按钮添加子视图,我可以在父UIView的范围内拖动子视图?
class ViewController: UIViewController {
@IBOutlet weak var addSubview: UIButton!
@IBOutlet weak var backgroundView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
backgroundView.clipsToBounds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonAddSubview(sender: AnyObject) {
let labelText = sender.titleLabel!?.text!
var subview : UIImageView
subview = UIImageView(frame:CGRectMake(300, 300, 200, 200));
subview = UIImageView(image: UIImage(named: labelText!))
backgroundView.addSubview(subview)
backgroundView.clipsToBounds = true
subview.image = UIImage(named:labelText!)
}
}